<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>Aravindan Ramkumar’s Blog</description><title>Thoughts on programming</title><generator>Tumblr (3.0; @aravindanr)</generator><link>http://www.aravindanr.com/</link><item><title>Difference between / and /* in servlet mapping</title><description>&lt;p&gt;/ means the servlet is the default servlet for the app&lt;/p&gt;
&lt;p&gt;/* is used for path mapping and captures all requests to the app. In essence, every request goes through this servlet even requests for JSPs. JSP cannot be used with a servlet mapping of /*&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Never use /* in the servlet mapping.&lt;/p&gt;
&lt;p&gt;This thread by far has the best explanation &lt;a href="http://www.coderanch.com/t/366340/Servlets/java/servlet-mapping-url-pattern-vs" target="_blank"&gt;&lt;a href="http://www.coderanch.com/t/366340/Servlets/java/servlet-mapping-url-pattern-vs"&gt;http://www.coderanch.com/t/366340/Servlets/java/servlet-mapping-url-pattern-vs&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;</description><link>http://www.aravindanr.com/post/14876139393</link><guid>http://www.aravindanr.com/post/14876139393</guid><pubDate>Tue, 27 Dec 2011 14:19:00 -0600</pubDate></item><item><title>Scalate is good but not usable</title><description>&lt;p&gt;&lt;a href="http://scalate.fusesource.org" title="Scalate" target="_blank"&gt;Scalate&lt;/a&gt; is an awesome template engine and i really mean that. Its written in Scala and just fabulous in the way that it supports multiple template languages like HAML(Scaml),Jade and Mustache over a common interface.&lt;/p&gt;
&lt;p&gt;Its simple enough to understand and is really easy to write HTML templates using Jade/Scaml without using an IDE. The documentation is decent enough to convince you to use it on your project. Some of the Scala web frameworks like Scalatra use it as the default TE.&lt;/p&gt;
&lt;p&gt;But if you are going to use it on a Java web project, be warned. You will end up writing Scala code to support simple operations like iterating a collection. I like the idea that the template gets translated to Scala code but to write Scala foreach or for loops for iterating collections every time is ridiculous. Writing templates should be easy and common tasks should already be provided.&lt;/p&gt;
&lt;p&gt;Sure, Scalate lets you call Scala functions and it looks like custom tags in JSP, but the fact that YOU have to write that function makes it look less sexier. I don&amp;#8217;t like JSP, i live with it because it has JSTL. Scalate should come up standard set of functions for mundane tasks.&lt;/p&gt;
&lt;p&gt;If you are using Spring MVC / Struts 2, both provide tag libraries for JSP. Using Scalate, you have to create your own set of functions to mirror the tag-lib functionality, which is certainly not what you want to do.&lt;/p&gt;
&lt;p&gt;Thinking about Scalate for Java web projects, my advice is stay away.&lt;/p&gt;</description><link>http://www.aravindanr.com/post/14320427068</link><guid>http://www.aravindanr.com/post/14320427068</guid><pubDate>Fri, 16 Dec 2011 14:48:00 -0600</pubDate></item><item><title>"Test post from Android"</title><description>“Test post from Android”</description><link>http://www.aravindanr.com/post/10228639868</link><guid>http://www.aravindanr.com/post/10228639868</guid><pubDate>Wed, 14 Sep 2011 22:12:58 -0500</pubDate></item><item><title>Escaping ID selector in jQuery</title><description>&lt;p&gt;If you are using a framework like JSF/Struts which generates ids like&lt;/p&gt;
&lt;p&gt;&lt;em&gt;id=&amp;#8221;form:myButton&amp;#8221;&lt;/em&gt; or &lt;em&gt;id=&amp;#8221;form.myIput[1]&amp;#8221;&lt;/em&gt; and want to use these ids in jQuery, it would complain &amp;#8220;unrecognized expression :myButton&amp;#8221;.&lt;/p&gt;
&lt;p&gt;Escape it with double backslashes like this&lt;/p&gt;
&lt;p&gt;&lt;em&gt;$(&amp;#8220;#form&amp;#92;&amp;#92;:myButton&amp;#8221;) or $(&amp;#8220;#form&amp;#92;&amp;#92;.myInput&amp;#92;&amp;#92;[1&amp;#92;&amp;#92;]&amp;#8221;)&lt;/em&gt;&lt;/p&gt;</description><link>http://www.aravindanr.com/post/10204702432</link><guid>http://www.aravindanr.com/post/10204702432</guid><pubDate>Wed, 14 Sep 2011 11:34:00 -0500</pubDate><category>javascript</category><category>jQuery</category></item><item><title>Deft - Tornodo like app server on the JVM</title><description>&lt;a href="http://deftserver.appspot.com/"&gt;Deft - Tornodo like app server on the JVM&lt;/a&gt;</description><link>http://www.aravindanr.com/post/10147865112</link><guid>http://www.aravindanr.com/post/10147865112</guid><pubDate>Mon, 12 Sep 2011 20:18:00 -0500</pubDate></item><item><title>REST Services in Scala using Akka</title><description>&lt;a href="https://github.com/spray/spray/wiki"&gt;REST Services in Scala using Akka&lt;/a&gt;: &lt;p&gt;I searched for quite a while to find this framework. Its built on top of &lt;a target="_blank" href="http://www.akka.io"&gt;Akka&lt;/a&gt; and its really straightforward to write services.&lt;/p&gt;</description><link>http://www.aravindanr.com/post/10147648209</link><guid>http://www.aravindanr.com/post/10147648209</guid><pubDate>Mon, 12 Sep 2011 20:13:37 -0500</pubDate></item><item><title>Custom Username Password Parameter Names in Spring Security 3</title><description>&lt;p&gt;Spring Security 3 does not allow custom parameter names for username &amp;amp; password, if you are using using form based login. You have to use the default &lt;em&gt;j_username&lt;/em&gt; and &lt;em&gt;j_password&lt;/em&gt;. This may not be a problem for many use cases, but sometimes you would want to change that.&lt;/p&gt;
&lt;p&gt;&lt;!-- more --&gt;A &lt;a target="_blank" href="https://jira.springsource.org/browse/SEC-1445"&gt;feature request&lt;/a&gt; is in process by Spring Security community. If you dont want to wait till that&amp;#8217;s done, here&amp;#8217;s a quick fix.&lt;/p&gt;
&lt;script src="https://gist.github.com/1212600.js?file=UsernamePasswordAuthenticationFilterPostProcessor.java"&gt;&lt;/script&gt;&lt;p&gt;It can be loaded into your config file like this,&lt;/p&gt;
&lt;script src="https://gist.github.com/1212600.js?file=security_config.xml"&gt;&lt;/script&gt;</description><link>http://www.aravindanr.com/post/10138991000</link><guid>http://www.aravindanr.com/post/10138991000</guid><pubDate>Mon, 12 Sep 2011 17:07:00 -0500</pubDate><category>spring</category></item><item><title>Facebook style input box using YUI2</title><description>&lt;p&gt;&lt;a title="Facebook Style Autocomplete by aravindanr, on Flickr" href="http://www.flickr.com/photos/67187588@N02/6118298900/"&gt;&lt;img alt="Facebook Style Autocomplete" height="96" width="500" src="http://farm7.static.flickr.com/6184/6118298900_2a668ea44f.jpg"/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;A facebook style input box using YUI 2 Autocomplete. &lt;a title="Facebook style input demo" target="_blank" href="http://moviechaat.com/aravindanr-blog/demos/aclist/list.html"&gt;Demo here&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;a href="http://moviechaat.com/aravindanr-blog/demos/autocomplete-list.zip"&gt;Download the code&lt;/a&gt; here.&lt;/span&gt;&lt;/p&gt;</description><link>http://www.aravindanr.com/post/9853904232</link><guid>http://www.aravindanr.com/post/9853904232</guid><pubDate>Sun, 02 May 2010 00:00:00 -0500</pubDate></item><item><title>CSS Selector for HTTPBuilder</title><description>&lt;p&gt;&lt;span&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;If you use HTTPBuilder to crawl web pages and extract information, you would have noticed that it uses the Groovy&amp;#8217;s XML Support for parsing HTML. Groovy&amp;#8217;s GPath is powerful, but HTML has something more powerful (not to mention simple, easy and intuitive) for selection, CSS Selectors. jQuery has proved css selectors are indeed, the best way for DOM manipulation.&lt;/p&gt;
&lt;p&gt;&lt;!-- more --&gt;CSS Selectors are available for Java using this library&lt;/p&gt;
&lt;p&gt;&lt;a href="http://github.com/chrsan/css-selectors"&gt;&lt;a href="http://github.com/chrsan/css-selectors"&gt;http://github.com/chrsan/css-selectors&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I wrote a small facade class &lt;a href="http://code.google.com/p/css-selector-httpbuilder/source/browse/trunk/src/main/java/groovyx/net/http/CSSSelector.java"&gt;CSSSelector&lt;/a&gt; to expose css selectors, the Groovy way.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://code.google.com/p/css-selector-httpbuilder/"&gt;&lt;a href="http://code.google.com/p/css-selector-httpbuilder/"&gt;http://code.google.com/p/css-selector-httpbuilder/&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s an example,&lt;/p&gt;
&lt;script src="https://gist.github.com/1196051.js?file=httpbuilder.groovy"&gt;&lt;/script&gt;&lt;p&gt;The CSSSelector class has no dependencies on HTTPBuilder. It can be used with any library in Groovy. If you want to use it with a Java library, you can use &lt;a href="http://github.com/chrsan/css-selectors"&gt;css-selectors&lt;/a&gt; in github&lt;/p&gt;</description><link>http://www.aravindanr.com/post/9851968534</link><guid>http://www.aravindanr.com/post/9851968534</guid><pubDate>Sun, 02 May 2010 00:00:00 -0500</pubDate></item><item><title>window.open behavior in Google Chrome</title><description>&lt;p&gt;&lt;span&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In Chrome,if window.open is called with same window name by pages which are managed by the same process, then the first call creates a new window and subsequent calls return reference to the existing window.If the pages are managed by different processes, then each call to window.open creates a new window.So far, i have been thinking&lt;/p&gt;
&lt;p&gt;&lt;!-- more --&gt;that if window.open is called with name of window which is already open, then a reference to that window is returned, instead of creating a new one. At least thats what &lt;a target="_blank" href="https://developer.mozilla.org/en/DOM/window.open"&gt;MDC &lt;/a&gt;and &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms536651(v=VS.85).aspx"&gt;MSDN&lt;/a&gt;say.&lt;/p&gt;
&lt;p&gt;From MDC,&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If a window with the name strWindowName already exists, then, instead of opening a new window, strUrl is loaded into the existing window. In this case the return value of the method is the existing window.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Before diving into the details, lets have a quick Chrome process architecture primer. You are probably aware that Chrome has a multi process architecture. But contrary what you might think, not every tab has its own process. Some tabs reuse the existing process. So, how does Chrome decide to create a new process for a tab or use an existing one?&lt;/p&gt;
&lt;p&gt;From &lt;a target="_blank" href="http://www.google.com/chrome/intl/en/webmasters-faq.html#newtab"&gt;Chrome webmaster FAQ&lt;/a&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;New tabs spawned from a web page, however, are usually opened in the &lt;span mce_name="em" mce_style="font-style: italic;"&gt;same&lt;/span&gt; process, so that the original page can access the new tab using JavaScript.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The above statement holds good even if the tab spawned from a web page is of a different domain. So, watch your external links.The corollary of this statement is that if a user opens the same page from the same domain,in fresh tabs (using Ctrl-T or the &amp;#8216;plus&amp;#8217; button), then those two pages reside in a separate process. Try these steps to see it in action.&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;Search  &amp;#8221;cnn&amp;#8221; in Yahoo.com and open the first link using Ctrl - left click. It should open in a new tab.&lt;/li&gt;
&lt;li&gt;Now, open another tab and type in cnn.com.&lt;/li&gt;
&lt;li&gt;Open Chrome task manager, you can see 3 items, yahoo search results page and 2 CNN pages. Click on the yahoo search results item, it will also select one CNN.com tab. This means those 2 tabs are managed by the same process. The other cnn.com opened in a fresh tab is managed by a different process.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;&lt;a title="Chrome Task Manager by aravindanr, on Flickr" href="http://www.flickr.com/photos/67187588@N02/6117734961/"&gt;&lt;img src="http://farm7.static.flickr.com/6187/6117734961_1723ac3d42.jpg" width="476" height="308" alt="Chrome Task Manager"/&gt;&lt;/a&gt;Back to our original discussion. This is going to sound like a math theorem, but i cant find a simpler way to explain it. Lets assume page P from domain D is loaded in tabs T1 and T2 and both are managed by different processes. On a button click, P opens a new window using window.open. First button click from P in T1 will open a new window and the first button click from P in T2 will open another window. Subsequent clicks in T1 and T2 will reuse their existing window(s) respectively. But the window created by T1 cannot be used by T2 and vice versa, even though they are from the same domain D.&lt;/p&gt;
&lt;p&gt;In IE6, 7, 8 &amp;amp; Firefox, T2 can access the window opened by T1 and all clicks in T1,T2 reuse the same window.  &lt;span mce_name="em" mce_style="font-style: italic;"&gt;The bottom line is Chrome creates 2 windows for a scenario where every other browser does only one.&lt;/span&gt; You can probably argue, Chrome does it right and 2 pages from same domain in separate processes shouldn&amp;#8217;t be allowed to talk to each other.&lt;/p&gt;
&lt;p&gt;Well, in IE6 every window is a new process, but that allowed 2 pages from same domain use the same popup window. Ok, I cannot win any argument these days by quoting IE6. But IE8 has a multi process architecture too. It does it right.&lt;/p&gt;
&lt;p&gt;How does this affect you? It may not, but its good to know. Does this make me hate Chrome? Not at all.&lt;/p&gt;
&lt;p&gt;Is this a bug or its intentional behavior? Any thoughts?&lt;/p&gt;</description><link>http://www.aravindanr.com/post/9851796067</link><guid>http://www.aravindanr.com/post/9851796067</guid><pubDate>Thu, 15 Apr 2010 00:00:00 -0500</pubDate></item><item><title>Fixed Header Table using jQuery</title><description>&lt;p&gt;This is yet another &lt;a href="http://plugins.jquery.com/project/fixed_header_tables"&gt;jQuery plugin&lt;/a&gt; to provide fixed headers for tables. It differs in the way that it does not require any odd html table semantics. It just needs a TABLE tag with THEAD and TBODY to work its magic.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How it works?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Simple.  It takes a TABLE, pulls the THEAD and TBODY out of it and puts them into two separate  DIVs.  Now, the DIV which THEAD (header-div) goes is overflow restricted. &lt;!-- more --&gt;The DIV the TBODY (content-div) goes to allows vertical overflow. That&amp;#8217;s it. Since the content-div can have a vertical scroll bar,  the header-div cant. We have our table with fixed header.  Thats it. The plugin takes care of aligning the columns. So, as a plugin user, you dont have to worry about the alignment.&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Now, the gotchas. For extremely large tables (thousands of rows and tens of columns), you will see a delay in the page load, because the plugin rearranges the DOM. This doesnt mean that this plugin cant be used for large tables. There is no strict definition of &amp;#8220;large&amp;#8221;. So, dont hesitate to try it on a large table.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://plugins.jquery.com/project/fixed_header_tables"&gt;jQuery Plugin Site&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://moviechaat.com/aravindanr-blog/fixed_header_samples/fixed_header_test.html"&gt;Fixed Header Sample&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://moviechaat.com/aravindanr-blog/fixed_header_samples/fixed_header_multiple_tables.html"&gt;Fixed Header Multiple Tables&lt;/a&gt;&lt;/p&gt;</description><link>http://www.aravindanr.com/post/9851695731</link><guid>http://www.aravindanr.com/post/9851695731</guid><pubDate>Tue, 28 Jul 2009 00:00:00 -0500</pubDate></item><item><title>Auditing using Interceptors in Hibernate</title><description>&lt;p&gt;This would probably work only in Hibernate 3. Its loosely based on &lt;a href="https://www.hibernate.org/48.html"&gt;&lt;a href="https://www.hibernate.org/48.html"&gt;https://www.hibernate.org/48.html&lt;/a&gt;&lt;/a&gt;. I am showing a very basic example which sets the created time in a property when an entity is inserted and the updated time in another property when its updated.&lt;/p&gt;
&lt;p&gt;Hibernate allows interceptors to be registered and calls it when an entity&amp;#8217;s state changes during its lifecycle. Refer the &lt;a href="https://www.hibernate.org/hib_docs/v3/api/org/hibernate/Interceptor.html"&gt;Interceptor&lt;/a&gt; documentation for information on its methods. The method names give you an idea of when it gets called.&lt;/p&gt;
&lt;p&gt;&lt;!-- more --&gt;In order for the Interceptor to identify the entities to be audited, the entities implement an interface. Like this&lt;/p&gt;
&lt;p&gt;AuditStamp contains the audit information that is set or updated by the interceptor. It can range from simple datetime to user information. The AuditStamp class shown below just has dates, but more can be added as needed. The advantage of having it as a separate class is that when more audit information needs to be added, the entity implementing Auditable remains unaffected.&lt;/p&gt;
&lt;script src="https://gist.github.com/1196023.js?file=AuditStamp.java"&gt;&lt;/script&gt;&lt;p&gt;This AuditStamp class is mapped in Hibernate using a custom type&lt;/p&gt;
&lt;script src="https://gist.github.com/1196023.js?file=AuditStampType.java"&gt;&lt;/script&gt;&lt;p&gt;Refer the &lt;a href="https://www.hibernate.org/hib_docs/v3/api/org/hibernate/usertype/CompositeUserType.html"&gt;CompositeUserType&lt;/a&gt; documentation to see how this type is implemented. Now the Interceptor is also a minimal implementation, it can be enriched as needed.&lt;/p&gt;
&lt;script src="https://gist.github.com/1196023.js?file=AuditInterceptor.java"&gt;&lt;/script&gt;&lt;p&gt;Interceptors are little if you need to change the state of an entity. Even though the first parameter in onSave and onFlushDirty is the entity itself, changing the state of it does not seem to work. Instead the third parameter contains the values of all the properties in the entity in the same order as property names are specified in the fourth parameter.&lt;/p&gt;
&lt;p&gt;The setValue does the magic of finding the index of the property and setting the value.&lt;/p&gt;
&lt;p&gt;Interceptors come in two flavors: Session-scoped and SessionFactory-scoped.&lt;/p&gt;
&lt;p&gt;A Session-scoped interceptor is specified when a session is opened using one of the overloaded SessionFactory.openSession() methods accepting an Interceptor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Session session = sf.openSession( new AuditInterceptor() );&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;A SessionFactory-scoped interceptor is registered with the Configuration object prior to building the SessionFactory. In this case, the supplied interceptor will be applied to all sessions opened from that SessionFactory; this is true unless a session is opened explicitly specifying the interceptor to use. SessionFactory-scoped interceptors must be thread safe, taking care to not store session-specific state since multiple sessions will use this interceptor (potentially) concurrently.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;new Configuration().setInterceptor( new AuditInterceptor() );&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#8217;s it.&lt;/p&gt;
&lt;p&gt;The AuditStamp is specified in the hibernate mapping as a custom type. A typedef can be used as well.&lt;/p&gt;
&lt;script src="https://gist.github.com/1196023.js?file=audit_stamp.xml"&gt;&lt;/script&gt;</description><link>http://www.aravindanr.com/post/9851446724</link><guid>http://www.aravindanr.com/post/9851446724</guid><pubDate>Fri, 26 Jun 2009 00:00:00 -0500</pubDate><category>spring</category><category>hibernate</category></item><item><title>Authoring Custom Namespaces in Spring 2.0</title><description>&lt;p&gt;This post is a republish of the article I wrote for TheServerSide.com. You can find it &lt;a title="Authoring Custom Namespaces in Spring 2.0" target="_blank" href="http://www.theserverside.com/tt/articles/article.tss?l=CustomNamespacesSpring2"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Starting from version 2.0, Spring supports XML Namespaces. The purpose of this article is to discuss the new XML schema based configuration available in Spring 2.0. Familiarity with previous versions of Spring and basic AOP terms is assumed. Spring now supports and recommends usage of XML Schema rather than DTD. As users of previous versions, you might remember using the &amp;lt;bean&amp;gt; tag and its structure being declared in a DTD. Instead, this is now declared in a XML schema (spring-beans-2.0.xsd). Rest assured, the DTD configuration is 100% legal and is fully supported in Spring 2.0. DTD has its limitations and modern IDEs encourage using XML schema to fully utilize features like auto-complete that comes handy during XML authoring. In the old-fashioned &lt;em&gt;bean&lt;/em&gt; style configuration the developer has to know the name of the appropriate factory or proxy beans. Everything is a bean with a proxy or factory class and a different set of attributes. Transaction, caching, security etc are implemented using appropriate proxy classes and configured using the properties in XML. This makes the XML verbose and it also exposes the internal classes of Spring. These factory or proxy classes are never called in the code directly. It makes perfect sense to hide them from the developer – one of the reasons for the shift. Schema based configuration greatly reduces XML plumbing and eases authoring. &lt;span id="more-33"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;!-- more --&gt;An example would make this clear,&lt;/p&gt;
&lt;p&gt;This is how transaction management is done in earlier versions,&lt;/p&gt;
&lt;script src="https://gist.github.com/1195960.js?file=old_txn.xml"&gt;&lt;/script&gt;&lt;p&gt;Now,&lt;/p&gt;
&lt;script src="https://gist.github.com/1195960.js?file=new_txn.xml"&gt;&lt;/script&gt;&lt;p&gt;&lt;em&gt;tx&lt;/em&gt; is the namespace of the tags that declare the transaction manager ‘txManager’ as an advice. The &lt;em&gt;aop&lt;/em&gt; tags define the pointcut and the advisor which complete the aspect declaration. The &lt;a href="http://www.springframework.org/docs/reference/aop.html#aop-pointcuts-examples"&gt;execution expression&lt;/a&gt; used in the &lt;em&gt;expression&lt;/em&gt; attribute of&lt;em&gt; pointcut &lt;/em&gt;tag is explained in detail in the &lt;a href="http://www.springframework.org/docs/reference/aop.html#aop-pointcuts-examples"&gt;Spring documentation&lt;/a&gt;. Namespaces hide the internal classes from the developer. For the Spring IoC, everything is a bean and the namespaces are linked to beans using configuration, explained in a later section. The namespaces are fully customizable and extensible. Customizable – Additional attributes can be added to existing tags. Extensible – New set of tags can be developed and used in the configuration. Spring comes bundled with namespaces for common purposes like,&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;The &lt;em&gt;util&lt;/em&gt; tags deal with common, &lt;em&gt;utility&lt;/em&gt; configuration issues, such as configuring collections, referencing constants, and suchlike.&lt;/li&gt;
&lt;/ul&gt;&lt;script src="https://gist.github.com/1195960.js?file=util_example.xml"&gt;&lt;/script&gt;&lt;li&gt;The &lt;em&gt;jee&lt;/em&gt; tags deal with JEE (Java Enterprise Edition)-related configuration issues, such as looking up a JNDI object and defining EJB references.&lt;/li&gt;
&lt;script src="https://gist.github.com/1195960.js?file=jee_example.xml"&gt;&lt;/script&gt;&lt;li&gt;The &lt;em&gt;lang&lt;/em&gt; tags deal with exposing objects that have been written in a dynamic language such as JRuby or Groovy as beans in the Spring container.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;aop&lt;/em&gt; tags deal with configuring all things AOP in Spring: this includes Spring’s own proxy-based AOP framework and Spring’s integration with the AspectJ AOP framework.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;tool&lt;/em&gt; tags are for use when you want to add tooling-specific metadata to your custom configuration elements.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;beans&lt;/em&gt; tags that is present since the inception of the framework.&lt;/li&gt;
&lt;p&gt;A more detailed explanation of these tags is available in the Spring documentation (see Resources).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Custom Namespace&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Having said about the existing tags lets move on to the creation of custom ones. The creation is a four step process,&lt;/p&gt;
&lt;ol&gt;&lt;li&gt;Author an XML schema for the namespace.&lt;/li&gt;
&lt;li&gt;Code a &lt;em&gt;NamespaceHandler&lt;/em&gt; linking the namespace to JavaBean.&lt;/li&gt;
&lt;li&gt;Code a &lt;em&gt;BeanDefinitionParser&lt;/em&gt; that parses the XML and builds the JavaBean.&lt;/li&gt;
&lt;li&gt;Link the namespace to Spring i.e. make Spring aware of your namespace.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;The four step process is illustrated here with an example. Our custom namespace is called as &lt;em&gt;log&lt;/em&gt; and it exposes logging as an aspect. Spring AOP only supports method pointcuts, so our &lt;em&gt;log&lt;/em&gt; tags provide logging of method arguments, return values and thrown exceptions. All these are done by the tag and can be enabled for an application through configuration. Logging comes to the methods for free and not even a single line of code is necessary. This is quite similar to how transaction works as an aspect. Methods in the service won’t be aware that they are inside a transaction. Logging is also one of the many cross cutting concerns (&lt;em&gt;aspects&lt;/em&gt;) that an enterprise application has to deal with.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Authoring XML Schema&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For creating the schema, you need to have an idea of how the actual tags would look like. Our &lt;em&gt;log&lt;/em&gt; tags look like,&lt;/p&gt;
&lt;script src="https://gist.github.com/1195960.js?file=log.xml"&gt;&lt;/script&gt;&lt;p&gt;The &lt;em&gt;advice &lt;/em&gt;tag declares logging as an advice and assigns an id to it. This id is later used in the configuration while defining an aspect. The &lt;em&gt;args&lt;/em&gt; tag enables logging of method arguments with the level, if specified, returns and throws tag for return values and exceptions respectively. The level attribute is optional and defaults to INFO for args, return and ERROR for throws.&lt;/p&gt;
&lt;p&gt;The schema looks like this,&lt;/p&gt;
&lt;script src="https://gist.github.com/1195960.js?file=log.xsd"&gt;&lt;/script&gt;&lt;p&gt;The schema is self explanatory. Enumeration is used to restrict the values for the ‘level’ attribute. It only allows the standard logging levels INFO, TRACE, ERROR, FATAL, DEBUG and WARN.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Coding the &lt;em&gt;NamespaceHandler&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The namespace handler links the tags under the namespace to an XML parser (&lt;em&gt;BeanDefinitionParser&lt;/em&gt;). Whenever a tag is encountered in configuration, the Spring IoC calls the associated parser registered through the namespace handler. All namespace handlers need to implement the &lt;em&gt;NamespaceHandler&lt;/em&gt; interface. As you might expect, much of the mundane code required for implementing the interface is already done by the abstract class &lt;em&gt;NamespaceHandlerSupport&lt;/em&gt;. LogNamespaceHandler is the handler for the &lt;em&gt;log&lt;/em&gt; namespace. The method which we are concerned about is the &lt;em&gt;init&lt;/em&gt;.&lt;/p&gt;
&lt;script src="https://gist.github.com/1195960.js?file=LogNamespaceHandler.java"&gt;&lt;/script&gt;&lt;p&gt;&lt;strong&gt;Coding the BeanDefinitionParser&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As I already mentioned, everything is a bean to the Spring IoC. In any XML backed by a bean, the attributes in the XML map to the properties in the bean. The function of the parser is to get the values of attribute and populate them in the appropriate properties of the bean. The &lt;em&gt;BeanDefinitionParser&lt;/em&gt; does this indirectly; it doesn’t get direct access to the beans associated with the tag. Instead it builds a map of Javabean property names and values retrieved from the XML attributes called as a &lt;em&gt;BeanDefinition&lt;/em&gt;. After calling the parser, the Spring container populates the JavaBean using the &lt;em&gt;BeanDefinition(s)&lt;/em&gt;. A significant advantage of this approach is that the XML attribute names and JavaBean property names need not match.&lt;/p&gt;
&lt;p&gt;Now the question arises, how the container knows which JavaBean to populate. The answer is it doesn’t know we have to tell it using the &lt;em&gt;getBeanClass&lt;/em&gt; method in &lt;em&gt;BeanDefinitionParser&lt;/em&gt; interface. LogBeanDefinitionParser is the parser for our &lt;em&gt;advice&lt;/em&gt; tag,&lt;/p&gt;
&lt;script src="https://gist.github.com/1195960.js?file=BeanDefinitionParser.java"&gt;&lt;/script&gt;&lt;p&gt;The registered JavaBean for the &lt;em&gt;advice&lt;/em&gt; tag is SimpleLogger. Note that the &lt;em&gt;doParse&lt;/em&gt; method gets a reference to the &lt;em&gt;BeanDefinitionBuilder&lt;/em&gt; which is responsible for building the &lt;em&gt;BeanDefinition(s)&lt;/em&gt;. The SimpleLogger is defined as follows,&lt;/p&gt;
&lt;script src="https://gist.github.com/1195960.js?file=SimpleLogger.java"&gt;&lt;/script&gt;&lt;p&gt;Please see the highlighted code to get a glimpse of how &lt;em&gt;BeanDefinition&lt;/em&gt;s are built. If the &lt;em&gt;args&lt;/em&gt; tag is present then &lt;em&gt;argsLogEnabled&lt;/em&gt; is set to true. It’s the same for the &lt;em&gt;return&lt;/em&gt; and &lt;em&gt;throws&lt;/em&gt; tags. Similarly, if the ‘&lt;em&gt;level&lt;/em&gt;’ attribute is present, its set accordingly based on the tag it’s present in. For example, if it’s present for &lt;em&gt;args&lt;/em&gt; tag, value will be set in the &lt;em&gt;argLevel&lt;/em&gt; property.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Linking to Spring&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Here comes the configuration part. Don’t panic, there is no XML.We need to deal with two properties files, &lt;em&gt;spring.schemas&lt;/em&gt; and &lt;em&gt;spring.handlers.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;spring.schema specifies the location of the XML schema definition,&lt;/p&gt;
&lt;p&gt;&lt;em&gt;http\://www.theserverside.com/spring/log=SimpleLogger.xsd&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;spring.handlers links the namespace to the NamespaceHandler,&lt;/p&gt;
&lt;p&gt;&lt;em&gt;http\://www.theserverside.com/spring/log=com.spring.aop.test.LogNamespaceHandler&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Using the handlers file, Spring knows which &lt;em&gt;NamespaceHandler&lt;/em&gt; to call when &lt;em&gt;log &lt;/em&gt;is encountered in the configuration.&lt;/p&gt;
&lt;p&gt;Please note that these two files should be present in the META-INF directory of your JAR. In fact, if you open the spring.jar under the META-INF directory, you can see the details of all the schema and handlers for the namespaces that comes with Spring.&lt;/p&gt;
&lt;p&gt;That’s all! You can now package your classes, schema and properties files in a JAR. If this JAR is available in your CLASSPATH, you (or anyone) can use it as if its part of Spring.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;SimpleLogger – The Interceptor&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I can imagine you wondering where is the code for logging. The steps above are what Spring expects us to do for every custom namespace. As I mentioned earlier, the &lt;em&gt;log&lt;/em&gt; exposes logging as an aspect. For logging method arguments, our interceptor needs to implement &lt;em&gt;MethodBeforeAdvice&lt;/em&gt;, for return values &lt;em&gt;AfterReturningAdvice&lt;/em&gt; and for exceptions &lt;em&gt;ThrowsAdvice&lt;/em&gt;. I am using the SimpleLogger for implementing the interfaces.&lt;/p&gt;
&lt;script src="https://gist.github.com/1195960.js?file=LoggingInterceptor.java"&gt;&lt;/script&gt;&lt;p&gt;In AOP terms, an aspect consists of an advice and a pointcut. Our log namespace tags define the advice. We use the Spring aop tags to define the pointcut and henceforth the aspect.&lt;/p&gt;
&lt;script src="https://gist.github.com/1195963.js?file=log_tx.xml"&gt;&lt;/script&gt;&lt;p&gt;Note that the &lt;em&gt;tx&lt;/em&gt; example shown earlier resembles the above configuration. The pointcut is defined as all classes &lt;em&gt;within&lt;/em&gt; the class &lt;em&gt;com.spring.aop.test.ServiceImpl. &lt;/em&gt;Note that the id ‘logAdvice’ is referred in the &lt;em&gt;advice-ref &lt;/em&gt;of &lt;em&gt;advisor&lt;/em&gt; tag. The above configuration&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;Defines a fooService which is an instance of com.spring.aop.test.ServiceImpl&lt;/li&gt;
&lt;li&gt;Enables logging of arguments, return values and exceptions for all the methods of ‘fooService’.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;SimpleLogger component follows the Spring principle of eliminating cross cutting concerns from business objects. The fooService is a POJO and its methods are not aware that they are being logged. Using the execution expression, the pointcut can be customized and hence it’s possible to apply this logging advice to an entire application. This is how &lt;em&gt;tx&lt;/em&gt; tags work, methods in the DAO (or any service) wont be aware that they are inside a transaction.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Running the code&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The code requires the following libraries,&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;Spring 2.0&lt;/li&gt;
&lt;li&gt;Commons Logging&lt;/li&gt;
&lt;li&gt;Log4J&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Run the TestSimpleLogger for a demonstration. You can also add the JAR file to your CLASSPATH and use the SimpleLogger in your own applications. Please refer the spring.xml for a usage example.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Spring’s shift to XML schema configuration is a step in the right direction. It revolutionizes the way developers work with Spring and bootstraps a budding component market too.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;Download the &lt;a href="http://www.aravindanr.com/wp-content/uploads/2009/05/simplelogger.jar"&gt;Simple Logger&lt;/a&gt; source.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.springframework.org/docs/reference/xsd-config.html"&gt;XML Schema-based configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.springframework.org/docs/reference/extensible-xml.html"&gt;Extensible XML authoring&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.springframework.org/docs/reference/aop.html"&gt;Aspect Oriented Programming with Spring&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description><link>http://www.aravindanr.com/post/9693628062</link><guid>http://www.aravindanr.com/post/9693628062</guid><pubDate>Thu, 05 Apr 2007 00:00:00 -0500</pubDate><category>spring</category></item></channel></rss>

