Wednesday 31 August 2011

jquery imgnotes



imgnotes is nothing but notes over images,still not clear?okie, this is something similar to tagging the images as we do in social networking sites like facebook,orkut...etc.




Include the following in the head of your html:



<link rel="stylesheet" type="text/css" href="css/imgnotes.css">
<link rel="stylesheet" type="text/css" href="css/styles.css">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.imgnotes-0.1.js"></script>


and the files can be downloaded from http://plugins.jquery.com/project/ImgNotes.


The notes to be tagged is given in json format,for eg as below



notes = [ {
"x1" : "195",
"y1" : "183",
"height" : "20",
"width" : "26",
"note" : "<b>  DP means DurgaPrasad not Dharu Party</b>"
} ];





x1 - The X co-ordinate of the top-left corner of the note area
y1 - The Y co-ordinate of the top-left corner of the note area
note - The text of the note, it supports HTML as well
height and width specifies the image range to be covered.
x1 and y1 are relative the position of the image on which the notes will be displayed, so when you move the image around in your page the the notes will move with it.
and the following piece of js will do the work for you.
$(window).load(function() {
$('#tern').imgNotes(notes);
  //someMoreOnClickFunctions();
});

This plugin is very useful in making the images of your website more dynamic and user interactive and can be used to make various parts of your static image as navigational sources....
Thanks for Reading.... cheers :-)



Monday 29 August 2011

Jquery - Image appender Plugin to tag



I was using a lot of jquery plug-ins say for Date component,for creating charts,for sorting tables and just thought of writing a very simple plug-in (jqueryImageAppender.js) to start with.

The compressed version of the jqueryImageAppender.js  is as below.This takes 3 params
1.id of the container(table)
2.Path of the image to be appended
3.Position to be placed (front of tr or end of tr)

so, this function appends the image to each tr at the specified position (at the begin or end)




appendImage('DATA_TABLE','list.gif','not_end') will result in the table as below.










Thanks for reading...cheers  ;-)

Tuesday 23 August 2011

Sample Stock Market Simulator - Spring 3.0



    The module I've decided to work is simple and the following use case describes it.

    I shall use
    Spring 3 mvc,
    jdbc template,  
    mysql, 
    Tomcat 7
    js and jquery Ajax



















and the proj structure looks as below.

    











will update ...cheers :-)

Sunday 7 August 2011

Obsolete Object References




Unlike C and C++ ,Java takes care of memory management through Garbage-collector,but  care must be taken to avoid the unintentional objects (Obsolete Object References ) occurring.An obsolete reference is a reference that will never be dereferred again.

refer the following sample code of  Stack.

































When the pop is called the copy of the reference is poped out ,but there will be unintentional reference still lying in the array  and it will be replaced for the push of a new item.


But,if there are series of pop outs and no new elements pushed there it results in many obsolete references.This can be avoided by nulling out the references after each pop as below.













Eliminating this unintentional references results in  Better performance, less memory, and When nulling out the references, if they get de-referred to later by a mistake, you will get a NullPointerException (as you should!)

Its always beneficial to detect programming errors as quickly as possible.

(Inspired from Effective Java Item:5)


Cheers. . . .  ;-)

Friday 5 August 2011

PermGen - OOME(out of memory error) -Tomcat 7








PermGen  - OOME(out of memory error) -Tomcat 7




PermGen (Permananet Generation) space is the place where JVM stores the classes.Classes are stored in the permgen space using class loaders and unique  class identifiers.Each webapp has its own class loader which allows the use of the different version of classes(with same name) in permgen space with out any conflicts.


So,a webapp has its own class loader and a new class loader is created for every reload.The class loaders are garbage collected by the garbage collector when the application stops.But,in case if some reference still exists even after the application is stopped,the class loaders will not be garbage collected,say you are developing a web app where some reference still exists and on every reload or restart of your server,the permgen is heaping up.
The Permgen space is limited and when the classloaders grow up it results in OOME.To,avoid this we must see that no reference to the webapp remains after the app is stopped.




some sceanrios which can cause Permgen space errors are some references to JDBC driver registration,logging frameworks,threads started but not stopped,non removal of objects in thread locals etc.


Apache Tomcat 7.x has come up with many features,Web application memory leak detection and prevention is one of them.JreMemoryLeakPreventionListener is the new life cycle listener class added that calls the various java apis that are known to retain a reference to the current context class loader.If the webapp is the fist to call such apis then there may result in unnecessary existence of references and hence the unrequired class loaders and so the OOME.If the Tomcat calls the java apis first,the memory leaks can be prevented and hence the JreMemoryLeakPreventionListener.This feature also comes with ability to detect the memory leakage occurence,this is added as enhancement to the WebappClassLoader






These seems to be a good enhancement and we should be seeing less PermGen space error with Tomcat 7.x






Cheers... :-)

Thursday 4 August 2011

Java Static Block





I was working on the requirement of extracting the contents from a html page(Which is not that dynamic,in the sense no frequent changes) and format it, display on the front end(jsp in my case).


I opted  HtmlUnit and grepped the contents and transferred them to the front end.Since,the HtmlUnit has to parse the tables,, the page rendering was very slow.


Static block for rescue(since the html is not so dynamic and has no frequent changes),parsed the html and load the contents at the time of class loading.


The loaded data will be served for each request instead of loading data for each request and decreasing the page rendering time and the static block looks as below and it worked like a charm ;-).


So,the elementList in the above code will be populated (which is time consuming) during the class load and happens only once and this would be used in the request serving methods.Load once Serve manier times.




Cheers... Happy Coding ;-)

Monday 1 August 2011

HtmlUnit



HtmlUnit provides an API to invoke web pages,fill the input fields,submit the forms.It is used to simulate the browser actions and is intended to use along with junit or testng .Apart,from testing HtmlUnit can also be used to grep the data from the browser and it supports many calls like getElementsByTagName("****"),getElementById("***"),getInputByName("****") etc.


The following sample code illustrates the use of HtmlUnit to input the fields and submit click action.This sounds similar to selenium,but here there is no browser invocation stuffs, it is  "GUI-Less browser for Java programs" and can capture the content of the browser,can be the data from tables,fields,container divs (similar to js functions nodes,cells,siblings etc).it also provides good support to js and ajax calls.






























The following case uses HtmlUnit along with Junit to test the web browser 





@Test
public void homePage_Firefox() throws Exception {
    final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_2);
    final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
    assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());

    webClient.closeAllWindows();
}


More examples can be found here




cheers... happy coding !! :-)


Table Sorting Jquery plugin

The sorting of the data has become quiet easy using jquery plugin.Here goes the sample code to sort  the table.

Include the jquery and Jquery.tablesorter js files.



any related selector can be used for the container,in the above piece of code DATA_TABLE1 is the id of the table.


Parser[i] is undefined 


Case 1:


I've encountered the above error while trying to sort and the reason for the error was there were some empty
<tr> tags present in the table(since,I was using dynamic creation of the table empty <tr> tags sneaked in and on removal of them it  went on fine)


Case 2:


The same error can happen when trying to sort a empty table and be sure that if you are creating the container(dynamically),the sort is applied once the table is filled up.






Cheers and Happy Coding  :-)