title: Does classloading isolation work in jboss 5.1.x
body: <p>I have a pretty standard java application which uses maven some pretty common libraries including</p>
  
  <ul>
  <li>Spring</li>
  <li>Hibernate ORM</li>
  <li>Hibernate validator</li>
  <li>Log4j</li>
  </ul>
  
  <p>The application is composed of two war modules which share lot of common dependencies.</p>
  
  <p>During development we've used both jetty and tomcat and everything is working fine. Now we're about to deploy in production and we have some constraints on this environment: </p>
  
  <ul>
  <li>The application must be deployed in an already configured jboss 5.1.x instance which has a ton of libraries under both its $JBOSS_HOME/common/lib/ and $JBOSS_HOME/server//lib/, obviously there are lot of duplicates jar in these directories (especially log4j and hibernate validation stuff)</li>
  <li>We can't change JBoss configuration or libraries included</li>
  <li>We need to provide a single ear archive as final artifact to be deployed</li>
  <li>Some application code relies on newer libraries than the provided ones</li>
  </ul>
  
  <p>The final structure of the ear produced by maven is</p>
  
  <pre><code>application.ear
  ├── META-INF
  │   ├── application.xml
  │   └── jboss-classloading.xml
  ├── lib
  │   ├── long list of shared jars
  │   ├── ...
  │   ├── // some of key libraries which also are in AS lib folder
  │   ├── hibernate-validator-4.3.1.Final.jar
  │   ├── jaxb-impl-2.1.10.jar
  │   ├── log4j-1.2.17.jar
  │   ├── slf4j-api-1.5.5.jar
  │   ├── slf4j-log4j12-1.5.5.jar
  │   └── validation-api-1.0.0.GA.jar
  ├── webapp1.war
  └── webapp2.war
  </code></pre>
  
  <p>In our pom the only two artifacts marked as provided are these, so we need them provided in the container.</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
      &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
      &lt;version&gt;${servlet.version}&lt;/version&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;javax.xml.bind&lt;/groupId&gt;
      &lt;artifactId&gt;jaxb-api&lt;/artifactId&gt;
      &lt;version&gt;${jaxb.version}&lt;/version&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>However when we deploy the ear in jboss we encounter lot of classloading problems (I'll list some of them later). What we'd like to obtain is to have jboss load libraries in this order WAR->EAR->Server. In this way we can use newer libraries and safely exclude from deployment libraries shared with application server. To obtain this I created a maven jboss profile which list common libraries (log4j, slf4j, resteasy, and some other) as provided and include in the ear all the others including newer versions of jars present on the AS.</p>
  
  <p>After reading lot of posts about jboss classloading isolation in JBoss 5.x the confguration I'm trying is the one contained in <a href="http://phytodata.wordpress.com/2010/10/21/demystifying-the-jboss5-jboss-classloading-xml-file/" rel="nofollow noreferrer">this article</a> which is linked in lot of different sources and it's supposed to work.</p>
  
  <p>Another attempt I've made is to deploy as two WARs to see if something changes. In that case I'm using this configuration in one of the war modules and I'm trying to deploy only that war </p>
  
  <pre><code>&lt;classloading xmlns="urn:jboss:classloading:1.0"
                domain="myapp.war"
                parent-domain="DefaultDomain"
                parent-first="false"
                export-all="NON_EMPTY"
                top-level-classloader="true"
                import-all="true"&gt;
  &lt;/classloading&gt;
  </code></pre>
  
  <p>The error remain the same.</p>
  
  <p>I don't know how to do, also because I can't find any official documentation about <code>jboss-classloading.xml</code> file with parameters explained and concrete use cases.</p>
  
  <p>My question is does classloading isolation works in JBoss 5.1.x? Is it reliable? If yes how to obtain <code>WAR-&gt;EAR-&gt;Server</code> classloading? </p>
  
  <p>Alternatively how can I completely exclude server lib and use only JARs contained in my archive? In that case do I need to include also the original provided dependencies, i.e. servlet-api, jaxb-api and maybe xml-apis?</p>
  
  <h3>Classloading articles I've found</h3>
  
  <ul>
  <li><a href="https://access.redhat.com/site/documentation/en-US/JBoss_Enterprise_Web_Platform/5/html/JBoss_Microcontainer_User_Guide/sect-JBoss_Microcontainer_User_Guide-The_ClassLoading_Layer-ClassLoading.html" rel="nofollow noreferrer">https://access.redhat.com/site/documentation/en-US/JBoss_Enterprise_Web_Platform/5/html/JBoss_Microcontainer_User_Guide/sect-JBoss_Microcontainer_User_Guide-The_ClassLoading_Layer-ClassLoading.html</a></li>
  <li><a href="http://huima.wordpress.com/2011/09/16/having-fun-with-jboss-classloader-or-how-i-figured-out-how-to-get-my-application-to-work-in-jboss-5/" rel="nofollow noreferrer">http://huima.wordpress.com/2011/09/16/having-fun-with-jboss-classloader-or-how-i-figured-out-how-to-get-my-application-to-work-in-jboss-5/</a></li>
  <li><a href="http://thoughts.inphina.com/2010/09/02/classloading-isolation-issues-with-jboss-5-1/" rel="nofollow noreferrer">http://thoughts.inphina.com/2010/09/02/classloading-isolation-issues-with-jboss-5-1/</a></li>
  <li><a href="http://phytodata.wordpress.com/2010/10/21/demystifying-the-jboss5-jboss-classloading-xml-file/#comment-22" rel="nofollow noreferrer">http://phytodata.wordpress.com/2010/10/21/demystifying-the-jboss5-jboss-classloading-xml-file/#comment-22</a></li>
  </ul>
  
  <h3>Current deployment issues</h3>
  
  <p>With current config the first error we encounter is this one (stack trace omitted)</p>
  
  <pre><code>13:10:11,972 INFO  [STDOUT] 13:10:11,969 ERROR [ContextLoader] Context initialization failed
  org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.ClassCastException: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to javax.xml.parsers.DocumentBuilderFactory
  </code></pre>
  
  <p>By looking at <a href="https://stackoverflow.com/q/17360000/518204">some</a> <a href="https://community.jboss.org/thread/146600" rel="nofollow noreferrer">posts</a> we're able to solve this by putting this in our maven jboss profile</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;xml-apis&lt;/groupId&gt;
      &lt;artifactId&gt;xml-apis&lt;/artifactId&gt;
      &lt;version&gt;1.0.b2&lt;/version&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>After that we're getting lot of errors about instantiation of hibernate validator object which are declared as bean using this line in spring context</p>
  
  <pre><code>&lt;!-- Hibernate validator stuff --&gt;
  &lt;bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/&gt;
  </code></pre>
  
  <p>Here's the error</p>
  
  <pre><code>16:52:58,430 ERROR [ContextLoader] Context initialization failed
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'validator' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.jboss.logmanager.LogContext.getAttachment(Ljava/lang/String;Lorg/jboss/logmanager/Logger$AttachmentKey;)Ljava/lang/Object;
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
  </code></pre>
  
  <p>If I remove bundled version of hibernate validator I loose some useful validators used in application code and I'm getting errors about missing annotations</p>
  
  <pre><code>java.lang.NoClassDefFoundError: [Ljavax/validation/constraints/Pattern$Flag;
  </code></pre>
  
  <p>So I'm stuck with this problem, please advise.</p>