<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5539280185079501154</id><updated>2024-09-26T19:34:12.191-07:00</updated><category term="Hibernate"/><category term="mysql"/><category term="web application"/><category term="Performance"/><category term="Security"/><category term="architecture"/><category term="innodb"/><category term="locking"/><category term="struts2"/><category term="ActiveMQ"/><category term="Apache Camel"/><category term="Code"/><category term="Code Review"/><category term="Heap Error"/><category term="IDE"/><category term="Sonar"/><category term="Troubleshooting"/><category term="asynchronous"/><category term="patterns"/><category term="regular expressions"/><category term="scripting"/><title type='text'>Tech Stuff, Stay Curious</title><subtitle type='html'>stay curious!</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>29</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-6903688555660251591</id><published>2013-01-11T06:44:00.001-08:00</published><updated>2013-01-11T06:44:21.640-08:00</updated><title type='text'>What is base64 encoding ?</title><content type='html'>&lt;br /&gt;
&lt;br /&gt;
Ref : http://stackoverflow.com/questions/201479/what-is-the-use-of-base-64-encoding&lt;br /&gt;
&lt;div class=&quot;post-text&quot;&gt;
&lt;br /&gt;
It&#39;s a textual encoding of binary data where 
the resultant text has nothing but letters, numbers and the symbols &quot;+&quot;,
 &quot;/&quot; and &quot;=&quot;. It&#39;s a convenient way to store/transmit binary data over 
media that is specifically used for textual data.&lt;br /&gt;


But why Base-64? The two alternatives for converting binary data into text that immediately spring to mind are:&lt;br /&gt;


&lt;ol&gt;
&lt;li&gt;Decimal: store the decimal value of each byte as three numbers: 045 
112 101 037 etc. where each byte is represented by 3 bytes. The data 
bloats three-fold.&lt;/li&gt;
&lt;li&gt;Hexadecimal: store the bytes as hex pairs: AC 47 0D 1A etc. where each byte is represented by 2 bytes. The data bloats two-fold.&lt;/li&gt;
&lt;/ol&gt;
Base-64 maps 3 bytes (8 x 3 = 24 bits) in 4 characters that span 
6-bits (6 x 4 = 24 bits). The result looks something like 
&quot;TWFuIGlzIGRpc3Rpb...&quot;. Therefore the bloating is only a mere  4/3 = 
1.3333333 times the original.&lt;br /&gt;

&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/6903688555660251591/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/6903688555660251591' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/6903688555660251591'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/6903688555660251591'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2013/01/what-is-base64-encoding.html' title='What is base64 encoding ?'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-2109905531859339943</id><published>2010-12-30T12:55:00.000-08:00</published><updated>2010-12-30T13:01:06.255-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Code Review"/><category scheme="http://www.blogger.com/atom/ns#" term="Sonar"/><title type='text'>sonar 2.4 and 2.5</title><content type='html'>Excited about Sonar&#39;s new features to set Architectural rules in version 2.4,  and upcoming Track violations over time, differential views, manual code review in 2.5.&lt;br /&gt;&lt;br /&gt;This surely will make my life little easier when doing daily code reviews.&lt;br /&gt;&lt;br /&gt;http://www.sonarsource.org/roadmap/&lt;br /&gt;http://www.infoq.com/news/2010/12/sonar-2.4&lt;br /&gt;&lt;br /&gt;Thank you sonar source</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/2109905531859339943/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/2109905531859339943' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/2109905531859339943'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/2109905531859339943'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2010/12/sonar-24-and-25.html' title='sonar 2.4 and 2.5'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-7365009768087986984</id><published>2010-12-22T07:36:00.000-08:00</published><updated>2010-12-28T10:35:25.721-08:00</updated><title type='text'>Java Autoboxing - Auto-unboxing</title><content type='html'>Java 1.5 onward auto-boxing and auto-unboxing were added. This allows you to covert primitives and boxed types primitives back and forth with simple assignment vs calling special functions or constructors.&lt;br/&gt;&lt;br/&gt;Lets look at some examples.&lt;br/&gt;&lt;pre&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;int primitiveInt = 1000;                   //primitive int&lt;br/&gt;Integer wrapperInt = new Integer(1000);    // wrapper Integer, boxed type primitive.&lt;br/&gt;Integer autoBoxedWrapperInteger = 1000;    // Autoboxing, primitive is autoboxed to Integer, an&lt;br/&gt;                                            //Object is bing created&lt;br/&gt;int autoUnboxedPrimitiveInt = wrapperInt;  //Auto-unboxing, primitive is being assigned a value by auto-unoxing&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;pre&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;//Comparing a primitive to boxed type, boxed type is autoboxed to primitive so the results is&lt;br/&gt;// two primitives are being compared using === as opposed to 2 Objects are being ref compared&lt;br/&gt;// using ==&lt;br/&gt;System.out.println(&quot;primitiveInt == wrapperInt  :&quot; +&lt;br/&gt;            (primitiveInt == wrapperInt ? true : false));&lt;/span&gt;&lt;br/&gt;//Two object references are being compared using ==, therefore the result is false&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;System.out.println(&quot;wrapperInt == autoBoxedWrapperInteger  :&quot; +&lt;br/&gt;            (wrapperInt == autoBoxedWrapperInteger ? true : false));&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;System.out.println(&quot;primitiveInt == autoBoxedWrapperInteger  :&quot; +&lt;br/&gt;           (primitiveInt == autoBoxedWrapperInteger ? true : false));&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;System.out.println(&quot;primitiveInt == autoUnboxedPrimitiveInt  :&quot; +&lt;br/&gt;           (primitiveInt == autoUnboxedPrimitiveInt ? true : false));&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;Output&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;&lt;br/&gt;primitiveInt == wrapperInt  :true&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;wrapperInt == autoBoxedWrapperInteger  :false&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;primitiveInt == autoBoxedWrapperInteger  :true&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;primitiveInt == autoUnboxedPrimitiveInt  :true&lt;/span&gt;&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;Here are some tips based on reading from Effective Java 2nd edition.&lt;br/&gt;&lt;ul&gt;&lt;br/&gt;	&lt;li&gt; Prefer primitives over boxed primitives.&lt;/li&gt;&lt;br/&gt;	&lt;li&gt; When you mix primitives and boxed primitive in single opeartion - boxed-primitive is auto-unboxed.&lt;/li&gt;&lt;br/&gt;	&lt;li&gt; When mixing primitives and wrapper types in expression watch out for &quot;==&quot; comparision.&lt;/li&gt;&lt;br/&gt;	&lt;li&gt; Performance hit due to autoboxing and unboxing, a new object is created when autoboxing is performed.&lt;/li&gt;&lt;br/&gt;	&lt;li&gt; Unboxing can throw NullPointerException&lt;/li&gt;&lt;br/&gt;&lt;/ul&gt;</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/7365009768087986984/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/7365009768087986984' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/7365009768087986984'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/7365009768087986984'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2010/12/java-autoboxing-auto-unboxing.html' title='Java Autoboxing - Auto-unboxing'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-7485689837151992935</id><published>2010-12-15T12:26:00.000-08:00</published><updated>2010-12-28T10:35:25.724-08:00</updated><title type='text'>HTTP Notes</title><content type='html'>&lt;strong&gt;Keep alive/Persistent Connections :&lt;/strong&gt; HTTP 1.1 introduced keep alive to keep the connection between web client and server alive and reuse it to serve subsequent requests. In HTTP 1.0 a new connection had to be created for serving each request.</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/7485689837151992935/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/7485689837151992935' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/7485689837151992935'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/7485689837151992935'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2010/12/http-notes.html' title='HTTP Notes'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-502067087010748440</id><published>2010-12-15T11:58:00.000-08:00</published><updated>2010-12-28T10:35:25.727-08:00</updated><title type='text'>Servlet 3.0 Asynchronous Processing</title><content type='html'>Came across a great post on Servlet 3.0 asynchronous processing at http://www.javaworld.com/javaworld/jw-02-2009/jw-02-servlet3.html?page=1&lt;br/&gt;&lt;br/&gt;Following are some notes based on the post.&lt;br/&gt;&lt;br/&gt;Important thing to note is that it is not a fire and forget mechanism, as in put a request on a queue and return the response right away. The web client  has to wait however long the request processing take place.&lt;br/&gt;&lt;br/&gt;It is a way to free up request handling server threads from long running code. Instead the work in offloaded to be handled by manually created thread(s) by application code.&lt;br/&gt;&lt;br/&gt;Client response stream can be open for extended period of time, and server can write to response to implement &quot;push&quot; model.&lt;br/&gt;&lt;br/&gt;&amp;nbsp;&lt;br/&gt;&lt;br/&gt;&amp;nbsp;&lt;br/&gt;&lt;br/&gt;&amp;nbsp;&lt;br/&gt;&lt;br/&gt;&amp;nbsp;&lt;br/&gt;&lt;br/&gt;&lt;a href=&quot;http://tech20nn.files.wordpress.com/2010/12/asyncservlet33.gif&quot;&gt;&lt;img class=&quot;alignnone size-full wp-image-191&quot; title=&quot;asyncservlet3&quot; src=&quot;http://tech20nn.files.wordpress.com/2010/12/asyncservlet33.gif&quot; alt=&quot;&quot; width=&quot;627&quot; height=&quot;439&quot; /&gt;&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;Following are some of the important methods from ServletRequest API from Servlet 3.0 .&lt;br/&gt;&lt;br/&gt;&lt;code&gt;&lt;a title=&quot;interface in javax.servlet&quot; href=&quot;http://download.oracle.com/javaee/6/api/javax/servlet/AsyncContext.html&quot;&gt;AsyncContext&lt;/a&gt;&lt;/code&gt; &lt;code&gt;&lt;strong&gt;&lt;a href=&quot;http://download.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getAsyncContext%28%29&quot;&gt;getAsyncContext&lt;/a&gt;&lt;/strong&gt;()&lt;/code&gt;&lt;br/&gt;Gets the AsyncContext that was created or reinitialized by the  most recent invocation of &lt;a href=&quot;http://download.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#startAsync%28%29&quot;&gt;&lt;code&gt;startAsync()&lt;/code&gt;&lt;/a&gt; or  &lt;a href=&quot;http://download.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#startAsync%28javax.servlet.ServletRequest,%20javax.servlet.ServletResponse%29&quot;&gt;&lt;code&gt;startAsync(ServletRequest,ServletResponse)&lt;/code&gt;&lt;/a&gt; on this request.&lt;br/&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;boolean     isAsyncStarted() &lt;/span&gt; Checks if this request has been put into asynchronous mode.&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;boolean  isAsyncSupported()&lt;/span&gt; Checks if this request supports asynchronous operation.&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;AsyncContext     startAsync() &lt;/span&gt;Puts this request into asynchronous mode, and initializes its AsyncContext with the original (unwrapped) ServletRequest and ServletResponse objects.&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;AsyncContext     startAsync(ServletRequest servletRequest, ServletResponse servletResponse)&lt;/span&gt;&lt;br/&gt;Puts this request into asynchronous mode, and initializes its AsyncContext with the given request and response objects.&lt;br/&gt;&lt;table border=&quot;1&quot; cellspacing=&quot;0&quot; cellpadding=&quot;3&quot; width=&quot;147&quot;&gt;&lt;br/&gt;&lt;tbody&gt;&lt;br/&gt;&lt;tr bgcolor=&quot;white&quot;&gt;&lt;br/&gt;&lt;td width=&quot;1%&quot; align=&quot;right&quot; valign=&quot;top&quot;&gt;&lt;/td&gt;&lt;br/&gt;&lt;td&gt;&lt;/td&gt;&lt;br/&gt;&lt;/tr&gt;&lt;br/&gt;&lt;tr bgcolor=&quot;white&quot;&gt;&lt;br/&gt;&lt;td width=&quot;1%&quot; align=&quot;right&quot; valign=&quot;top&quot;&gt;&lt;/td&gt;&lt;br/&gt;&lt;td&gt;&lt;/td&gt;&lt;br/&gt;&lt;/tr&gt;&lt;br/&gt;&lt;/tbody&gt;&lt;br/&gt;&lt;/table&gt;</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/502067087010748440/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/502067087010748440' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/502067087010748440'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/502067087010748440'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2010/12/servlet-30-asynchronous-processing.html' title='Servlet 3.0 Asynchronous Processing'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-5260168172072180673</id><published>2010-11-17T08:55:00.000-08:00</published><updated>2010-12-28T10:35:25.730-08:00</updated><title type='text'>HashMap Implementation notes</title><content type='html'>Implements Map interface&lt;br/&gt;&lt;br/&gt;Permits null values and null key&lt;br/&gt;&lt;br/&gt;None of the methods of HashMap are synchronized, so if you are adding or deleting keys - you must synchronize in multi threaded environment. Synchronize on some object, if  external object is not available use Collections.synchronizedMap(new HashMap())&lt;br/&gt;&lt;br/&gt;Collections.synchronizedMap(new HashMap()) synchronizes all map operations using a internal mutex. However synchronization every single method is not enough, some client side locking is also required.&lt;br/&gt;&lt;br/&gt;Iterators of HashMap are fail-fast when underlying map is modified (add/delete key) - ConcurrentModificationException is thrown.&lt;br/&gt;&lt;br/&gt;Ordering of keys is not guaranteed to be same over time.&lt;br/&gt;&lt;br/&gt;Access for get and put is time constant.&lt;br/&gt;&lt;br/&gt;Max Capacity ~ 1.07 billiion&lt;br/&gt;&lt;br/&gt;Default Initial capacity = 16&lt;br/&gt;&lt;br/&gt;Uses hash function to disperse keys around.--&lt;br/&gt;&lt;br/&gt;Uses Array to store elements.&lt;br/&gt;&lt;br/&gt;Try and provide capacity  as part of initialization.&lt;br/&gt;&lt;br/&gt;&amp;nbsp;&lt;br/&gt;&lt;br/&gt;&amp;nbsp;</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/5260168172072180673/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/5260168172072180673' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/5260168172072180673'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/5260168172072180673'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2010/11/hashmap-implementation-notes.html' title='HashMap Implementation notes'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-5515107117545201646</id><published>2010-03-29T18:35:00.000-07:00</published><updated>2010-12-30T13:03:29.425-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ActiveMQ"/><category scheme="http://www.blogger.com/atom/ns#" term="Apache Camel"/><category scheme="http://www.blogger.com/atom/ns#" term="architecture"/><category scheme="http://www.blogger.com/atom/ns#" term="asynchronous"/><category scheme="http://www.blogger.com/atom/ns#" term="web application"/><title type='text'>Executing Asynchronous Actions in a Web Application</title><content type='html'>Web applications need to provide instant response to end users, however not all requests can be served instantly due to a variety of constraints on infrastructure the application must live with. In an enterprise web applications there could a variety of long running tasks initiated by user request. To handle such task, the request could be queued for later processing and user is presented with a message to check status at a later time.  For some requests a part of necessary processing could be done instantly and remaining could be done at a later time.&lt;br/&gt;&lt;br/&gt;Queuing a request requires adding extra infrastructure to web applications. Request could be queued in a single instance of share data structure across the application. Application threads running in background can read the queue data structure and act on queue messages. Rather than writing this framework/infrastructure enterprise class open source libraries could be utilized.  ActiveMQ provides a robust queuing solution and Apache Camel provides processing queued actions. Both libraries can be used in embedded or standalone fashion.&lt;br/&gt;&lt;br/&gt;Architecture:&lt;br/&gt;&lt;br/&gt;ServletContextListener initialize the ActiveMQ Broker and CamelContext, queues and routes are setup.&lt;br/&gt;&lt;br/&gt;Have web action/controller write to ActiveMQ queue.&lt;br/&gt;&lt;br/&gt;Write classes/camel routes to read from queue .</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/5515107117545201646/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/5515107117545201646' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/5515107117545201646'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/5515107117545201646'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2010/03/executing-asynchronous-actions-in-web.html' title='Executing Asynchronous Actions in a Web Application'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-1151060853280572487</id><published>2010-02-19T20:05:00.000-08:00</published><updated>2010-12-30T13:03:43.707-08:00</updated><title type='text'>uuid</title><content type='html'>UUID - Universally unique id is a 128 bit is a reasonably unique value. UUID was standarized by Open Software Foundation and Distributed Computing Environment. Purpose of UUID is to generate a universally unique identifier without contacting a central identifier generation system.&lt;br/&gt;&lt;br/&gt;UUID are relavant to distributed computing, each machine can have 10 million UUID per second as per &lt;a title=&quot;IEFT RFC4122&quot; href=&quot;http://tools.ietf.org/html/rfc4122&quot; target=&quot;_blank&quot;&gt;IETF REFC 4122&lt;/a&gt; . Implementation for generating UUID are widely available in various programming language libraries and in DBMS.&lt;br/&gt;&lt;br/&gt;JDK 1.5 provides classes to generate UUID.&lt;br/&gt;&lt;br/&gt;&lt;code&gt;Oracle SYS_GUID() , MySQL &lt;/code&gt;UUID() , &lt;code&gt;SQL Server &lt;/code&gt;NEWID()</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/1151060853280572487/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/1151060853280572487' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/1151060853280572487'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/1151060853280572487'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2010/02/uuid.html' title='uuid'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-8904263822156498773</id><published>2010-02-19T17:36:00.000-08:00</published><updated>2010-12-28T10:35:38.250-08:00</updated><title type='text'>Apache Camel File Polling</title><content type='html'>Just</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/8904263822156498773/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/8904263822156498773' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/8904263822156498773'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/8904263822156498773'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2010/02/apache-camel-file-polling.html' title='Apache Camel File Polling'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-3177511246817516289</id><published>2010-01-20T06:20:00.000-08:00</published><updated>2010-12-28T10:35:25.736-08:00</updated><title type='text'>Open source CSV reader</title><content type='html'>A useful library can be used to read a CSV file or simply parse a CSV string.  A huge time saver.&lt;br/&gt;&lt;br/&gt;http://opencsv.sourceforge.net/project-summary.html&lt;br/&gt;&lt;pre&gt;Maven Dependency&lt;br/&gt;GroupId: net.sf.opencsv&lt;br/&gt;ArtifactId: opencsv&lt;br/&gt;Version: 2.1&lt;br/&gt;Type: jar&lt;br/&gt;&lt;br/&gt;Code sample, input could be &lt;br/&gt;&lt;br/&gt;CSVParser parser = new CSVParser();&lt;br/&gt;String values[] = parser.parseLine(lineText);                    &lt;br/&gt;&lt;br/&gt;Sample input&lt;br/&gt;This, &quot;is&quot;, a. &quot;huge time save utility. Handles this comma , nicely &quot;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/3177511246817516289/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/3177511246817516289' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/3177511246817516289'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/3177511246817516289'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2010/01/open-source-csv-reader.html' title='Open source CSV reader'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-5414432719408867981</id><published>2010-01-18T11:22:00.000-08:00</published><updated>2010-12-28T10:35:25.738-08:00</updated><title type='text'>Contract First Vs Code First</title><content type='html'>Contact-first is one of design principles for achieving SOA. I came across an informative discussion on Contract First Vs Code First web service&lt;br/&gt;&lt;br/&gt;http://tssblog.blogs.techtarget.com/2007/04/11/contract-first-or-code-first-design-part-1/&lt;br/&gt;&lt;br/&gt;Discussion is structured around following 4 arguments for using Contract first&lt;br/&gt;&lt;ul&gt;&lt;br/&gt;	&lt;li&gt;Code-first interfaces may not be well suite for the demands of remote traffic.&lt;/li&gt;&lt;br/&gt;	&lt;li&gt;Implementation code typically changes at a faster rate than the interface.&lt;/li&gt;&lt;br/&gt;	&lt;li&gt;Generated WSDL leads to interface bloat and duplicated and incompatible type definition.&lt;/li&gt;&lt;br/&gt;	&lt;li&gt;Code-first doesn’t work in a multi-lingual development organization.&lt;/li&gt;&lt;br/&gt;&lt;/ul&gt;&lt;br/&gt;After being enriched by this discussion, here is my take on the these points&lt;br/&gt;&lt;br/&gt;&lt;strong&gt;Code-first interfaces many not be well suited for demands of remote traffic.&lt;/strong&gt;&lt;br/&gt;&lt;br/&gt;Web Service interface should not contain chatty methods. Number of  method calls invoked during conversation needs to be limited for performance reasons. It does not mater whether you use Contract-first or Code-first approach, as a web service interface designer you need to keep number of method calls to minimum. In code first  approach WSDL is  auto generated, however web service interface is your decision. On the Contract-first side as well you could end up with chatty web service, it is you who has to design the WSDL.&lt;br/&gt;&lt;br/&gt;&lt;strong&gt;Implementation code typically changes at a faster rate than the interface.&lt;/strong&gt;&lt;br/&gt;&lt;br/&gt;If interface is remaining same and implementation is changing, one approach is not necessarily better than other. Changes could made to back-end without changing the interface.  Implementer of Code-first needs to be careful when changing the implementation not to inadvertently affect the generated WSDL by exposing extra methods.&lt;br/&gt;&lt;br/&gt;If interface is varying, then the comparison comes into picture what does it involve&lt;br/&gt;&lt;br/&gt;In Contract-first approach, interface changes requires collaboration on interface , data type change amongst  development personal from multiple applications.  Possibilities of  contention are lot more when you change schema type definitions common to multiple application. Multiple applications have to conform to same Schema type definition, which in itself could be limiting due to tight coupling.  Let say CRM application requires changes to representation of Address/Contact, now rest of the applications that have Address/Contact domain model associated with common Address/Contact schema type  has to be change and redeployed whether they want CRM address/contact  change to be reflected in their application or not. On the flip side if we were to use code first approach only the applications that have something to do with CRM Address/Contact have to be changed. Change in Contract-first is much more involved and impact is lot larger than code-first approach. Down side of code first approach is that the representation are different across the applications and mapping code for common fields in Address/Contact need to be maintained.  As times goes by, Address/Contact representations across the application will grow further apart due to number of changes over the years. It is will get harder to maintain the mappings code.&lt;br/&gt;&lt;br/&gt;I would go with code-first approach in terms of time/efforts/resources required to make a change.  Changes to mapping code/new representation could still be communicated to other application groups and put on their to-do list, as opposed to expecting them to drop everything and comply with super urgent change for your application.&lt;br/&gt;&lt;br/&gt;If you can pull off the alignment of development resources and the collaboration needed to do Contract-first approach, contract-first approach is better and will keep data type uniform across the systems.  Can you do it over a prolonged period ?&lt;br/&gt;&lt;br/&gt;&lt;strong&gt;Generated WSDL leads to interface bloat and duplicated and incompatible type definition.&lt;/strong&gt;&lt;br/&gt;&lt;br/&gt;This point again depends on skills of code-first interface designer, a well designed interface won&#39;t generate bloated WSDL. Duplicate and Incompatible data type goes back to 2nd argument discussed earlier. The time and resources required to collaborate over common data type/data type reuse will be lot more over the years.&lt;br/&gt;&lt;br/&gt;&lt;strong&gt;Code-first doesn’t work in a multi-lingual development organization.&lt;/strong&gt;&lt;br/&gt;&lt;br/&gt;If the language provide auto-generation tools, why not.</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/5414432719408867981/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/5414432719408867981' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/5414432719408867981'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/5414432719408867981'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2010/01/contract-first-vs-code-first.html' title='Contract First Vs Code First'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-2849727439722676882</id><published>2010-01-15T11:53:00.000-08:00</published><updated>2010-12-28T10:35:25.742-08:00</updated><title type='text'>JAXB</title><content type='html'>Data representation is a core component of communication between enterprise applications. There are a number of formats for data exchange - XML, CSV, JSON, proprietary to name a few. XML is a common format when working with web services and is also fundamental to SOA. Working with XML in Java applications often requires transformation between Java objects and XML documents. There are many &lt;a href=&quot;http://java-source.net/open-source/xml-parsers&quot;&gt;open source tools&lt;/a&gt; (JAXB,  XMLBeans, Xstreme, JiBX, Zeus, etc.)  to achieve this transformation .  This post explores JAXB. JAXB API is part of J2SE 1.6,&lt;br/&gt;&lt;br/&gt;&lt;span style=&quot;font-weight:bold;&quot;&gt; Convert XML Document into Java Objects (Unmarshal)&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;Converting a XML document into Java objects is a common use case. XML document that is being transformed to Java objects is required to have a XML Schema to produce bindings.  Following steps will achieve the transformation .  As you go through these steps you will notice use of auto generated classes to get XML data, you do not have to write XML parsing code using DOM or SAX.  In some cases your application may already have predefined domain classes which closely match the XML schema representation or somewhat match XML representation and you may not not like using of auto generated classes,  for these cases there are ways to minimize use of auto generated classes, using some mapping libraries or using JAXB Customizations.&lt;br/&gt;&lt;ul&gt;&lt;br/&gt;	&lt;li&gt;Step 1:  At compile time, use XML Schema associated with XML document and JAXB tool auto generate Java classes, this is called binding (Tool lets you specify a package name and generates java Interface and Implementation source file and an ObjectFactory class which provides methods to create generated types.&lt;/li&gt;&lt;br/&gt;&lt;/ul&gt;&lt;br/&gt;&lt;p style=&quot;padding-left:30px;&quot;&gt;Use following command..(for generating code using Maven scroll down)&lt;/p&gt;&lt;br/&gt;&lt;p style=&quot;padding-left:30px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; jdk\bin\xjc.exe -p com.mypkgname  appscheama.xsd&lt;/span&gt;&lt;/p&gt;&lt;br/&gt;&lt;p style=&quot;padding-left:30px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &lt;strong&gt; &lt;span style=&quot;color:#000000;&quot;&gt;INPUT Schema&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;br/&gt;&lt;p style=&quot;padding-left:30px;&quot;&gt;&lt;/p&gt;&lt;br/&gt;&lt;p style=&quot;padding-left:30px;&quot;&gt;I have used following XML Schema to model NFL scores&lt;/p&gt;&lt;br/&gt;&lt;br/&gt;&lt;pre style=&quot;padding-left:90px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&amp;gt;&lt;br/&gt;xmlns=&quot;http://www.w3.org/2001/XMLSchema&quot; targetNamespace=&quot;http://www.example.org/SportsSchema&quot;&lt;br/&gt;xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:nfl=&quot;http://www.example.org/SportsSchema&quot;&lt;br/&gt;elementFormDefault=&quot;qualified&quot;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;pre style=&quot;padding-left:90px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;&amp;lt;xs:complexType name=&quot;TeamType&quot;&amp;gt;&lt;br/&gt;     &amp;lt;xs:attribute name=&quot;name&quot; type=&quot;xs:string&quot;/&amp;gt;&lt;br/&gt;     &amp;lt;xs:attribute name=&quot;location&quot; type=&quot;xs:string&quot; /&amp;gt;&lt;br/&gt;&amp;lt;!--&lt;span class=&quot;hiddenSpellError&quot;&gt;xs:complexType&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;pre style=&quot;padding-left:90px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;&amp;lt;xs:complexType name=&quot;FootballScoreType&quot;&amp;gt;&lt;br/&gt;   &amp;lt;xs:sequence&amp;gt;&lt;br/&gt;      &amp;lt;xs:element name=&quot;firstTeam&quot; type=&quot;nfl:TeamType&quot; /&amp;gt;&lt;br/&gt;      &amp;lt;xs:element name=&quot;secondTeam&quot; type=&quot;nfl:TeamType&quot; /&amp;gt;&lt;br/&gt;      &amp;lt;xs:element name=&quot;firstTeamPoints&quot; type=&quot;xs:int&quot;  /&amp;gt;&lt;br/&gt;      &amp;lt;xs:element name=&quot;secondTeamPoints&quot; type=&quot;xs:int&quot; /&amp;gt;&lt;br/&gt;   &amp;lt;!--&lt;span class=&quot;hiddenSpellError&quot;&gt;xs:sequence&lt;br/&gt;&amp;lt;!--&lt;span class=&quot;hiddenSpellError&quot;&gt;xs:complexType&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;pre style=&quot;padding-left:90px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;&amp;lt;xs:element name=&quot;scores&quot;&amp;gt;&lt;br/&gt;   &amp;lt;xs:complexType&amp;gt;&lt;br/&gt;     &amp;lt;xs:sequence&amp;gt;&lt;br/&gt;         &amp;lt;xs:element name=&quot;score&quot; type=&quot;nfl:FootballScoreType&quot; maxOccurs=&quot;unbounded&quot; /&amp;gt;&lt;br/&gt;    &amp;lt;!--&lt;span class=&quot;hiddenSpellError&quot;&gt;xs:sequence&amp;gt;&lt;br/&gt;   &amp;lt;!--&lt;span class=&quot;hiddenSpellError&quot;&gt;xs:complexType&amp;gt;&lt;br/&gt;&amp;lt;!--&lt;span class=&quot;hiddenSpellError&quot;&gt;xs:element&amp;gt;&lt;br/&gt;&amp;lt;/schema&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;p style=&quot;padding-left:30px;&quot;&gt;&lt;span style=&quot;color:#000000;&quot;&gt;&lt;strong&gt;AUTO GENERATED CODE&lt;/strong&gt; &lt;/span&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &lt;/span&gt;&lt;/p&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &lt;/span&gt;&lt;br/&gt;&lt;p style=&quot;padding-left:30px;&quot;&gt;Upon running the xjc tool,  various Java classes representing XML schema are generated. Following is an example of generated for one of the element in schema. Notice the annotations on Java classes, this what make the binding work. Generated code also a ObjectFactory which is used to create instances of various auto generated types.&lt;/p&gt;&lt;br/&gt;&lt;br/&gt;&lt;pre style=&quot;padding-left:60px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;//&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;pre style=&quot;padding-left:60px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0.2-b01-fcs &lt;/span&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;&lt;br/&gt;// See &lt;a href=&quot;http://java.sun.com/&amp;lt;span class=&quot;&gt;xml&lt;/a&gt;&lt;a href=&quot;http://java.sun.com/&amp;lt;span class=&quot;&gt;/jaxb&quot;&amp;gt;http://java.sun.com/xml/jaxb&lt;/a&gt; &lt;/span&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;&lt;br/&gt;// Any modifications to this file will be lost upon recompilation of the source schema. &lt;/span&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;&lt;br/&gt;// Generated on: 2010.01.08 at 02:37:52 PM EST&lt;br/&gt;//&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;pre style=&quot;padding-left:60px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;package com.spag.dataxchange.generated;&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;pre style=&quot;padding-left:60px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;import java.math.BigInteger;&lt;br/&gt;import javax.xml.bind.annotation.XmlAccessType;&lt;br/&gt;import javax.xml.bind.annotation.XmlAccessorType;&lt;br/&gt;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlType;&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;pre style=&quot;padding-left:60px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;/**&lt;br/&gt;&lt;/span&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;*&lt;br/&gt;Java class for FootballScoreType complex type. *&lt;br/&gt;&lt;/span&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;&lt;br/&gt;* &amp;lt;p&amp;gt;The following schema fragment specifies the expected content contained within this class.&lt;br/&gt;*/ &lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;pre&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;        @XmlAccessorType(XmlAccessType.FIELD)&lt;/span&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;&lt;br/&gt;        @XmlType(name = &quot;FootballScoreType&quot;, propOrder = {&lt;/span&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &quot;firstTeam&quot;,&lt;/span&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &quot;secondTeam&quot;,&lt;/span&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &quot;firstTeamPoints&quot;,&lt;/span&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &quot;secondTeamPoints&quot;&lt;/span&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;})&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;pre style=&quot;padding-left:60px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;public class FootballScoreType {&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;pre style=&quot;padding-left:60px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; @XmlElement(required = true)&lt;br/&gt; protected TeamType firstTeam;&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;pre style=&quot;padding-left:60px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; @XmlElement(required = true)&lt;br/&gt; protected TeamType secondTeam;&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;pre style=&quot;padding-left:60px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; @XmlElement(required = true)&lt;br/&gt; protected BigInteger firstTeamPoints;&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;pre style=&quot;padding-left:60px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; @XmlElement(required = true)&lt;br/&gt; protected BigInteger secondTeamPoints;&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;pre style=&quot;padding-left:60px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; public TeamType getFirstTeam() {return firstTeam;}&lt;br/&gt; public void setFirstTeam(TeamType value) {this.firstTeam = value;}&lt;/span&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;&lt;br/&gt; public TeamType getSecondTeam() {return secondTeam;}&lt;br/&gt; public void setSecondTeam(TeamType value) {this.secondTeam = value;}&lt;br/&gt; public BigInteger getFirstTeamPoints() { return firstTeamPoints; }&lt;br/&gt; public void setFirstTeamPoints(BigInteger value) {this.firstTeamPoints = value;}&lt;br/&gt; public BigInteger getSecondTeamPoints() {return secondTeamPoints;}&lt;br/&gt; public void setSecondTeamPoints(BigInteger value) {this.secondTeamPoints = value;}&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;pre style=&quot;padding-left:60px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;&lt;br/&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;Step 2: Now that we jave java XML bindings in place. We can use these classes to convert XML document into Java objects, aka unmarshaling.  Unmarshaling is performed using JAXB API and auto generated classes from step 1. Following is sample code.&lt;br/&gt;&lt;div style=&quot;padding-left:90px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;package com.spag.dataxchange;&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;import java.io.File;&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;import java.util.Iterator;&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;import java.util.List;&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;import javax.xml.bind.JAXBContext;&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;import javax.xml.bind.JAXBException;&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;import javax.xml.bind.Marshaller;&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;import javax.xml.bind.Unmarshaller;&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;import javax.xml.bind.Validator;&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;import com.spag.dataxchange.generated.*;&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;&lt;br/&gt;public class ScoreReader {&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;    public static void main(String [] args) {&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;       try {&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;           JAXBContext jaxbCtx =  JAXBContext.newInstance(&quot;com.spag.dataxchange.generated&quot;) ;&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;           Unmarshaller reader = jaxbCtx.createUnmarshaller();&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;           Scores obj = (Scores)reader.unmarshal(new File(&quot;src/main/resources/data/scores.xml&quot;));&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;           List list = obj.getScore();&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;           Iterator iter = list.iterator();&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;           int i = 0;&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;           while(iter.hasNext()) {&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;                i++;&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;                FootballScoreType score = iter.next();&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;                System.out.println(&quot;Game &quot; + i );&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;                System.out.println( score.getFirstTeam().getLocation()  + &quot; - &quot;  + score.getFirstTeam().getName() + &quot;: &quot; +                 score.getFirstTeamPoints());&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;                System.out.println( score.getSecondTeam().getLocation()  + &quot; - &quot;  + score.getSecondTeam().getName() + &quot;: &quot; +      score.getSecondTeamPoints());&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;           }          &lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;        } catch (JAXBException e) {&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;           e.printStackTrace();&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;        }&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;    }&lt;/span&gt;&lt;/code&gt;&lt;br/&gt;&lt;code&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br/&gt;&lt;/div&gt;&lt;br/&gt;&lt;div style=&quot;padding-left:30px;&quot;&gt;Consider the scenario where we already have domain classes defined, the challenge here is to either map the auto generated to classes to domain classes or to eliminate use of auto generated classes and go straight to domain classes. One way to eliminate/reduce use of auto generated classes  is to use JAXB Customization.  JAXB Customization tags lets you map XML schema to classes of your liking. Customizations can be specified inline with XML Schema code, however in most cases you won&#39;t be entitled to modifying the schema of XML document that being published by other application, that too referencing domain classes specific to your application.  Thanks goodness customizations can be specified in external binding file (binding.xjb).&lt;/div&gt;&lt;br/&gt;&lt;div style=&quot;padding-left:30px;&quot;&gt;bindings.xjb allows you to map existing classes to domain classes, when invoking the xjc comipler specify the binding file that is being used. Binding customizations can be done at various xml schema element to class mapping,&lt;/div&gt;&lt;br/&gt;&lt;pre style=&quot;padding-left:30px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;&amp;lt;jxb:bindings version=&quot;2.0&quot; xmlns:jxb=&quot;http://java.sun.com/xml/ns/jaxb&quot;&lt;br/&gt; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;&amp;gt;&lt;br/&gt;   &amp;lt;jxb:bindings schemaLocation=&quot;GolfSchema.xsd&quot; node=&quot;/xs:schema&quot;  &amp;gt;&lt;br/&gt;        &amp;lt;jxb:bindings node=&quot;//xs:complexType[@name=&#39;golfRound&#39;]&quot;&amp;gt;&lt;br/&gt;               &amp;lt;jxb:class ref=&quot;com.spag.dataxchange.golf.GolfRound&quot;/&amp;gt;&lt;br/&gt;        &amp;lt;!--&lt;span class=&quot;hiddenSpellError&quot;&gt;jxb:bindings&amp;gt;&lt;br/&gt;        &amp;lt;jxb:bindings node=&quot;//xs:complexType[@name=&#39;golfer&#39;]&quot;&amp;gt;&lt;br/&gt;              &amp;lt;jxb:class ref=&quot;com.spag.dataxchange.golf.Golfer&quot;/&amp;gt;&lt;br/&gt;        &amp;lt;!--&lt;span class=&quot;hiddenSpellError&quot;&gt;jxb:bindings&amp;gt;&lt;br/&gt;   &amp;lt;!--&lt;span class=&quot;hiddenSpellError&quot;&gt;jxb:bindings&amp;gt;&lt;br/&gt; &amp;lt;!--&lt;span class=&quot;hiddenSpellError&quot;&gt;jxb:bindings&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;div style=&quot;padding-left:30px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;&lt;span style=&quot;color:#000000;&quot;&gt;Binding file could get unwieldy, there are &lt;/span&gt;o&lt;/span&gt;ther options to  map auto generated class objects to domain class objects. Dozer is such a library that provides mapping from one JavaBean to other. This library could be utilized to convert between auto generated JAXB classes and your domain model.&lt;/div&gt;&lt;br/&gt;&lt;div style=&quot;padding-left:30px;&quot;&gt;Maven configuration for working with JAXB, including use of mapping files.&lt;/div&gt;&lt;br/&gt;&lt;p style=&quot;padding-left:120px;&quot;&gt;&lt;/p&gt;&lt;br/&gt;&lt;br/&gt;&lt;pre style=&quot;padding-left:90px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;&amp;lt;dependency&amp;gt;&lt;br/&gt; &amp;lt;groupId&amp;gt;javax.xml.bind&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &amp;lt;artifactId&amp;gt;jaxb-api&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &amp;lt;version&amp;gt;2.2&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &amp;lt;/dependency&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &amp;lt;plugin&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;     &amp;lt;groupId&amp;gt;org.codehaus.mojo&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;     &amp;lt;artifactId&amp;gt;jaxb2-maven-plugin&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;     &amp;lt;version&amp;gt;1.3&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;     &amp;lt;executions&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;         &amp;lt;execution&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;             &amp;lt;id&amp;gt;1&amp;lt;/id&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;             &amp;lt;goals&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;                &amp;lt;goal&amp;gt;xjc&amp;lt;/goal&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;            &amp;lt;/goals&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;           &amp;lt;configuration&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;                  &amp;lt;outputDirectory&amp;gt;${project.build.directory}/generated-sources&amp;lt;/outputDirectory&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;                   src/main/resources/schema/nfl&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;                  &amp;lt;schemaFiles&amp;gt;SportsSchema.xsd&amp;lt;/schemaFiles&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;                  &amp;lt;packageName&amp;gt;com.spag.dataxchange.generated&amp;lt;/packageName&amp;gt; &amp;lt;!-- The name of your generated source package --&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;           &amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;        &amp;lt;/execution&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;        &amp;lt;execution&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;           &amp;lt;id&amp;gt;2&amp;lt;/id&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;           &amp;lt;goals&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;             &amp;lt;goal&amp;gt;xjc&amp;lt;/goal&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;           &amp;lt;/goals&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;           &amp;lt;configuration&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;               &amp;lt;clearOutputDir&amp;gt;false&amp;lt;/clearOutputDir&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;               &amp;lt;outputDirectory&amp;gt;${project.build.directory}/generated-sources&amp;lt;/outputDirectory&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;                src/main/resources/schema/golf&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;               &amp;lt;schemaFiles&amp;gt;GolfSchema.xsd&amp;lt;/schemaFiles&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;               &amp;lt;packageName&amp;gt;com.spag.dataxchange.golf&amp;lt;/packageName&amp;gt; &amp;lt;!-- The name of your generated source package --&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;              src/main/resources/schema/golf&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;              &amp;lt;bindingFiles&amp;gt;bindings.xjb&amp;lt;/bindingFiles&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;           &amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;      &amp;lt;/execution&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &amp;lt;/executions&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &amp;lt;/plugin&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &lt;/span&gt;&lt;br/&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;&lt;br/&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;span style=&quot;font-weight:bold;&quot;&gt;Convert Java Objects into XML document (Marshal)&lt;/span&gt;&lt;/p&gt;&lt;br/&gt;To convert  Java object into XML document,  JAXB has schemagen tool which lets you create a Schema from your domain classes. Using Objects instances, generated XML schema and JAXB API you could generate the XML document easily. Here are some code snippets and maven configurations to ease your development.&lt;br/&gt;&lt;pre style=&quot;padding-left:90px;&quot;&gt;&lt;span style=&quot;color:#0000ff;&quot;&gt;&amp;lt;plugin&amp;gt;&lt;br/&gt; &amp;lt;groupId&amp;gt;org.codehaus.mojo&amp;lt;/groupId&amp;gt;&lt;br/&gt; &amp;lt;artifactId&amp;gt;jaxb2-maven-plugin&amp;lt;/artifactId&amp;gt;&lt;br/&gt; &amp;lt;version&amp;gt;1.3&amp;lt;/version&amp;gt;&lt;br/&gt; &amp;lt;executions&amp;gt;&lt;br/&gt; &amp;lt;execution&amp;gt;&lt;br/&gt; &amp;lt;goals&amp;gt;&lt;br/&gt; &amp;lt;goal&amp;gt;schemagen&amp;lt;/goal&amp;gt;&lt;br/&gt; &amp;lt;/goals&amp;gt;&lt;br/&gt; &amp;lt;/execution&amp;gt;&lt;br/&gt; &amp;lt;/executions&amp;gt;&lt;br/&gt; &amp;lt;configuration&amp;gt;&lt;br/&gt; &amp;lt;outputDirectory&amp;gt;${project.build.directory}/generated-schema&amp;lt;/outputDirectory&amp;gt;&lt;br/&gt; &amp;lt;includes&amp;gt;&lt;br/&gt; &amp;lt;include&amp;gt;com/spag/dataxchange/golf/*.java&amp;lt;/include&amp;gt;&lt;br/&gt; &amp;lt;/includes&amp;gt; &amp;lt;!-- The name of your generated source package --&amp;gt;&lt;br/&gt; &amp;lt;/configuration&amp;gt;&lt;br/&gt; &amp;lt;/plugin&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br/&gt;&lt;strong&gt;Further Pondering&lt;/strong&gt;&lt;br/&gt;&lt;br/&gt;JAXB is primarily used in webservices developed using existing Java classes.&lt;br/&gt;&lt;br/&gt;What happens when the the schema changes / domain model changes ? &lt;strong&gt;&lt;br/&gt;&lt;/strong&gt;&lt;br/&gt;&lt;br/&gt;Explore of JAXB annotations.&lt;br/&gt;&lt;br/&gt;Java to XML(WSDL)nges ?&lt;br/&gt;&lt;br/&gt;Ref: https://jaxb.dev.java.net/tutorial/&lt;br/&gt;&lt;br/&gt;http://blogs.sun.com/CoreJavaTechTips/entry/exchanging_data_with_xml_and1</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/2849727439722676882/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/2849727439722676882' title='58 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/2849727439722676882'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/2849727439722676882'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2010/01/jaxb.html' title='JAXB'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>58</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-6632799772249465570</id><published>2009-10-14T06:32:00.001-07:00</published><updated>2010-12-30T13:03:56.873-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Hibernate"/><title type='text'>Keep DAO clean using Hibernate Criteria /Criterion</title><content type='html'>Hibernate Criteria/Criterion classes helps reduce the number of lines in DAO code. Have private methods returning the specific Criterion and in public DAO method compose complex criteria by calling private methods. &lt;br /&gt;&lt;br /&gt;public List getSprints(Date afterDate, Date beforeDate) {&lt;br /&gt;   Criteria criteria =  getSession().createCriteria(MyEntity.class);&lt;br /&gt;   criteria.add(getDateCriteria(afterDate, beforeDate));&lt;br /&gt;   return criteria.list();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public List getEntityByType(String type, Date afterDate, Date beforeDate) {&lt;br /&gt;    Criteria criteria  =  getSession().createCriteria(MyEntity.class);&lt;br /&gt; criteria.add(getDateCriteria(afterDate, beforeDate));  &lt;br /&gt; criteria.add(Restrictions.eq(&quot;type&quot;, type));&lt;br /&gt; return criteria.list();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private Criterion getDateCriteria(Date afterDate, Date beforeDate) {&lt;br /&gt;      if(beforeDate == null) { // Is parameter provided ?&lt;br /&gt;  beforeDate = new Date();&lt;br /&gt; }&lt;br /&gt; if(afterDate == null) {   // Is parameter provided&lt;br /&gt;  Calendar cal = Calendar.getInstance();&lt;br /&gt;  cal.add(Calendar.YEAR, -1);&lt;br /&gt;  afterDate = cal.getTime();&lt;br /&gt; }&lt;br /&gt;  &lt;br /&gt; Criterion start = Restrictions.between(&quot;startDate&quot;, beforeDate, afterDate);&lt;br /&gt; Criterion end = Restrictions.between(&quot;endDate&quot;, beforeDate, afterDate);&lt;br /&gt; return Restrictions.or(start, end);&lt;br /&gt;}</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/6632799772249465570/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/6632799772249465570' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/6632799772249465570'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/6632799772249465570'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2009/10/keep-dao-clean-using-hibernate-criteria.html' title='Keep DAO clean using Hibernate Criteria /Criterion'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-7464589222631193221</id><published>2009-10-14T06:32:00.000-07:00</published><updated>2009-10-14T07:02:42.760-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Hibernate"/><title type='text'>Keep DAO clean using Hibernate Criteria /Criterion</title><content type='html'>Hibernate Criteria/Criterion classes helps reduce the number of lines in DAO code. Have private methods returning the specific Criterion and in public DAO method compose complex criteria by calling private methods. &lt;br /&gt;&lt;br /&gt;public List&lt;MyEntity&gt; getSprints(Date afterDate, Date beforeDate) {&lt;br /&gt;   Criteria criteria =  getSession().createCriteria(MyEntity.class);&lt;br /&gt;   criteria.add(getDateCriteria(afterDate, beforeDate));&lt;br /&gt;   return criteria.list();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public List&lt;MyEntity&gt; getEntityByType(String type, Date afterDate, Date beforeDate) {&lt;br /&gt;    Criteria criteria  =  getSession().createCriteria(MyEntity.class);&lt;br /&gt; criteria.add(getDateCriteria(afterDate, beforeDate));  &lt;br /&gt; criteria.add(Restrictions.eq(&quot;type&quot;, type));&lt;br /&gt; return criteria.list();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private Criterion getDateCriteria(Date afterDate, Date beforeDate) {&lt;br /&gt;      if(beforeDate == null) { // Is parameter provided ?&lt;br /&gt;  beforeDate = new Date();&lt;br /&gt; }&lt;br /&gt; if(afterDate == null) {   // Is parameter provided&lt;br /&gt;  Calendar cal = Calendar.getInstance();&lt;br /&gt;  cal.add(Calendar.YEAR, -1);&lt;br /&gt;  afterDate = cal.getTime();&lt;br /&gt; }&lt;br /&gt;  &lt;br /&gt; Criterion start = Restrictions.between(&quot;startDate&quot;, beforeDate, afterDate);&lt;br /&gt; Criterion end = Restrictions.between(&quot;endDate&quot;, beforeDate, afterDate);&lt;br /&gt; return Restrictions.or(start, end);&lt;br /&gt;}</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/7464589222631193221/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/7464589222631193221' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/7464589222631193221'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/7464589222631193221'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2009/10/keeping-dao-clean.html' title='Keep DAO clean using Hibernate Criteria /Criterion'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-5491145225116515465</id><published>2009-07-18T09:41:00.001-07:00</published><updated>2010-12-30T13:03:56.875-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Security"/><category scheme="http://www.blogger.com/atom/ns#" term="struts2"/><category scheme="http://www.blogger.com/atom/ns#" term="web application"/><title type='text'>Web App + Security + Struts2</title><content type='html'>When securing a web application there are few options for authentication and authorization of user/roles. You could write your custom logic,  use container managed security, or use 3rd part libraries, or augment container managed security with custom logic.&lt;br /&gt;&lt;br /&gt;Custom Security: Custom security is implemented by writing programming logic. For a pure Struts2 app (Where every url of the web application is served by Struts actions), typically user is authenticated using a custom HTML form backed by a custom LoginAction. LoginAction will validated username/password and then store the user information in session variable.  This user/role information stored in  session can be used control the logic for what URL/actions can be accessed by user. You can write a Struts2 interceptor and have every single request for struts2 resource go through this interceptor. When user tries to access a resource in the application, this interceptor will check if user is not logged by looking at certain data in user session. If data is not found user can redirected to login action. If user is logged in then you can check the role to make sure user has access to the resource, once the action is executed, you can use the user role information in view code (JSPs or other ) to hide/show certain section of view.&lt;br /&gt;&lt;br /&gt;Disadvantage of above approach is is that only struts actions are protected.  Another approach will be to write a Servlet filter instead of a struts Interceptor. Other disadvantage is you will end up with lot of logic for protecting different url for various roles. You may write code to read a custom configuration file.&lt;br /&gt;&lt;br /&gt;Following is sample code&lt;br /&gt;&lt;br /&gt;struts.xml&lt;br /&gt;---------------&lt;br /&gt;&lt;br /&gt;    &amp;lt;package  extends=&quot;struts-default&quot;&amp;gt;&lt;br /&gt;               &lt;br /&gt;        &amp;lt;interceptors&amp;gt;&lt;br /&gt;            &amp;lt;interceptor name=&quot;login&quot; class=&quot;mypkg.AuthenticationInterceptor&quot;/&amp;gt;&lt;br /&gt;           &amp;lt;interceptor-stack name=&quot;intStack&quot;&amp;gt;&lt;br /&gt;                          &amp;lt;interceptor-ref name=&quot;login&quot;/&amp;gt;&lt;br /&gt;                          &amp;lt;interceptor-ref name=&quot;defaultStack&quot;/&amp;gt;    &lt;br /&gt;           &amp;lt;/interceptor-stack&amp;gt;&lt;br /&gt;        &amp;lt;/interceptors&amp;gt;&lt;br /&gt;       &lt;br /&gt;        &amp;lt;default-interceptor-ref name=&quot;intStack&quot;/&amp;gt;&lt;br /&gt;&lt;br /&gt;Login form&lt;br /&gt;---------------&lt;br /&gt;    &amp;lt;s:form action=&quot;login&quot;&amp;gt;&lt;br /&gt;        &amp;lt;table&amp;gt; &amp;lt;tr&amp;gt; &amp;lt;td &amp;gt;Username:&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;                    &amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&amp;lt;s:textfield name=&quot;username&quot;/&amp;gt;&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;                    &amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Password:&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;                   &amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&amp;lt;s:password label=&quot;Password&quot; name=&quot;password&quot;/&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;                   &amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&amp;lt;s:submit value=&quot;Login&quot; /&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;        &amp;lt;/table&amp;gt;&lt;br /&gt;    &amp;lt;/s:form&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;LoginAction&lt;br /&gt;----------------&lt;br /&gt;&lt;br /&gt;public class LoginAction  {&lt;br /&gt;   &lt;br /&gt;    private User user;&lt;br /&gt;    private String username;&lt;br /&gt;    private String password;&lt;br /&gt;   &lt;br /&gt;    public String execute() throws Exception {&lt;br /&gt;        Map session = ActionContext.getContext().getSession();&lt;br /&gt;       &lt;br /&gt;        if (username == null) {&lt;br /&gt;            return &quot;login&quot;;&lt;br /&gt;        }&lt;br /&gt;        user = yourDAO.getUser(username, password);&lt;br /&gt;        if (user != null) {&lt;br /&gt;            session.put(&quot;user&quot;, user);&lt;br /&gt;            return SUCCESS;&lt;br /&gt;        }&lt;br /&gt;        else {&lt;br /&gt;            return &quot;login&quot;;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    public void setUsername(String username){&lt;br /&gt;        this.username = username;&lt;br /&gt;    }&lt;br /&gt;    public String getUsername() {&lt;br /&gt;        return username;&lt;br /&gt;    }&lt;br /&gt;    public void setPassword(String password){&lt;br /&gt;        this.password = password;&lt;br /&gt;    }&lt;br /&gt;    public String getPassword() {&lt;br /&gt;        return password;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Interceptor code&lt;br /&gt;----------------------&lt;br /&gt;&lt;br /&gt;public class AuthenticationInterceptor implements Interceptor {&lt;br /&gt; &lt;br /&gt;   public String intercept(ActionInvocation invocation) throws Exception {&lt;br /&gt;&lt;br /&gt;        Map session = ActionContext.getContext().getSession();&lt;br /&gt;&lt;br /&gt;         //Check by looking at user&#39;s http session for certain data which you&lt;br /&gt;         // If not logged in redirect user to login.action&lt;br /&gt;        if (!session.containsKey(&quot;user&quot;) &amp;amp;&amp;amp; !ActionContext.getContext().getName().equals(&quot;login&quot;)) {&lt;br /&gt;                return &quot;login&quot;;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;         // If user is logged in then vcheck if user has access to requested url&lt;br /&gt;&lt;br /&gt;        String result = invocation.invoke();&lt;br /&gt;        return result;&lt;br /&gt;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Container managed security + Custom security: Servlet specification provides declarative security, access to web resources can be defined in web.xml. Once user is authenticated application servlet API could be used to access authenticated user / role information. Struts2 action can also be restricted to certain roles declarative style. One drawback is Roles information needs to be maintained in web.xml, If your application creates roles dynamically, new roles have to be also specified in web.xml (Needs rebuilding war file/application restart).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;security-constraint&amp;gt;&lt;br /&gt;  &amp;lt;web-resource-collection&amp;gt;&lt;br /&gt;   &amp;lt;web-resource-name&amp;gt;SecuredPages&amp;lt;/web-resource-name&amp;gt;&lt;br /&gt;   &amp;lt;url-pattern&amp;gt;/*&amp;lt;/url-pattern&amp;gt;&lt;br /&gt;  &amp;lt;/web-resource-collection&amp;gt;&lt;br /&gt;  &amp;lt;auth-constraint&amp;gt;&lt;br /&gt;   &amp;lt;role-name&amp;gt;*&amp;lt;/role-name&amp;gt;&lt;br /&gt;  &amp;lt;/auth-constraint&amp;gt;&lt;br /&gt; &amp;lt;/security-constraint&amp;gt;&lt;br /&gt; &lt;br /&gt; &amp;lt;security-role&amp;gt;&lt;br /&gt;  &amp;lt;description&amp;gt;Role required to see admin pages.&amp;lt;/description&amp;gt;&lt;br /&gt;  &amp;lt;role-name&amp;gt;admin&amp;lt;/role-name&amp;gt;&lt;br /&gt; &amp;lt;/security-role&amp;gt;&lt;br /&gt; &amp;lt;security-role&amp;gt;&lt;br /&gt;  &amp;lt;description&amp;gt;Role with read only.&amp;lt;/description&amp;gt;&lt;br /&gt;  &amp;lt;role-name&amp;gt;reader&amp;lt;/role-name&amp;gt;&lt;br /&gt; &amp;lt;/security-role&amp;gt;&lt;br /&gt;&lt;br /&gt;For autheticating form authentication can be used with a jsp specified in web.xml, the jsp includes a form action j_security_check. username/password are validated against the realm specified in web.xml. Realm represents the source of username/password and associated roles. There are a wide variety of Realms (JDBC, JNDI, xml file, Datasource) which are specific to servlet containers. For a web application deployed in Tomcat Realm is specified in context.xml under META-INF/ direcotory&lt;br /&gt;&lt;br /&gt; &amp;lt;login-config&amp;gt;&lt;br /&gt;  &amp;lt;auth-method&amp;gt;FORM&amp;lt;/auth-method&amp;gt;&lt;br /&gt;  &amp;lt;realm-name&amp;gt;JDBCRealm&amp;lt;/realm-name&amp;gt;&lt;br /&gt;  &amp;lt;form-login-config&amp;gt;&lt;br /&gt;   &amp;lt;form-login-page&amp;gt;/login2.jsp&amp;lt;/form-login-page&amp;gt;&lt;br /&gt;   &amp;lt;form-error-page&amp;gt;/error.jsp&amp;lt;/form-error-page&amp;gt;&lt;br /&gt;  &amp;lt;/form-login-config&amp;gt;&lt;br /&gt; &amp;lt;/login-config&amp;gt; &lt;br /&gt;Login form&lt;br /&gt;----------&lt;br /&gt;&lt;br /&gt;    &amp;lt;s:form action=&quot;j_security_check&quot;&amp;gt;&lt;br /&gt;        &amp;lt;table&amp;gt; &amp;lt;tr&amp;gt; &amp;lt;td &amp;gt;Username:&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;                    &amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&amp;lt;s:textfield name=&quot;j_username&quot;/&amp;gt;&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;                    &amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Password:&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;                   &amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&amp;lt;s:password label=&quot;Password&quot; name=&quot;j_password&quot;/&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;                   &amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&amp;lt;s:submit value=&quot;Login&quot; /&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;        &amp;lt;/table&amp;gt;&lt;br /&gt;    &amp;lt;/s:form&amp;gt;&lt;br /&gt;&lt;br /&gt;META-INF/context.xml, login action will be validated against following Realm&lt;br /&gt;-------------------------------------------------------------------------&lt;br /&gt;&amp;lt;Context&amp;gt;&lt;br /&gt;&amp;lt;Realm className=&quot;org.apache.catalina.realm.JDBCRealm&quot; &lt;br /&gt;     driverName=&quot;org.gjt.mm.mysql.Driver&quot; &lt;br /&gt;     connectionURL=&quot;jdbc:mysql://mydbserver/mydb_test2&quot;&lt;br /&gt;     connectionName=&quot;myuser&quot; connectionPassword=&quot;mypassword&quot;&lt;br /&gt;        userTable=&quot;users&quot; userNameCol=&quot;email&quot; &lt;br /&gt;        userCredCol=&quot;password&quot; digest=&quot;MD5&quot;        &lt;br /&gt;     userRoleTable=&quot;user_roles&quot; roleNameCol=&quot;name&quot;/&amp;gt;    &lt;br /&gt;&amp;lt;/Context&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Above setup will provide for authentication and authorization for URL in web applications.&lt;br /&gt;&lt;br /&gt;If you application requires further restrictions on struts Actions based on user roles, roles interceptor could be used and security could be declared in struts.xml.&lt;br /&gt;&lt;br /&gt;Any further customization could be programmed by writing additional struts interceptors or servlet filters. Following is an example of custom interceptor where a different struts result/view is presented to user based on role.&lt;br /&gt;&lt;br /&gt;public class ReaderViewInterceptor implements Interceptor{&lt;br /&gt;&lt;br /&gt; public void destroy() {&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void init() {&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public String intercept(ActionInvocation invocation) throws Exception {&lt;br /&gt;  &lt;br /&gt;    invocation.addPreResultListener(new PreResultListener() { &lt;br /&gt;        public void beforeResult(ActionInvocation invocation, String resultCode) { &lt;br /&gt;          Map resultsMap = invocation.getProxy().getConfig().getResults();&lt;br /&gt;    if( ServletActionContext.getRequest().isUserInRole(&quot;Reader&quot;) ) {&lt;br /&gt;     invocation.setResultCode(&quot;readonly_&quot; + resultCode);&lt;br /&gt;    }          &lt;br /&gt;              } &lt;br /&gt;  });   &lt;br /&gt;  &lt;br /&gt;  String result = invocation.invoke();&lt;br /&gt;  return result;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/5491145225116515465/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/5491145225116515465' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/5491145225116515465'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/5491145225116515465'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2009/07/web-app-security-struts2_18.html' title='Web App + Security + Struts2'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-2230539022339766539</id><published>2009-07-18T09:41:00.000-07:00</published><updated>2009-07-19T08:12:55.287-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Security"/><category scheme="http://www.blogger.com/atom/ns#" term="struts2"/><category scheme="http://www.blogger.com/atom/ns#" term="web application"/><title type='text'>Web App + Security + Struts2</title><content type='html'>When securing a web application there are few options for authentication and authorization of user/roles. You could write your custom logic,  use container managed security, or use 3rd part libraries, or augment container managed security with custom logic.&lt;br /&gt;&lt;br /&gt;Custom Security: Custom security is implemented by writing programming logic. For a pure Struts2 app (Where every url of the web application is served by Struts actions), typically user is authenticated using a custom HTML form backed by a custom LoginAction. LoginAction will validated username/password and then store the user information in session variable.  This user/role information stored in  session can be used control the logic for what URL/actions can be accessed by user. You can write a Struts2 interceptor and have every single request for struts2 resource go through this interceptor. When user tries to access a resource in the application, this interceptor will check if user is not logged by looking at certain data in user session. If data is not found user can redirected to login action. If user is logged in then you can check the role to make sure user has access to the resource, once the action is executed, you can use the user role information in view code (JSPs or other ) to hide/show certain section of view.&lt;br /&gt;&lt;br /&gt;Disadvantage of above approach is is that only struts actions are protected.  Another approach will be to write a Servlet filter instead of a struts Interceptor. Other disadvantage is you will end up with lot of logic for protecting different url for various roles. You may write code to read a custom configuration file.&lt;br /&gt;&lt;br /&gt;Following is sample code&lt;br /&gt;&lt;br /&gt;struts.xml&lt;br /&gt;---------------&lt;br /&gt;&lt;br /&gt;    &amp;lt;package  extends=&quot;struts-default&quot;&gt;&lt;br /&gt;               &lt;br /&gt;        &amp;lt;interceptors&gt;&lt;br /&gt;            &amp;lt;interceptor name=&quot;login&quot; class=&quot;mypkg.AuthenticationInterceptor&quot;/&gt;&lt;br /&gt;           &amp;lt;interceptor-stack name=&quot;intStack&quot;&gt;&lt;br /&gt;                          &amp;lt;interceptor-ref name=&quot;login&quot;/&gt;&lt;br /&gt;                          &amp;lt;interceptor-ref name=&quot;defaultStack&quot;/&gt;    &lt;br /&gt;           &amp;lt;/interceptor-stack&gt;&lt;br /&gt;        &amp;lt;/interceptors&gt;&lt;br /&gt;       &lt;br /&gt;        &amp;lt;default-interceptor-ref name=&quot;intStack&quot;/&gt;&lt;br /&gt;&lt;br /&gt;Login form&lt;br /&gt;---------------&lt;br /&gt;    &amp;lt;s:form action=&quot;login&quot;&gt;&lt;br /&gt;        &amp;lt;table&gt; &amp;lt;tr&gt; &amp;lt;td &gt;Username:&amp;lt;/td&gt; &amp;lt;/tr&gt;&lt;br /&gt;                    &amp;lt;tr&gt;&amp;lt;td&gt;&amp;lt;s:textfield name=&quot;username&quot;/&gt;&amp;lt;/td&gt; &amp;lt;/tr&gt;&lt;br /&gt;                    &amp;lt;tr&gt;&amp;lt;td&gt;Password:&amp;lt;/td&gt; &amp;lt;/tr&gt;&lt;br /&gt;                   &amp;lt;tr&gt;&amp;lt;td&gt;&amp;lt;s:password label=&quot;Password&quot; name=&quot;password&quot;/&gt;&amp;lt;/td&gt;&amp;lt;/tr&gt;&lt;br /&gt;                   &amp;lt;tr&gt;&amp;lt;td&gt;&amp;lt;s:submit value=&quot;Login&quot; /&gt;&amp;lt;/td&gt;&amp;lt;/tr&gt;&lt;br /&gt;        &amp;lt;/table&gt;&lt;br /&gt;    &amp;lt;/s:form&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;LoginAction&lt;br /&gt;----------------&lt;br /&gt;&lt;br /&gt;public class LoginAction  {&lt;br /&gt;   &lt;br /&gt;    private User user;&lt;br /&gt;    private String username;&lt;br /&gt;    private String password;&lt;br /&gt;   &lt;br /&gt;    public String execute() throws Exception {&lt;br /&gt;        Map session = ActionContext.getContext().getSession();&lt;br /&gt;       &lt;br /&gt;        if (username == null) {&lt;br /&gt;            return &quot;login&quot;;&lt;br /&gt;        }&lt;br /&gt;        user = yourDAO.getUser(username, password);&lt;br /&gt;        if (user != null) {&lt;br /&gt;            session.put(&quot;user&quot;, user);&lt;br /&gt;            return SUCCESS;&lt;br /&gt;        }&lt;br /&gt;        else {&lt;br /&gt;            return &quot;login&quot;;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    public void setUsername(String username){&lt;br /&gt;        this.username = username;&lt;br /&gt;    }&lt;br /&gt;    public String getUsername() {&lt;br /&gt;        return username;&lt;br /&gt;    }&lt;br /&gt;    public void setPassword(String password){&lt;br /&gt;        this.password = password;&lt;br /&gt;    }&lt;br /&gt;    public String getPassword() {&lt;br /&gt;        return password;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Interceptor code&lt;br /&gt;----------------------&lt;br /&gt;&lt;br /&gt;public class AuthenticationInterceptor implements Interceptor {&lt;br /&gt; &lt;br /&gt;   public String intercept(ActionInvocation invocation) throws Exception {&lt;br /&gt;&lt;br /&gt;        Map session = ActionContext.getContext().getSession();&lt;br /&gt;&lt;br /&gt;         //Check by looking at user&#39;s http session for certain data which you&lt;br /&gt;         // If not logged in redirect user to login.action&lt;br /&gt;        if (!session.containsKey(&quot;user&quot;) &amp;&amp; !ActionContext.getContext().getName().equals(&quot;login&quot;)) {&lt;br /&gt;                return &quot;login&quot;;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;         // If user is logged in then vcheck if user has access to requested url&lt;br /&gt;&lt;br /&gt;        String result = invocation.invoke();&lt;br /&gt;        return result;&lt;br /&gt;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Container managed security + Custom security: Servlet specification provides declarative security, access to web resources can be defined in web.xml. Once user is authenticated application servlet API could be used to access authenticated user / role information. Struts2 action can also be restricted to certain roles declarative style. One drawback is Roles information needs to be maintained in web.xml, If your application creates roles dynamically, new roles have to be also specified in web.xml (Needs rebuilding war file/application restart).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;security-constraint&gt;&lt;br /&gt;  &amp;lt;web-resource-collection&gt;&lt;br /&gt;   &amp;lt;web-resource-name&gt;SecuredPages&amp;lt;/web-resource-name&gt;&lt;br /&gt;   &amp;lt;url-pattern&gt;/*&amp;lt;/url-pattern&gt;&lt;br /&gt;  &amp;lt;/web-resource-collection&gt;&lt;br /&gt;  &amp;lt;auth-constraint&gt;&lt;br /&gt;   &amp;lt;role-name&gt;*&amp;lt;/role-name&gt;&lt;br /&gt;  &amp;lt;/auth-constraint&gt;&lt;br /&gt; &amp;lt;/security-constraint&gt;&lt;br /&gt; &lt;br /&gt; &amp;lt;security-role&gt;&lt;br /&gt;  &amp;lt;description&gt;Role required to see admin pages.&amp;lt;/description&gt;&lt;br /&gt;  &amp;lt;role-name&gt;admin&amp;lt;/role-name&gt;&lt;br /&gt; &amp;lt;/security-role&gt;&lt;br /&gt; &amp;lt;security-role&gt;&lt;br /&gt;  &amp;lt;description&gt;Role with read only.&amp;lt;/description&gt;&lt;br /&gt;  &amp;lt;role-name&gt;reader&amp;lt;/role-name&gt;&lt;br /&gt; &amp;lt;/security-role&gt;&lt;br /&gt;&lt;br /&gt;For autheticating form authentication can be used with a jsp specified in web.xml, the jsp includes a form action j_security_check. username/password are validated against the realm specified in web.xml. Realm represents the source of username/password and associated roles. There are a wide variety of Realms (JDBC, JNDI, xml file, Datasource) which are specific to servlet containers. For a web application deployed in Tomcat Realm is specified in context.xml under META-INF/ direcotory&lt;br /&gt;&lt;br /&gt; &amp;lt;login-config&gt;&lt;br /&gt;  &amp;lt;auth-method&gt;FORM&amp;lt;/auth-method&gt;&lt;br /&gt;  &amp;lt;realm-name&gt;JDBCRealm&amp;lt;/realm-name&gt;&lt;br /&gt;  &amp;lt;form-login-config&gt;&lt;br /&gt;   &amp;lt;form-login-page&gt;/login2.jsp&amp;lt;/form-login-page&gt;&lt;br /&gt;   &amp;lt;form-error-page&gt;/error.jsp&amp;lt;/form-error-page&gt;&lt;br /&gt;  &amp;lt;/form-login-config&gt;&lt;br /&gt; &amp;lt;/login-config&gt; &lt;br /&gt;Login form&lt;br /&gt;----------&lt;br /&gt;&lt;br /&gt;    &amp;lt;s:form action=&quot;j_security_check&quot;&gt;&lt;br /&gt;        &amp;lt;table&gt; &amp;lt;tr&gt; &amp;lt;td &gt;Username:&amp;lt;/td&gt; &amp;lt;/tr&gt;&lt;br /&gt;                    &amp;lt;tr&gt;&amp;lt;td&gt;&amp;lt;s:textfield name=&quot;j_username&quot;/&gt;&amp;lt;/td&gt; &amp;lt;/tr&gt;&lt;br /&gt;                    &amp;lt;tr&gt;&amp;lt;td&gt;Password:&amp;lt;/td&gt; &amp;lt;/tr&gt;&lt;br /&gt;                   &amp;lt;tr&gt;&amp;lt;td&gt;&amp;lt;s:password label=&quot;Password&quot; name=&quot;j_password&quot;/&gt;&amp;lt;/td&gt;&amp;lt;/tr&gt;&lt;br /&gt;                   &amp;lt;tr&gt;&amp;lt;td&gt;&amp;lt;s:submit value=&quot;Login&quot; /&gt;&amp;lt;/td&gt;&amp;lt;/tr&gt;&lt;br /&gt;        &amp;lt;/table&gt;&lt;br /&gt;    &amp;lt;/s:form&gt;&lt;br /&gt;&lt;br /&gt;META-INF/context.xml, login action will be validated against following Realm&lt;br /&gt;-------------------------------------------------------------------------&lt;br /&gt;&amp;lt;Context&gt;&lt;br /&gt;&amp;lt;Realm className=&quot;org.apache.catalina.realm.JDBCRealm&quot; &lt;br /&gt;     driverName=&quot;org.gjt.mm.mysql.Driver&quot; &lt;br /&gt;     connectionURL=&quot;jdbc:mysql://mydbserver/mydb_test2&quot;&lt;br /&gt;     connectionName=&quot;myuser&quot; connectionPassword=&quot;mypassword&quot;&lt;br /&gt;        userTable=&quot;users&quot; userNameCol=&quot;email&quot; &lt;br /&gt;        userCredCol=&quot;password&quot; digest=&quot;MD5&quot;        &lt;br /&gt;     userRoleTable=&quot;user_roles&quot; roleNameCol=&quot;name&quot;/&gt;    &lt;br /&gt;&amp;lt;/Context&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Above setup will provide for authentication and authorization for URL in web applications.&lt;br /&gt;&lt;br /&gt;If you application requires further restrictions on struts Actions based on user roles, roles interceptor could be used and security could be declared in struts.xml.&lt;br /&gt;&lt;br /&gt;Any further customization could be programmed by writing additional struts interceptors or servlet filters. Following is an example of custom interceptor where a different struts result/view is presented to user based on role.&lt;br /&gt;&lt;br /&gt;public class ReaderViewInterceptor implements Interceptor{&lt;br /&gt;&lt;br /&gt; public void destroy() {&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void init() {&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public String intercept(ActionInvocation invocation) throws Exception {&lt;br /&gt;  &lt;br /&gt;    invocation.addPreResultListener(new PreResultListener() { &lt;br /&gt;        public void beforeResult(ActionInvocation invocation, String resultCode) { &lt;br /&gt;          Map&lt;String, ResultConfig&gt; resultsMap = invocation.getProxy().getConfig().getResults();&lt;br /&gt;    if( ServletActionContext.getRequest().isUserInRole(&quot;Reader&quot;) ) {&lt;br /&gt;     invocation.setResultCode(&quot;readonly_&quot; + resultCode);&lt;br /&gt;    }          &lt;br /&gt;              } &lt;br /&gt;  });   &lt;br /&gt;  &lt;br /&gt;  String result = invocation.invoke();&lt;br /&gt;  return result;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/2230539022339766539/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/2230539022339766539' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/2230539022339766539'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/2230539022339766539'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2009/07/web-app-security-struts2.html' title='Web App + Security + Struts2'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-4338701219563252625</id><published>2009-06-30T06:13:00.001-07:00</published><updated>2010-12-30T13:03:56.879-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="innodb"/><category scheme="http://www.blogger.com/atom/ns#" term="locking"/><category scheme="http://www.blogger.com/atom/ns#" term="mysql"/><title type='text'>MySQL InnoDB lock</title><content type='html'>Recently I had to extract data from a number of large tables (MySql 5.0 InnoDB tables). Given the size of tables and number of joins, I used TEMP tables and stored procedure. When pushing data into temp tables I ran into locks on underlying SELECT tables. These queries were being run on a slave copy of MySQL replication. Lock on the select table were causing replication UPDATE/INSERT queries to time out and crash replication.&lt;br /&gt;&lt;br /&gt;It turned out &quot;INSERT INTO A_TEMP_TABLE select X, Y, Z from LARGE_TABLE1, LARGE_TABLE1 WHERE.....&quot; was locking large tables (InnoDB tables), even after specifying appropriate TRANSACTION ISOLATION LEVEL of READ COMMITTED. &lt;br /&gt;&lt;br /&gt;MySQL documentation says the tables won&#39;t be locked, but they do get locked on 5.0.&lt;br /&gt;&lt;br /&gt;   &quot;INSERT INTO T SELECT ... FROM S WHERE ...  sets an exclusive index record without a gap lock on each row inserted into T. If innodb_locks_unsafe_for_binlog  is enabled or the transaction isolation level is READ COMMITTED, InnoDB does the search on S as a consistent read (no locks)&quot;&lt;br /&gt;&lt;br /&gt;An ugly workaround was to use &quot;SELECT INTO OUTFILE&quot;. Once you write to OUTFILE, you need to import it back in DB using LOAD DATE INFILE. It turns out you can not use LOAD DATA INFILE inside a stored procedure, so I had to fall back to Perl script to do the job. &lt;br /&gt;&lt;br /&gt;Suggestions to MySQL.&lt;br /&gt;&lt;br /&gt;1) Provide ability to ISSUE &quot;INSERT INTO A_TEMP_TABLE select X, Y, Z from LARGE_TABLE1, LARGE_TABLE1 WHERE.....&quot; without locking tables when an apporpriate transaction level is specified. This probably is already fixed in 5.1.&lt;br /&gt;2) Allow DATA LOAD INFILE from stored procedures.</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/4338701219563252625/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/4338701219563252625' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/4338701219563252625'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/4338701219563252625'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2009/06/mysql-innodb-lock_30.html' title='MySQL InnoDB lock'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-5680335625040389095</id><published>2009-06-30T06:13:00.000-07:00</published><updated>2009-06-30T06:21:16.168-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="innodb"/><category scheme="http://www.blogger.com/atom/ns#" term="locking"/><category scheme="http://www.blogger.com/atom/ns#" term="mysql"/><title type='text'>MySQL InnoDB lock</title><content type='html'>Recently I had to extract data from a number of large tables (MySql 5.0 InnoDB tables). Given the size of tables and number of joins, I used TEMP tables and stored procedure. When pushing data into temp tables I ran into locks on underlying SELECT tables. These queries were being run on a slave copy of MySQL replication. Lock on the select table were causing replication UPDATE/INSERT queries to time out and crash replication.&lt;br /&gt;&lt;br /&gt;It turned out &quot;INSERT INTO A_TEMP_TABLE select X, Y, Z from LARGE_TABLE1, LARGE_TABLE1 WHERE.....&quot; was locking large tables (InnoDB tables), even after specifying appropriate TRANSACTION ISOLATION LEVEL of READ COMMITTED. &lt;br /&gt;&lt;br /&gt;MySQL documentation says the tables won&#39;t be locked, but they do get locked on 5.0.&lt;br /&gt;&lt;br /&gt;   &quot;INSERT INTO T SELECT ... FROM S WHERE ...  sets an exclusive index record without a gap lock on each row inserted into T. If innodb_locks_unsafe_for_binlog  is enabled or the transaction isolation level is READ COMMITTED, InnoDB does the search on S as a consistent read (no locks)&quot;&lt;br /&gt;&lt;br /&gt;An ugly workaround was to use &quot;SELECT INTO OUTFILE&quot;. Once you write to OUTFILE, you need to import it back in DB using LOAD DATE INFILE. It turns out you can not use LOAD DATA INFILE inside a stored procedure, so I had to fall back to Perl script to do the job. &lt;br /&gt;&lt;br /&gt;Suggestions to MySQL.&lt;br /&gt;&lt;br /&gt;1) Provide ability to ISSUE &quot;INSERT INTO A_TEMP_TABLE select X, Y, Z from LARGE_TABLE1, LARGE_TABLE1 WHERE.....&quot; without locking tables when an apporpriate transaction level is specified. This probably is already fixed in 5.1.&lt;br /&gt;2) Allow DATA LOAD INFILE from stored procedures.</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/5680335625040389095/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/5680335625040389095' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/5680335625040389095'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/5680335625040389095'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2009/06/mysql-innodb-lock.html' title='MySQL InnoDB lock'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-7298543355780066719</id><published>2009-04-29T19:57:00.000-07:00</published><updated>2009-05-01T03:46:13.241-07:00</updated><title type='text'>checked and unchecked exception</title><content type='html'>&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUPvPMCeSEMrCBjZLJpqAJnv63kxi6jPFf0o18DIIW8rJ6IuKkA8qXclNMWS5Kd1LqeE8RGbaBId0Nz1I3xQW_TkjW-Ddwx2s0IhT3cQWU4C_uYlWSAugSFTZFISTAM7W388842yNDkqo/s1600-h/ClassDiagram1.gif&quot;&gt;&lt;img style=&quot;cursor:pointer; cursor:hand;width: 400px; height: 256px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUPvPMCeSEMrCBjZLJpqAJnv63kxi6jPFf0o18DIIW8rJ6IuKkA8qXclNMWS5Kd1LqeE8RGbaBId0Nz1I3xQW_TkjW-Ddwx2s0IhT3cQWU4C_uYlWSAugSFTZFISTAM7W388842yNDkqo/s400/ClassDiagram1.gif&quot; border=&quot;0&quot; alt=&quot;&quot;id=&quot;BLOGGER_PHOTO_ID_5330317779479767394&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Checked Exceptions. These are the exception that must be caught if you call a method that throws Checked exception. A method that may throw such exception, must explicitly declare it in method signature.&lt;br /&gt;&lt;br /&gt;Unchecked exceptions: They can be thrown anytime, they not forced to be  caught by compiler. Methods do not have to declare that they can throw an unchecked exception. Error and RuntimeException and their subclasses are treated as unchecked exceptions by java compiler.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Exceptions represent errors from which a program can potentially recover.&lt;br /&gt;Exceptions and all its subclasses except RuntimeException are &quot;Checked&quot; exception&lt;br /&gt;if you call a method that throws an exception you must have a catch block or declare your method to throw the exception.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Runtime exceptions should represent errors due to programming error, and a program is not expected recover from such exceptions. A program may catch the exception with Catch block but is not forced to catch it by compiler as Runtime excptions are &quot;uncheck exceptions&quot;. Example ArrayIndexOutOfBound, NullPointerException, ClassCastException, NumberFormatException&lt;br /&gt;&lt;br /&gt;Error class is also &quot;unchecked exceptions&quot;. They represent errors from which program can not recover. Example is OutOfMemoryError.&lt;br /&gt;&lt;br /&gt;One should try to reuse existing exception classes. There should be a real good exception to write a new exception class.&lt;br /&gt;&lt;br /&gt;Following are commonly used exception. IllegalArgumentException, IllegalStateException, NullPointerException, IndexOutOfBoundsException, ConcurrentModificationException, UnsupportedOperationException.&lt;br /&gt;&lt;br /&gt;Exception chaining: wrap an exception in a new one and  throw new exception.&lt;br /&gt;&lt;br /&gt;Try handling them at lower level.&lt;br /&gt;&lt;br /&gt;Document exceptions thrown by a method.&lt;br /&gt;&lt;br /&gt;Include detailed error message.&lt;br /&gt;&lt;br /&gt;Validate and throw exceptions early on in a method.&lt;br /&gt;&lt;br /&gt;Don&#39;t ignore exceptions&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Reference: Effective Java, 2nd edition.</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/7298543355780066719/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/7298543355780066719' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/7298543355780066719'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/7298543355780066719'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2009/04/checked-and-unchecked-exceptions.html' title='checked and unchecked exception'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUPvPMCeSEMrCBjZLJpqAJnv63kxi6jPFf0o18DIIW8rJ6IuKkA8qXclNMWS5Kd1LqeE8RGbaBId0Nz1I3xQW_TkjW-Ddwx2s0IhT3cQWU4C_uYlWSAugSFTZFISTAM7W388842yNDkqo/s72-c/ClassDiagram1.gif" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-4866029046418744166</id><published>2009-03-09T20:52:00.000-07:00</published><updated>2009-03-09T20:54:03.211-07:00</updated><title type='text'>EJB Hibernate</title><content type='html'>&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRi2nT937-26VU0DZ4NVcEGjPFBlg1T1PPf-EIGAA3or9m7eGKKqYUVoF0g1RyyT2tVthIP43sKsq0ZAmUkQu7r7IFPNO-BjNOmIp8eTRbIswZF3CoyM2cJTVNuOByD0WOQrtcMiOIX8g/s1600-h/EJBHibernate.png&quot;&gt;&lt;img style=&quot;cursor:pointer; cursor:hand;width: 400px; height: 189px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRi2nT937-26VU0DZ4NVcEGjPFBlg1T1PPf-EIGAA3or9m7eGKKqYUVoF0g1RyyT2tVthIP43sKsq0ZAmUkQu7r7IFPNO-BjNOmIp8eTRbIswZF3CoyM2cJTVNuOByD0WOQrtcMiOIX8g/s400/EJBHibernate.png&quot; border=&quot;0&quot; alt=&quot;&quot;id=&quot;BLOGGER_PHOTO_ID_5311402374006144914&quot; /&gt;&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/4866029046418744166/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/4866029046418744166' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/4866029046418744166'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/4866029046418744166'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2009/03/ejb-hibernate.html' title='EJB Hibernate'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRi2nT937-26VU0DZ4NVcEGjPFBlg1T1PPf-EIGAA3or9m7eGKKqYUVoF0g1RyyT2tVthIP43sKsq0ZAmUkQu7r7IFPNO-BjNOmIp8eTRbIswZF3CoyM2cJTVNuOByD0WOQrtcMiOIX8g/s72-c/EJBHibernate.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-1687572111511231080</id><published>2009-03-04T14:10:00.000-08:00</published><updated>2009-03-04T15:53:49.063-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="regular expressions"/><category scheme="http://www.blogger.com/atom/ns#" term="scripting"/><title type='text'>Love and hate regular expressions</title><content type='html'>I had to look at thousands of lines of perl code packed full of regular expressions (some spanning multiple line). It is not a fun thing.  Regular expression are extremely powerful, with power comes responsibility :).&lt;br /&gt;&lt;br /&gt;I would use them with the consideration that the code I write will have to maintained by someone else few months/years down the line. I would place a few lines of documentation as to what the code is trying to do. It is very hard to document regular expression, placing a example text as to what RegEx is searching for helps. &lt;br /&gt;&lt;br /&gt;If I am writing a one time script that won&#39;t be used by anyone else, I would unleash the full power for regular expressions.&lt;br /&gt;&lt;br /&gt;Do check out this online tool to test your regular expressions. I can&#39;t thank enough the author of this tool. http://www.gskinner.com/RegExr/ , it made writing and testing the regular expression much less stressful.&lt;br /&gt;&lt;br /&gt;Another great resource to learn regular expressions is at http://www.regular-expressions.info/engine.html&lt;br /&gt;&lt;br /&gt;TODO: ADD cheatsheet of regex below!</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/1687572111511231080/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/1687572111511231080' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/1687572111511231080'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/1687572111511231080'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2009/03/love-and-hate-regular-expressions.html' title='Love and hate regular expressions'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-9159118832060217627</id><published>2009-02-17T15:10:00.000-08:00</published><updated>2009-03-02T15:09:07.676-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="architecture"/><category scheme="http://www.blogger.com/atom/ns#" term="patterns"/><title type='text'>Convention Over Configuration</title><content type='html'>Convention Over Configuration refers to predefining conventions (for example a standard location where a file/directory is expected or following a naming schema for resources) for underlying frameworks/applications so that framework/application can work without having to specify these entries in configuration files. This enable end user of software to get going without lot of configuration, however a user/developer still has flexibility to configure in unconventional way.&lt;br /&gt;&lt;br /&gt;Another term used is &quot;Configuration by exception&quot;, configure when there is exception to rule(convention).&lt;br /&gt;&lt;br /&gt;Example of these are plenty,&lt;br /&gt;&lt;br /&gt;Build tool Maven expect source files to be in certain conventional location (like java classes reside in main\java\src ), however user can still specify other location for source code.&lt;br /&gt;&lt;br /&gt;Hibernate Data mappings can work without specifying mapping files by following convention for Table names matching Class names.&lt;br /&gt;&lt;br /&gt;Advantages: &lt;br /&gt;-End user/developer can get going without too much configuration or too much boiler plate code(code that is repetitive and standard).&lt;br /&gt;-Provides structure/discipline to a projects, certain things should be done using conventions, like source files should be named certain ways.  Convention makes it easier for developer who is familiar with convention to jump into a project, as the project is organized in a familiar way.  (On the flip side a developer who is new to framework, need to learn conventions!)&lt;br /&gt;   &lt;br /&gt;&lt;br /&gt;Disadvantages:&lt;br /&gt;Underlying framework/application has to write code for convention and for configuration.&lt;br /&gt;Convention have to be learned.</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/9159118832060217627/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/9159118832060217627' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/9159118832060217627'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/9159118832060217627'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2009/02/convention-over-configuration.html' title='Convention Over Configuration'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-5272821404859531923</id><published>2009-01-20T19:18:00.000-08:00</published><updated>2009-02-13T10:53:44.462-08:00</updated><title type='text'>Maven</title><content type='html'>A few months back I started using maven for a project, and since then my appreciation for this tool has been on the rise as I discover various feature and new plugins. Maven help out with software development process as well with design. Following are some of the benefits I have experienced.&lt;br /&gt;&lt;br /&gt;Maven adds structure to your code repository. There is a place to put different artifacts (java source, properties files etc, JUnit test cases), this structure brings consistency to how code is organized in version control system.&lt;br /&gt;&lt;br /&gt;Maven promotes modular design thru its dependency management feature. You can break up you project into different modules, and then build other modules on top of other modules. Maven can bring these modules together at build time. For example you could separate out DAO/Data model code create a maven module, then create another maven module for business logic which uses DAO/Data module, maven lets you specify the dependencies. This way you are maintaining a modular code and this will help you avoid mix up between your Data model class and business classes, lower layer modules are usable in multiple project if you need them.&lt;br /&gt;&lt;br /&gt;Maven also helps you manage dependencies on 3rd party modules. you don&#39;t have to manually download tonnes of 3rd part jars, instead provide minimum information (artifactId, groupId, version) about 3rd party components, and the associated jars will be downloaded for you, you won&#39;t have to keep track hundreds of jars coming for dozens for 3rd part components.&lt;br /&gt;&lt;br /&gt;Another advantage of  of Maven is it lets developers work with different environments like QA, development, staging, production with ease. Settings for different environments could be specified in respective properties files  (for example where is QA DB ?) , and at build time simply include the property file or apply these properties to various configuration file and other resources by simply specify a profile switch as one of the maven command argument.&lt;br /&gt;&lt;br /&gt;Maven lets you run JUnit tests associated with you project.&lt;br /&gt;&lt;br /&gt;Maven provides command line interface, and also works very well with popular IDEs like Eclipse and Netbeans, and with automated builds tools.&lt;br /&gt;&lt;br /&gt;A wide range of maven plugins are available to do various task like compile, packaging, generating code and lot more. For example generating axis WSDL code from existing java sources, you could create various packaging foo you projects, execute SQL script againsts database, deploy your application. These links provide plugin descriptions http://maven.apache.org/plugins/index.html, http://mojo.codehaus.org/plugins.html&lt;br /&gt;&lt;br /&gt;When you are starting with Maven the terminologies could be daunting but once you start using it, it is a pleasant song.</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/5272821404859531923/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/5272821404859531923' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/5272821404859531923'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/5272821404859531923'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2009/01/maven.html' title='Maven'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-7978009503074119994</id><published>2008-12-29T14:35:00.000-08:00</published><updated>2008-12-29T19:51:40.008-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Heap Error"/><category scheme="http://www.blogger.com/atom/ns#" term="IDE"/><category scheme="http://www.blogger.com/atom/ns#" term="Troubleshooting"/><title type='text'>Java Heap Error in Maven JUnit Eclipse Environment</title><content type='html'>I encountered Java Heap Error (java.lang.outOfMemoryError) while running JUnit test cases using Maven plugin in Eclipse IDE. When maven build is run thru IDE, it forks another java process which kept running out of heap space after reaching only about 100M. After changing a number of different settings in IDE, Maven, OS environment variables and spending close to 8 hrs. Finally arrived at the solution.&lt;br /&gt;&lt;br /&gt;First changed environment variable JAVA_OPTS to -Xms40m -Xmx1024m -XX:MaxPermSize=256m  but no luck.&lt;br /&gt;&lt;br /&gt;Second changed MAVEN_OPTS to -Xms40m -Xmx1024m -XX:MaxPermSize=256m but no luck.&lt;br /&gt;&lt;br /&gt;Third changed eclipse.ini to have vm parameter -Xms40m -Xmx1024m -XX:MaxPermSize=256m but no luck. This file is so fragile, making changes here breaks eclipse pretty bad.&lt;br /&gt;&lt;br /&gt;Fourth through  IDE , Run Configuration -&gt; Maven Build provided VM parameters -Xms40m -Xmx1024m -XX:MaxPermSize=256m -verbose  but no luck.&lt;br /&gt;&lt;br /&gt;Fifth changed IDE setting for JDK to use vm parameters -Xms40m -Xmx1024m -XX:MaxPermSize=256m but no luck&lt;br /&gt;&lt;br /&gt;Sixth option worked like charmed, in maven pom.xml added following setting for the plugin that helps you run JUnit test cases. Added following to .pom file.&lt;br /&gt;&lt;br /&gt;  &amp;lt;plugin&gt;&lt;br /&gt;         &amp;lt;groupid&gt;org.apache.maven.&lt;wbr&gt;plugins&amp;lt;/groupid&gt;&lt;br /&gt;         &amp;lt;artifactid&gt;maven-surefire-&lt;wbr&gt;plugin&amp;lt;/artifactid&gt;&lt;br /&gt;         &amp;lt;version&gt;2.2&amp;lt;/version&gt;&lt;br /&gt;         &amp;lt;configuration&gt;&lt;br /&gt;           &amp;lt;argline&gt;-Xmx1024m&amp;lt;/argline&gt;&lt;br /&gt;         &amp;lt;/configuration&gt;&lt;br /&gt;       &amp;lt;/plugin&gt;</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/7978009503074119994/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/7978009503074119994' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/7978009503074119994'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/7978009503074119994'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2008/12/java-heap-error-in-maven-junit-eclipse.html' title='Java Heap Error in Maven JUnit Eclipse Environment'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5539280185079501154.post-2274503249885746234</id><published>2008-12-13T18:58:00.000-08:00</published><updated>2008-12-13T21:28:17.272-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="mysql"/><category scheme="http://www.blogger.com/atom/ns#" term="Performance"/><title type='text'>mysql replication</title><content type='html'>MySQL provides ability to replicate a database in a Master/Slave fashion. All write and update operations are done on Master instance and are written to  a binary log. This log is read by slave instances to replicate the changes. Replication is done in asynchronous mode, slaves keep track of the location within the log.&lt;br /&gt;&lt;br /&gt;When would you use a replication ?&lt;br /&gt;1) For high scalability, read off of slaves and writes on Master.&lt;br /&gt;2) For backups.&lt;br /&gt;3) For having a copy of data to resource intensive analytics without hindering master server performance.</content><link rel='replies' type='application/atom+xml' href='http://tech20nn.blogspot.com/feeds/2274503249885746234/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5539280185079501154/2274503249885746234' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/2274503249885746234'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5539280185079501154/posts/default/2274503249885746234'/><link rel='alternate' type='text/html' href='http://tech20nn.blogspot.com/2008/12/mysql-replication.html' title='mysql replication'/><author><name>vp</name><uri>http://www.blogger.com/profile/00515415171336913815</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>