title: Servicemix - OSGi classloading issues with embedded dependencies
body: <p>I'm developing project with multiple OSGi bundles, deployed on ServiceMix (FuseESB compilation, v. 4.3.1). The issue is, one of this bundles is connecting to EJB on WebLogic, therefore it embeddes weblogic.jar.</p>
  
  <p>The solution is working, however a trick was required. The bundle exports Spring service via OSGi. This service is imported in another bundle, which is entry point to the system. When from this bundle the service was called, the weblogic classes were invisible. The working trick is to wrap Spring service in following aspect, which temporarly switches classloader:</p>
  
  <pre><code>    public Object profileInventory(ProceedingJoinPoint pjp) throws Throwable {
      Object output = null;
      ClassLoader clOld = Thread.currentThread().getContextClassLoader();
  
      try {
          Thread.currentThread().setContextClassLoader(pjp.getTarget().getClass().getClassLoader());
          output = pjp.proceed();
      } finally {
          Thread.currentThread().setContextClassLoader(clOld);
      }
      return output;
  }
  </code></pre>
  
  <p>As I have understood, the service is called with classloader from entry bundle, not with the classloader from bundle that embedds weblogic, and for this classloader embedded dependency classes are not visible. In similar case, exported Spring service can use private imports and private packages from its bundle, but with embedded jars it is not so.</p>
  
  <p>My question is: is the embedding jars something so specific, that this embedded classes will be visible only when the call originates from embedding bundle (or with classloader swich trick), or there is something more to specify when embedding bundle, something I have forgot to do?</p>
  
  <p>I'm using maven-bundle-plugin</p>
  
  <blockquote>
  <pre><code>        &lt;plugin&gt;
              &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
              &lt;artifactId&gt;maven-bundle-plugin&lt;/artifactId&gt;
              &lt;configuration&gt;
                  &lt;instructions&gt;
                      &lt;Bundle-Name&gt;${pom.artifactId}&lt;/Bundle-Name&gt;
                      &lt;Bundle-SymbolicName&gt;${pom.groupId}.${pom.artifactId}&lt;/Bundle-SymbolicName&gt;
                      &lt;Embed-Dependency&gt;
                   weblogic;scope=*,
                      &lt;/Embed-Dependency&gt;
  </code></pre>
  </blockquote>