title: Java Class loader issue and JaxB Exception in consuming the cxf generated client in OSGI
body: <p>I am newbie to CXF and JAXB. I doing a proof of concept as part of my Java project. Trying to generate client from wsdl to remove weblogic dependency in my application by replacing it with Apache CXF. My application is based on OSGI model and uses Adobe CQ 5.4 as a web tool for UI development. My application is deployed in Oracle weblogic server 10.3.4 and running with java 1.6.</p>
  
  <p>Used Ant 1.8 as my build script to generate the web service client and packaged those classes as a separate jar and added that in my bundle classpath.</p>
  
  <p>Used the below command in my ant build script</p>
  
  <pre><code>&lt;java classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="true"&gt;
  </code></pre>
  
  <p>to generate my client. I am using CXF of version 2.7.2.</p>
  
  <p>When I am invoking the CXF generated service class like below,</p>
  
  <p>QueryCustomerOutService queryCustomerIdentification = new QueryCustomerOutService();</p>
  
  <p>with the OSGI class loader with the java code as below:</p>
  
  <pre><code>ClassLoader osgiClassLoader = getClass().getClassLoader();
  
  ClassLoader weblogicClassLoader = Thread.currentThread().getContextClassLoader();
  
    try{
  Thread.currentThread().setContextClassLoader(osgiClassLoader);
  //invoking my service
  }
  finally{
    Thread.currentThread().setContextClassLoader(weblogicClassLoader);
  }
  </code></pre>
  
  <p>I am getting below Exception:</p>
  
  <p>org.apache.cxf.service.factory.ServiceConstructionException
      at org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:332)
      at org.apache.cxf.service.factory.AbstractServiceFactoryBean.initializeDataBindings(AbstractServiceFactoryBean.java:86)
      at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.buildServiceFromClass(ReflectionServiceFactoryBean.java:475)
      at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.initializeServiceModel(ReflectionServiceFactoryBean.java:537)
      at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.create(ReflectionServiceFactoryBean.java:249)
      at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpoint(AbstractWSDLBasedEndpointFactory.java:101)
      at org.apache.cxf.frontend.ClientFactoryBean.create(ClientFactoryBean.java:90)
      at org.apache.cxf.frontend.ClientProxyFactoryBean.create(ClientProxyFactoryBean.java:156)</p>
  
  <p>weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
      at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
      at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
      at weblogic.work.ExecuteThread.execute(ExecuteThread.java:207)
      at weblogic.work.ExecuteThread.run(ExecuteThread.java:176)
  <em>Caused by: javax.xml.bind.JAXBException: Provider com.sun.xml.bind.v2.ContextFactory not found
   - with linked exception:
  [java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory not found by [my bundle name][419]]</em>
      at javax.xml.bind.ContextFinder.loadSpi(ContextFinder.java:176)
      at javax.xml.bind.ContextFinder.find(ContextFinder.java:101)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:65)
      at org.apache.cxf.common.jaxb.JAXBContextCache$2.run(JAXBContextCache.java:267)
      at org.apache.cxf.common.jaxb.JAXBContextCache$2.run(JAXBContextCache.java:265)
      at java.security.AccessController.doPrivileged(Native Method)
      at org.apache.cxf.common.jaxb.JAXBContextCache.createContext(JAXBContextCache.java:265)
      at org.apache.cxf.common.jaxb.JAXBContextCache.getCachedContextAndSchemas(JAXBContextCache.java:172)
      at org.apache.cxf.jaxb.JAXBDataBinding.createJAXBContextAndSchemas(JAXBDataBinding.java:464)
      at org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:330)
      ... 156 more
  Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory not found by [my Bundle name][419]
      at org.apache.felix.framework.ModuleImpl.findClassOrResourceByDelegation(ModuleImpl.java:787)
      at org.apache.felix.framework.ModuleImpl.access$400(ModuleImpl.java:71)
      at org.apache.felix.framework.ModuleImpl$ModuleClassLoader.loadClass(ModuleImpl.java:1768)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
      at javax.xml.bind.ContextFinder.loadSpi(ContextFinder.java:171)
      ... 165 more</p>
  
  <p>I searched many contents and related articles in Internet and came to know about 2 things.</p>
  
  <ol>
  <li>CXF 2.7.2 uses 2.2 version of jaxws and jaxb as its binding. But my jdk 1.6 uses 2.1 version of jaxws and jaxb.</li>
  <li>This exception might be because of OSGI class loading issue.</li>
  </ol>
  
  <p>So with the information I got from internet, I tried following two approaches.</p>
  
  <ol>
  <li>Tried using CXF version 2.1 which supports 2.1 version  of JAXB. But still Iam getting the exception as above.</li>
  <li>Tried invoking the service using CXF bus factory class loader. But this approach also throws me a similar kind of JAXB exception but with even more errors with it related to XML annotation not being recognized.</li>
  </ol>
  
  <p>Whether my approach is wrong or am I missing some basic configuration in my application to make it work. How to consume the CXF generated services or client in my OSGI bundle deployed in weblogic server. </p>
  
  <p>I am using Apache Felix as my OSGI container and hence information on this would be more helpful than giving me on Apache karaf. Do i need to do configuration like placing cxf.xml, cxf-servlet.xml in my APP-INF for OSGI as we do it for spring application?[sources given in internet]</p>
  
  <p>I am struggling on this issue for almost a week. Any timely help is much appreciated. Thanks in advance.</p>