<?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: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-8988872</id><updated>2011-07-08T09:14:40.506-07:00</updated><category term='xml'/><category term='acegi'/><category term='marshalling'/><category term='javascript'/><category term='java'/><category term='jaxb'/><category term='ajax'/><category term='object'/><category term='cloning'/><category term='aop'/><category term='serialization'/><category term='jcl'/><category term='jvm'/><category term='cas'/><category term='object clone'/><category term='jar'/><category term='deep copy'/><category term='log4j'/><category term='dwr'/><category term='clone'/><category term='classloader'/><category term='guice'/><category term='struts'/><category term='copy'/><category term='deep'/><category term='spring'/><category term='html'/><category term='class'/><category term='jboss'/><category term='jmx'/><category term='xstream'/><category term='json'/><category term='google'/><category term='ioc'/><title type='text'>Terra Java</title><subtitle type='html'>Welcome to my blog, I am a Sun certified Java/J2EE professional and specialize in various Open source software technologies. This blog has some Java/J2EE tips and some solutions to the problems I have resolved.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://terrajava.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://terrajava.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Kamran Zafar</name><uri>http://www.blogger.com/profile/11912759447062753547</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>11</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8988872.post-6499472272907819104</id><published>2009-10-14T03:41:00.000-07:00</published><updated>2009-10-14T04:35:55.183-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='log4j'/><title type='text'>Log4J: Emailing specific errors only</title><content type='html'>Log4J's SMTPAppender provides enough basic functionality to send out error messages as emails. Although emailing error messages is not always a good idea, unless something really goes wrong in system. Sometimes it is required to send out only specific errors in specific areas of the system. It is possible to do that with the SMTPAppender, even if these specific errors lie in the same category(WARN, ERROR etc.) as other errors. This is achieved with &lt;a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/spi/TriggeringEventEvaluator.html"&gt;&lt;i&gt;TriggeringEventEvaluator&lt;/i&gt;&lt;/a&gt;. Here is a sample config for the SMTPAppender.&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="xml"&gt;&lt;br /&gt;    &amp;lt;appender name="EMAIL" class="org.apache.log4j.net.SMTPAppender"&amp;gt;&lt;br /&gt;        &amp;lt;param name="To" value="kamran.zafar@xeustechnologies.org" /&amp;gt;&lt;br /&gt;        &amp;lt;param name="From" value="server-errors@xeustechnologies.org" /&amp;gt;&lt;br /&gt;        &amp;lt;param name="Subject" value="SERIOUS ERROR" /&amp;gt;&lt;br /&gt;        &amp;lt;param name="SMTPHost" value="mysmtphost" /&amp;gt;&lt;br /&gt;        &amp;lt;param name="SMTPUsername" value="server-errors" /&amp;gt;&lt;br /&gt;        &amp;lt;param name="SMTPPassword" value="password" /&amp;gt;&lt;br /&gt;        &amp;lt;param name="Threshold" value="ERROR" /&amp;gt;&lt;br /&gt;        &amp;lt;param name="BufferSize" value="1" /&amp;gt;&lt;br /&gt;        &amp;lt;param name="EvaluatorClass" value="org.xeustechnologies.test.log4j.SmtpTrigger" /&amp;gt;&lt;br /&gt;        &amp;lt;layout class="org.apache.log4j.PatternLayout"&amp;gt;&lt;br /&gt;            &amp;lt;param name="ConversionPattern" value="[%d{dd MMM yyyy HH:mm:ss.SSS}][%p][%t][%c] - %m%n%n" /&amp;gt;&lt;br /&gt;        &amp;lt;/layout&amp;gt;&lt;br /&gt;    &amp;lt;/appender&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The parameter EvaluatorClass is where we specify the implementation of TriggeringEventEvaluator, which acts like a filter to allow emailing only specific errors:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="java"&gt;&lt;br /&gt;package org.xeustechnologies.test.log4j;&lt;br /&gt;&lt;br /&gt;import org.apache.log4j.Level;&lt;br /&gt;import org.apache.log4j.spi.LoggingEvent;&lt;br /&gt;import org.apache.log4j.spi.TriggeringEventEvaluator;&lt;br /&gt;&lt;br /&gt;public class SmtpTrigger implements TriggeringEventEvaluator {&lt;br /&gt;&lt;br /&gt;    public boolean isTriggeringEvent(LoggingEvent event) {&lt;br /&gt;        /*&lt;br /&gt;         * Email errors&lt;br /&gt;         */&lt;br /&gt;        if( event.getLoggerName().equals( "SomeSeriousErrorLogger" )&lt;br /&gt;                &amp;&amp; event.getLevel().equals( Level.ERROR ) ) {&lt;br /&gt;            return true;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        return false;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now only ERRORs from "SomeSeriousErrorLogger" will be emailed.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988872-6499472272907819104?l=terrajava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://terrajava.blogspot.com/feeds/6499472272907819104/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988872&amp;postID=6499472272907819104' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/6499472272907819104'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/6499472272907819104'/><link rel='alternate' type='text/html' href='http://terrajava.blogspot.com/2009/10/log4j-emailing-specific-errors.html' title='Log4J: Emailing specific errors only'/><author><name>Kamran Zafar</name><uri>http://www.blogger.com/profile/11912759447062753547</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988872.post-7366375809532374705</id><published>2009-08-19T08:59:00.000-07:00</published><updated>2009-08-19T09:31:10.266-07:00</updated><title type='text'>Merging arrays in Java</title><content type='html'>I was playing around with java arrays and ran into a problem where I wanted to merge smaller arrays into a single big array. So the first thing I thought was to make a new array of size equal to the total length of all smaller arrays and then populating the values using loops. Then I went on to make a generic method to merge Generic arrays of same type but I realized that I cannot create a Generic array. So I thought of another way to merge Generic arrays using &lt;span style="font-style: italic;"&gt;List&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="java"&gt;&lt;br /&gt;  public &amp;lt;T&amp;gt T[] merge(T[]... arrays) {&lt;br /&gt;      List&amp;lt;T&amp;gt list = new ArrayList&amp;lt;T&amp;gt();&lt;br /&gt;&lt;br /&gt;      for( T[] array : arrays )&lt;br /&gt;          list.addAll( Arrays.asList( array ) );&lt;br /&gt;&lt;br /&gt;      return list.toArray( (T[]) Array.newInstance( arrays[0][0].getClass(), list.size() ) );&lt;br /&gt;  }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This code here will merge any number of generic arrays into one single array. The last line gives an unchecked cast warning, but can be ignored because we already assumed that the arrays are of same type.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988872-7366375809532374705?l=terrajava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://terrajava.blogspot.com/feeds/7366375809532374705/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988872&amp;postID=7366375809532374705' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/7366375809532374705'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/7366375809532374705'/><link rel='alternate' type='text/html' href='http://terrajava.blogspot.com/2009/08/merging-arrays-in-java.html' title='Merging arrays in Java'/><author><name>Kamran Zafar</name><uri>http://www.blogger.com/profile/11912759447062753547</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988872.post-6293938212990223209</id><published>2009-05-08T03:32:00.000-07:00</published><updated>2009-05-08T16:59:28.108-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='log4j'/><category scheme='http://www.blogger.com/atom/ns#' term='jboss'/><title type='text'>Resolving log4j.dtd error on JBoss</title><content type='html'>I recently faced an issue where the application was not able to find the log4j.dtd, when deployed on jboss. We were using our own repository selector for logging and an isolated classloader. But could see a "FileNotFoundException" for log4j.dtd in the JBoss log when the application was deployed, although the dtd was part of the log4j jar contained within the application. The reason for that was the xml parser was not looking in the classpath resources for the dtd, but was just looking in a path relative to the application's root directory.&lt;br /&gt;&lt;br /&gt;Now in order to load the dtd from the classpath I updated the repository selector and set an EntityResolver to the DocumentBuilder so that when the parser encounters the dtd reference the resolver looks it up from the classpath. Here is the code to that.&lt;br /&gt;&lt;br /&gt;Assuming that the doctype reference in log4j.xml is:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="xml"&gt;&lt;br /&gt;&amp;lt!DOCTYPE log4j:configuration SYSTEM "org/apache/log4j/xml/log4j.dtd"&amp;gt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The code to override the default behavior of the parser to look for the dtd in the classpath:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="java"&gt;&lt;br /&gt;import java.io.InputStream;&lt;br /&gt;import javax.xml.parsers.DocumentBuilder;&lt;br /&gt;import javax.xml.parsers.DocumentBuilderFactory;&lt;br /&gt;import org.w3c.dom.Document;&lt;br /&gt;import org.xml.sax.EntityResolver;&lt;br /&gt;import org.xml.sax.InputSource;&lt;br /&gt;.&lt;br /&gt;.&lt;br /&gt;.&lt;br /&gt;DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();&lt;br /&gt;DocumentBuilder builder = factory.newDocumentBuilder();&lt;br /&gt;builder.setEntityResolver( new EntityResolver() {&lt;br /&gt;    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {&lt;br /&gt;        // Check for dtd ref&lt;br /&gt;        if( systemId.endsWith( "org/apache/log4j/xml/log4j.dtd" ) ) {&lt;br /&gt;            // return the dtd from classpath&lt;br /&gt;            return new InputSource( getClass().getClassLoader().getResourceAsStream( "org/apache/log4j/xml/log4j.dtd" ) );&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        // Resume normal flow&lt;br /&gt;        return null;&lt;br /&gt;    }&lt;br /&gt;} );&lt;br /&gt;&lt;br /&gt;// now parse the log4j.xml&lt;br /&gt;Document doc = builder.parse( "WEB-INF/classes/log4j.xml" );&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now our EntityResolver will return the dtd from classpath whenever the parser encounters it. Although I wish the parser could support the "classpath" protocol in the doctype uri.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988872-6293938212990223209?l=terrajava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://terrajava.blogspot.com/feeds/6293938212990223209/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988872&amp;postID=6293938212990223209' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/6293938212990223209'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/6293938212990223209'/><link rel='alternate' type='text/html' href='http://terrajava.blogspot.com/2009/05/log4jdtd-error-on-jboss.html' title='Resolving log4j.dtd error on JBoss'/><author><name>Kamran Zafar</name><uri>http://www.blogger.com/profile/11912759447062753547</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988872.post-5632877825379656214</id><published>2009-04-24T05:42:00.000-07:00</published><updated>2009-05-07T03:57:35.143-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='jmx'/><category scheme='http://www.blogger.com/atom/ns#' term='guice'/><category scheme='http://www.blogger.com/atom/ns#' term='google'/><category scheme='http://www.blogger.com/atom/ns#' term='jboss'/><title type='text'>Guice and JMX on JBoss</title><content type='html'>It is very easy to use Guice's runtime injector bindings to register MBeans on any instance of mbean server. Lets look at a simple example of a HelloMBean. In this example a simple MBean is created and registered on JBoss using google-guice runtime injection.&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="java"&gt;&lt;br /&gt;  /**&lt;br /&gt;   * Hello MBean&lt;br /&gt;   */&lt;br /&gt;   public interface HelloMBean{&lt;br /&gt;      public String sayHello(String name);&lt;br /&gt;   }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The implementation of this MBean needs to have a method for the injection of the MBeanServer.&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="java"&gt;&lt;br /&gt;  /**&lt;br /&gt;   * HelloMBean implementation&lt;br /&gt;   */&lt;br /&gt;   import javax.management.JMException;&lt;br /&gt;   import javax.management.MBeanServer;&lt;br /&gt;   import javax.management.ObjectName;&lt;br /&gt;   import com.google.inject.Inject;&lt;br /&gt;&lt;br /&gt;   public class Hello implements HelloMBean{&lt;br /&gt;      public String sayHello(String name){&lt;br /&gt;         return "Hello " + name;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      /**&lt;br /&gt;       * This method is used to inject an MBeanServer instance&lt;br /&gt;       * that will be used to register this MBean&lt;br /&gt;       */&lt;br /&gt;      @Inject&lt;br /&gt;      public void register(MBeanServer server){&lt;br /&gt;         try {&lt;br /&gt;             server.registerMBean( this, new ObjectName( "Hello:type=Hello" ) );&lt;br /&gt;         } catch (JMException e) {&lt;br /&gt;             e.printStactTrace();&lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now all that needs to be done is creating an &lt;span style="font-style: italic;"&gt;Injector&lt;/span&gt; with bean and server bindings. The following piece of code can be used in any class (like a Servlet) to register the MBean when the application is deployed or as part of your injection bootstrap.&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="java"&gt;&lt;br /&gt;import javax.management.MBeanServer;&lt;br /&gt;import org.jboss.mx.util.MBeanServerLocator;&lt;br /&gt;import com.google.inject.AbstractModule;&lt;br /&gt;import com.google.inject.Guice;&lt;br /&gt;import com.google.inject.tools.jmx.Manager;&lt;br /&gt;.&lt;br /&gt;.&lt;br /&gt;.&lt;br /&gt;Manager.manage( "ExampleMBean", Guice.createInjector( new AbstractModule() {&lt;br /&gt;    @Override&lt;br /&gt;    protected void configure() {&lt;br /&gt;        // Bind the Jboss MBean server instance&lt;br /&gt;        bind( MBeanServer.class ).toInstance( MBeanServerLocator.locateJBoss() );&lt;br /&gt;        bind( HelloMBean.class ).to( Hello.class ).asEagerSingleton();&lt;br /&gt;    }&lt;br /&gt;} ) );&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;You can also use "&lt;span style="font-style: italic;"&gt;ManagementFactory.getPlatformMBeanServer()&lt;/span&gt;" to get/create an MBeanServer. Unfortunately this method cannot be used to get/locate the current JBoss MBeanServer, so "&lt;span style="font-style: italic;"&gt;MBeanServerLocator&lt;/span&gt;" in the last code snippet above is the only way to locate a JBoss MBean Server instance. After the bean is registered you can use jboss jmx-console or jdk's jconsole to access the mbean.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988872-5632877825379656214?l=terrajava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://terrajava.blogspot.com/feeds/5632877825379656214/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988872&amp;postID=5632877825379656214' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/5632877825379656214'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/5632877825379656214'/><link rel='alternate' type='text/html' href='http://terrajava.blogspot.com/2009/04/guice-and-jmx-on-jboss.html' title='Guice and JMX on JBoss'/><author><name>Kamran Zafar</name><uri>http://www.blogger.com/profile/11912759447062753547</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988872.post-3403099370823270648</id><published>2009-04-03T08:23:00.000-07:00</published><updated>2009-04-03T17:26:10.986-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='aop'/><title type='text'>RegEx based Spring AOP auto-proxy creator</title><content type='html'>When using Spring AOP in a medium-large application to add behavior to objects using crosscutting, it is desirable to have auto-proxy creators rather then creating a proxy for each bean (Spring AOP is &lt;a href="http://static.springframework.org/spring/docs/2.5.x/reference/aop.html#aop-understanding-aop-proxies"&gt;proxy based&lt;/a&gt;). When auto-proxying a subset of beans, BeanNameAutoProxyCreator is most commonly used. But it has certain limitations, you have to provide a whole list of beanNames, which in a fairly large application becomes cumbersome and sometimes hard to maintain. Now because the application we were using had fairly well categorized beans with similar IDs, it was convenient to use a pattern or regular expression to identify them, but BeanNameAutoProxyCreator doesn't support regular expressions. So I extended the BeanNameAutoProxyCreator to provide regular expression support.&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="java"&gt;&lt;br /&gt;&lt;br /&gt;package xeus.spring.aop;&lt;br /&gt;&lt;br /&gt;import java.util.ArrayList;&lt;br /&gt;import java.util.List;&lt;br /&gt;import java.util.regex.Matcher;&lt;br /&gt;import java.util.regex.Pattern;&lt;br /&gt;&lt;br /&gt;import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;&lt;br /&gt;import org.springframework.beans.BeansException;&lt;br /&gt;import org.springframework.beans.factory.annotation.Autowired;&lt;br /&gt;import org.springframework.context.ApplicationContext;&lt;br /&gt;import org.springframework.context.ApplicationContextAware;&lt;br /&gt;&lt;br /&gt;public class RegExpBeanNameAutoProxyCreator extends BeanNameAutoProxyCreator implements ApplicationContextAware {&lt;br /&gt;&lt;br /&gt;  private ApplicationContext applicationContext;&lt;br /&gt;&lt;br /&gt;   @Autowired&lt;br /&gt;   public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {&lt;br /&gt;    this.applicationContext = applicationContext;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public void setPattern(String pattern) {&lt;br /&gt;&lt;br /&gt;    String beans[] = applicationContext.getBeanDefinitionNames();&lt;br /&gt;    List&amp;lt;String&amp;gt; proxiedBeans = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;&lt;br /&gt;    Pattern pat = Pattern.compile( pattern, Pattern.CASE_INSENSITIVE );&lt;br /&gt;    for( String bean : beans ) {&lt;br /&gt;        Matcher matcher = pat.matcher( bean );&lt;br /&gt;        if( matcher.find() ) {&lt;br /&gt;            proxiedBeans.add( bean );&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    setBeanNames( proxiedBeans.toArray( new String [proxiedBeans.size()] ) );&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This auto-proxy creator takes a regular expression and matches all the beans available in the Spring Application Context to create implicit runtime proxies for the matched beans. The bean definition for RegExpBeanNameAutoProxyCreator is similar to BeanNameAutoProxyCreator but it takes a "pattern" rather then the "beanNames".&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="xml"&gt;&lt;br /&gt;&lt;br /&gt;&lt;bean class="xeus.spring.aop.RegExpBeanNameAutoProxyCreator"&gt;&lt;br /&gt;    &lt;property name="pattern"&gt;&lt;br /&gt;        &lt;!-- all beans ending with Impl except the ones with "NoProxy" in their names. --&gt;&lt;br /&gt;        &lt;value&gt;^(?!.*(NoProxy)).*Impl$&lt;/value&gt;&lt;br /&gt;    &lt;/property&gt;&lt;br /&gt;    &lt;property name="interceptorNames"&gt;&lt;br /&gt;        &lt;list&gt;&lt;br /&gt;            &lt;value&gt;someInterceptor&lt;/value&gt;&lt;br /&gt;        &lt;/list&gt;&lt;br /&gt;    &lt;/property&gt;&lt;br /&gt;&lt;/bean&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Here the "pattern" property takes the regular expression to match the beans available in spring context. It is pretty handy if you don't want to put the whole list of beanNames here; you can also use this to exclude beans as illustrated by the regular expression in this example.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988872-3403099370823270648?l=terrajava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://terrajava.blogspot.com/feeds/3403099370823270648/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988872&amp;postID=3403099370823270648' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/3403099370823270648'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/3403099370823270648'/><link rel='alternate' type='text/html' href='http://terrajava.blogspot.com/2009/04/regex-based-spring-auto-proxy-creator.html' title='RegEx based Spring AOP auto-proxy creator'/><author><name>Kamran Zafar</name><uri>http://www.blogger.com/profile/11912759447062753547</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988872.post-8138489188405404154</id><published>2009-03-27T08:49:00.001-07:00</published><updated>2009-03-28T12:15:45.716-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='javascript'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='dwr'/><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='ajax'/><category scheme='http://www.blogger.com/atom/ns#' term='acegi'/><title type='text'>Handling DWR Session timeouts</title><content type='html'>I recently had an issue in an application using Acegi and DWR, where even after the session invalidation the DWR calls could still be made. It seemed DWR calls were ignoring session timeouts. In order to over come this issue I wrote a little Ajax filter to check for session invalidation for DWR calls and throw an Exception if session has timed out. Throwing exception is more of a natural way of handling such scenarios then sending "plain/text" messages as &lt;a href="http://directwebremoting.org/dwr/changelog/dwr20"&gt;recommended&lt;/a&gt; in DWR 2 API.&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="java"&gt;&lt;br /&gt;package myApp;&lt;br /&gt;&lt;br /&gt;import java.lang.reflect.Method;&lt;br /&gt;import org.directwebremoting.AjaxFilter;&lt;br /&gt;import org.directwebremoting.AjaxFilterChain;&lt;br /&gt;import org.directwebremoting.WebContextFactory;&lt;br /&gt;import org.directwebremoting.extend.LoginRequiredException;&lt;br /&gt;&lt;br /&gt;public class DwrSessionFilter implements AjaxFilter {&lt;br /&gt;    public Object doFilter(Object obj, Method method, Object[] params, AjaxFilterChain chain) throws Exception {&lt;br /&gt;&lt;br /&gt;        //Check if session has timedout/invalidated&lt;br /&gt;        if( WebContextFactory.get().getSession( false ) == null ) {&lt;br /&gt;            //Throw an exception&lt;br /&gt;            throw new LoginRequiredException( "This operation requires login." );&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        return chain.doFilter( obj, method, params );&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Then attach this filter with DWR bean:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="xml"&gt;&lt;br /&gt;&lt;bean id="myBean" class="myPackage.MyBean"&gt;&lt;br /&gt;     &lt;dwr:remote javascript="myBean"&gt;&lt;br /&gt;       &lt;dwr:include method="doSomething"&gt;&lt;/dwr:include&gt;&lt;br /&gt;       &lt;dwr:filter class="myApp.DwrSessionFilter"&gt;&lt;/dwr:filter&gt;&lt;br /&gt;     &lt;/dwr:remote&gt;&lt;br /&gt;&lt;/bean&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In version 3.0RC-2 or later it is possible to have a global filter (&lt;a href="http://bugs.directwebremoting.org/bugs/browse/DWR-336"&gt;&amp;lt;dwr:global-filter&amp;gt;&lt;/a&gt;) that can be used by all DWR beans.&lt;br /&gt;&lt;br /&gt;Now after this all you need to do is catch the thrown LoginRequiredException on the client side.&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="js"&gt;&lt;br /&gt;dwr.engine.setErrorHandler(errorHandler);&lt;br /&gt;&lt;br /&gt;function errorHandler(message, exception){&lt;br /&gt;    //Session timedout/invalidated&lt;br /&gt;    if(exception &amp;amp;&amp;amp; exception.javaClassName&lt;br /&gt;             == 'org.directwebremoting.extend.LoginRequiredException'){&lt;br /&gt;        //Reload or display an error etc.&lt;br /&gt;        document.location.reload();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Here reloading the page was helpful because we were using Acegi (now known as spring security) and we had the FilterSecurityInterceptor setup to take care of the request. DWR guys also recommend something similar but that requires reverse Ajax and it also is not a natural way of handling this.&lt;br /&gt;&lt;br /&gt;In Acegi it is also possible to have method-level role-based authorization for DWR calls using the MethodSecurityInterceptor. I will try to explain that in the next blog entry.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988872-8138489188405404154?l=terrajava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://terrajava.blogspot.com/feeds/8138489188405404154/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988872&amp;postID=8138489188405404154' title='10 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/8138489188405404154'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/8138489188405404154'/><link rel='alternate' type='text/html' href='http://terrajava.blogspot.com/2009/03/handling-dwr-session-timeouts.html' title='Handling DWR Session timeouts'/><author><name>Kamran Zafar</name><uri>http://www.blogger.com/profile/11912759447062753547</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>10</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988872.post-5374435432181035354</id><published>2009-02-19T04:55:00.000-08:00</published><updated>2009-02-19T05:45:37.831-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='javascript'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='struts'/><category scheme='http://www.blogger.com/atom/ns#' term='html'/><title type='text'>Disabled &lt;select&gt; binding problem</title><content type='html'>There is common problem while designing the frontends; sometimes the business logic requires the html fields to be readonly. Now the &lt;span style="font-style: italic;"&gt;select&lt;/span&gt; html element doesn't have a "readonly" attribute, which forces the user to disable the select element. This causes another problem in frameworks like struts, spring-mvc etc where the fields are bound with the object model. The value of disabled fields will not be posted to the server and hence will not be bound with the model. There is a work around using javascript to simulate a readonly &lt;span style="font-style: italic;"&gt;select&lt;/span&gt; box:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="js"&gt;&lt;br /&gt;var selIndex;&lt;br /&gt;function setupReadonly(selbox){&lt;br /&gt;   selIndex=selbox.selectedIndex;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function startReadonly(selbox){&lt;br /&gt;   selbox.options[selIndex].selected=true;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;And in the plain html or spring-mvc etc. code do:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="html"&gt;&lt;br /&gt;    &lt;form:select path="color" onfocus="setupReadonly(this)" onchange="startReadonly(this)"&gt;&lt;br /&gt;         &lt;form:options items="${colors}"&gt;&lt;/form:options&gt;&lt;br /&gt;    &lt;/form:options&gt;&lt;br /&gt;&lt;/form:select&gt;&lt;/pre&gt;&lt;br /&gt;This will retain the initial value of the select box even if the user tries to change it.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988872-5374435432181035354?l=terrajava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://terrajava.blogspot.com/feeds/5374435432181035354/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988872&amp;postID=5374435432181035354' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/5374435432181035354'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/5374435432181035354'/><link rel='alternate' type='text/html' href='http://terrajava.blogspot.com/2009/02/disabled-binding-issue.html' title='Disabled &amp;lt;select&amp;gt; binding problem'/><author><name>Kamran Zafar</name><uri>http://www.blogger.com/profile/11912759447062753547</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988872.post-8991203894350901878</id><published>2008-04-01T03:51:00.000-07:00</published><updated>2008-04-01T05:27:02.984-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='marshalling'/><category scheme='http://www.blogger.com/atom/ns#' term='serialization'/><category scheme='http://www.blogger.com/atom/ns#' term='xml'/><category scheme='http://www.blogger.com/atom/ns#' term='json'/><category scheme='http://www.blogger.com/atom/ns#' term='jaxb'/><category scheme='http://www.blogger.com/atom/ns#' term='xstream'/><title type='text'>XML Serialization of Java Objects</title><content type='html'>Sometimes it is necessary to stream objects in a standard way like XML or JSON. There are different ways of doing so. Below I will take an example of XStream to illustrate how Java Objects can be serialized into XML.&lt;br /&gt;&lt;a href="http://xstream.codehaus.org/"&gt;&lt;br /&gt;&lt;b&gt;XStream&lt;/b&gt;&lt;/a&gt; is an open source, a very light weight and easy to use API to Serialize Java Objects into XML and back. Below is a simple example:&lt;br /&gt;&lt;pre name="code" class="java"&gt;&lt;br /&gt;import com.mypack.Person;&lt;br /&gt;&lt;br /&gt;//The object to serialize&lt;br /&gt;Person p = new Person();&lt;br /&gt;p.setName("Kamran");&lt;br /&gt;p.setAddress("Lahore, Pakistan");&lt;br /&gt;&lt;br /&gt;//Serializing p&lt;br /&gt;XStream xstream = new XStream(new PureJavaReflectionProvider(), new DomDriver());&lt;br /&gt;System.out.println(xstream.toXML(p));&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The above code will serialize the object of type Person to the following XML. There are other &lt;i&gt;toXML(..)&lt;/i&gt; methods available to write the XML to a Writer and OutputStream.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;&amp;lt;com.mypack.Person&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt; &amp;lt;name&amp;gt;&lt;/span&gt;Kamran&lt;span style="color: rgb(0, 153, 0);"&gt;&amp;lt;/name&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt; &amp;lt;address&amp;gt;&lt;/span&gt;Lahore, Pakistan&lt;span style="color: rgb(0, 153, 0);"&gt;&amp;lt;/address&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;&amp;lt;/com.mypack.Person&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The above XML can be deserialized back to the Java object. The following &lt;i&gt;fromXML(..)&lt;/i&gt; method is overloaded, see the API for more details.&lt;br /&gt;&lt;pre name="code" class="java"&gt;&lt;br /&gt;import com.mypack.Person;&lt;br /&gt;&lt;br /&gt;String xml = "&lt;...&gt;"; //The above xml&lt;br /&gt;&lt;br /&gt;//Deserializing back to Person&lt;br /&gt;XStream xstream = new XStream(new PureJavaReflectionProvider(), new DomDriver());&lt;br /&gt;Person p = (Person) xstream.fromXML(xml);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Another approach is using &lt;a href="http://java.sun.com/developer/technicalArticles/WebServices/jaxb/"&gt;&lt;b&gt;JAXB&lt;/b&gt;&lt;/a&gt;, a Java-XML binding API. JAXB uses XML Schemas to generate Java classes; the objects of these generated artifacts can then be serialized to XML.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988872-8991203894350901878?l=terrajava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://terrajava.blogspot.com/feeds/8991203894350901878/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988872&amp;postID=8991203894350901878' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/8991203894350901878'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/8991203894350901878'/><link rel='alternate' type='text/html' href='http://terrajava.blogspot.com/2008/04/xml-serialization-of-java-objects.html' title='XML Serialization of Java Objects'/><author><name>Kamran Zafar</name><uri>http://www.blogger.com/profile/11912759447062753547</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988872.post-5078511476995728821</id><published>2008-03-28T05:15:00.000-07:00</published><updated>2008-03-31T07:24:54.175-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='copy'/><category scheme='http://www.blogger.com/atom/ns#' term='object'/><category scheme='http://www.blogger.com/atom/ns#' term='deep copy'/><category scheme='http://www.blogger.com/atom/ns#' term='deep'/><category scheme='http://www.blogger.com/atom/ns#' term='cloning'/><category scheme='http://www.blogger.com/atom/ns#' term='object clone'/><category scheme='http://www.blogger.com/atom/ns#' term='clone'/><title type='text'>Deep cloning of Serializable objects</title><content type='html'>The following code snippet shows how to use Object I/O streams to deep clone Serializable objects. The object bytes once written on an ObjectOutputStream can be read back to a new object with all the populated values intact. The only catch is that, because the object is written and then read from a stream, this process is slower then the normal getter-setter approach.&lt;br /&gt;&lt;pre name="code" class="java"&gt;&lt;br /&gt;import java.io.ByteArrayInputStream;&lt;br /&gt;import java.io.ByteArrayOutputStream;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.io.ObjectInputStream;&lt;br /&gt;import java.io.ObjectOutputStream;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * Deep-copy of  Serializable objects&lt;br /&gt; */&lt;br /&gt;public class SerializableObjectCloner{&lt;br /&gt;&lt;br /&gt; public static Object clone(Object original) {&lt;br /&gt;  Object clone = null;&lt;br /&gt;  try {&lt;br /&gt;     //Increased buffer size to speed up writing&lt;br /&gt;     ByteArrayOutputStream bos = new ByteArrayOutputStream(5120);&lt;br /&gt;     ObjectOutputStream out = new ObjectOutputStream(bos);&lt;br /&gt;     out.writeObject(original);&lt;br /&gt;     out.flush();&lt;br /&gt;     out.close();&lt;br /&gt;&lt;br /&gt;     ObjectInputStream in = new ObjectInputStream(&lt;br /&gt;             new ByteArrayInputStream(bos.toByteArray()));&lt;br /&gt;     clone = in.readObject();&lt;br /&gt;&lt;br /&gt;     in.close();&lt;br /&gt;     bos.close();&lt;br /&gt;&lt;br /&gt;     return clone;&lt;br /&gt;  } catch (IOException e) {&lt;br /&gt;     e.printStackTrace();&lt;br /&gt;  } catch (ClassNotFoundException cnfe) {&lt;br /&gt;     cnfe.printStackTrace();&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  return null;&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988872-5078511476995728821?l=terrajava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://terrajava.blogspot.com/feeds/5078511476995728821/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988872&amp;postID=5078511476995728821' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/5078511476995728821'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/5078511476995728821'/><link rel='alternate' type='text/html' href='http://terrajava.blogspot.com/2008/03/deep-cloning-of-serializable-objects.html' title='Deep cloning of Serializable objects'/><author><name>Kamran Zafar</name><uri>http://www.blogger.com/profile/11912759447062753547</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988872.post-2156679730726054803</id><published>2007-09-14T04:44:00.000-07:00</published><updated>2008-03-31T07:26:07.710-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='cas'/><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='ioc'/><category scheme='http://www.blogger.com/atom/ns#' term='acegi'/><category scheme='http://www.blogger.com/atom/ns#' term='jboss'/><title type='text'>Cas-Acegi-Spring integration on JBoss</title><content type='html'>CAS and Acegi are popular open source authentication and identity solutions available for enterprise java applications. CAS is a Centralized Authentication Server used for single sign-on and to decouple authentication mechanism from applications. Acegi is an authentication API used to put role-based access control on application. This is a simple example that shows how to integrate CAS and Acegi using Spring IoC container in a JBoss Application Server.&lt;br /&gt;&lt;br /&gt;This example was tested on JBoss 4.0.4. See the readme file for more details.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.geocities.com/xeus_man/acegi-cas-demo.zip"&gt;Download Example Source&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988872-2156679730726054803?l=terrajava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://terrajava.blogspot.com/feeds/2156679730726054803/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988872&amp;postID=2156679730726054803' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/2156679730726054803'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/2156679730726054803'/><link rel='alternate' type='text/html' href='http://terrajava.blogspot.com/2007/09/cas-acegi-spring-integration-on-jboss.html' title='Cas-Acegi-Spring integration on JBoss'/><author><name>Kamran Zafar</name><uri>http://www.blogger.com/profile/11912759447062753547</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988872.post-116706272659566712</id><published>2006-12-25T07:50:00.000-08:00</published><updated>2008-03-31T07:29:07.291-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='jcl'/><category scheme='http://www.blogger.com/atom/ns#' term='jar'/><category scheme='http://www.blogger.com/atom/ns#' term='class'/><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='classloader'/><category scheme='http://www.blogger.com/atom/ns#' term='jvm'/><title type='text'>Loading Classes directly from JAR files</title><content type='html'>&lt;p&gt;Whenever a class is referenced in a java program it is loaded using JVM's bootstrap class loader. This often becomes a problem when two  different classes with same name and same package declaration are to be loaded.  For example relying on JVM's class loader one cannot load two different versions  of the same JDBC driver. So how to get around this problem? The answer lies in  making a custom class loader and loading classes directly from JAR archives. See the code snippet below:&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;pre name="code" class="java"&gt;&lt;br /&gt;import java.net.*;&lt;br /&gt;&lt;br /&gt;URL url=new URL("jar:file:/c:\\myJars\\myJar1.jar!/");&lt;br /&gt;URLClassLoader ucl = URLClassLoader(new URL[] { url });&lt;br /&gt;Object myClassObject =&lt;br /&gt;Class.forName("MyClass", true, ucl).newInstance();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;This example illustrates how to load java classes from their respective jar  files using URLClassLoader. There are other APIs also available for this purpose like &lt;a href="http://jcloader.sourceforge.net/"&gt;Jar Class Loader (JCL)&lt;/a&gt;, which also provides Spring integration.&lt;br /&gt;&lt;br /&gt;Tip By: &lt;a href="http://xeustech.blogspot.com/"&gt;Kamran Zafar&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988872-116706272659566712?l=terrajava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://terrajava.blogspot.com/feeds/116706272659566712/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988872&amp;postID=116706272659566712' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/116706272659566712'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988872/posts/default/116706272659566712'/><link rel='alternate' type='text/html' href='http://terrajava.blogspot.com/2006/12/loading-classes-directly-from-jar.html' title='Loading Classes directly from JAR files'/><author><name>Kamran Zafar</name><uri>http://www.blogger.com/profile/11912759447062753547</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
