title: OSGi on JBoss EAP 6
body: <p>I have an existing ear application that makes no use of OSGi in any way running on a jboss server that currently has no OSGi configured.</p>
  
  <p>Now I need to plug in a number of external libraries that use OSGi (declaratively!) for service setup so I need to enable the OSGi container in the configuration and somehow make my ear application aware of them.</p>
  
  <p>To enable the OSGi container, I copied the relevant (or so I hope) parts of the example configuration file: <code>standalone-osgi-only.xml</code> (including the logger) but currently I see no additional output when loading the server (subsystem is set to eager).</p>
  
  <p>I have moved the bundles into a custom folder in the "bundles" directory but am not sure if this is the correct place for them.</p>
  
  <p>Additionally I am not sure how to configure my ear file to "see" the bundles. Do I need to add something to the <code>jboss-app.xml</code> or <code>jboss-deployment-structure.xml</code> or can it simply see all the OSGi bundles?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The code in the ear file does not actually need access to any of the OSGi services, it needs access to a specific class that is in one of the libraries, let's call it "MyClass". So the code in the ear file does:</p>
  
  <pre><code>MyClass myClass = new MyClass();
  myClass.doSomething();
  </code></pre>
  
  <p>The class MyClass however uses a ton of OSGi-managed services to accomplish the task in doSomething().</p>
  
  <p>I have loaded all the libraries in a test OSGi environment and the test runs ok.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Ok, I have successfully enabled OSGi on the jboss server by adding the following to standalone.xml:</p>
  
  <pre><code>&lt;extensions&gt;
      &lt;extension module="org.jboss.as.configadmin"/&gt;
      &lt;extension module="org.jboss.as.osgi"/&gt;
  &lt;/extensions&gt;
  </code></pre>
  
  <p>And</p>
  
  <pre><code>    &lt;subsystem xmlns="urn:jboss:domain:configadmin:1.0"&gt;
          &lt;configuration pid="org.apache.felix.webconsole.internal.servlet.OsgiManager"&gt;
              &lt;property name="manager.root" value="jboss-osgi"/&gt;
          &lt;/configuration&gt;
      &lt;/subsystem&gt;
      &lt;subsystem xmlns="urn:jboss:domain:osgi:1.2" activation="eager"&gt;
          &lt;properties&gt;
              &lt;property name="org.jboss.osgi.system.modules.extra"&gt;
                  org.apache.log4j
              &lt;/property&gt;
              &lt;property name="org.osgi.framework.startlevel.beginning"&gt;
                  1
              &lt;/property&gt;
              &lt;property name="org.osgi.framework.system.packages.extra"&gt;
                  org.apache.log4j;version=1.2
              &lt;/property&gt;
          &lt;/properties&gt;
          &lt;capabilities&gt;
              &lt;capability name="javax.servlet.api:v25"/&gt;
              &lt;capability name="javax.transaction.api"/&gt;
              &lt;capability name="javax.xml.bind.api"/&gt;
              &lt;capability name="javax.validation.api2"/&gt;
              &lt;capability name="org.apache.felix.log" startlevel="1"/&gt;
              &lt;capability name="org.jboss.osgi.logging" startlevel="1"/&gt;
              &lt;capability name="org.apache.felix.configadmin" startlevel="1"/&gt;
              &lt;capability name="org.jboss.as.osgi.configadmin" startlevel="1"/&gt;
          &lt;/capabilities&gt;
      &lt;/subsystem&gt;
  </code></pre>
  
  <p>On the first try I was getting WARN logs like this:</p>
  
  <pre><code>[org.jboss.as.osgi] (MSC service thread 1-6) JBAS011910: Cannot resolve requirements: 
      [XPackageRequirement[atts={osgi.wiring.package=javax.xml.bind.annotation},
      [mybundle:1.0.0]]]
  </code></pre>
  
  <p>Even though these were warnings, they would prevent the application from booting, to fix this I added the necessary "root" dependencies (so to speak) by adjusting the "capabilities" bit of the OSGi subsystem. I have added the module javax.xml.bind.api which contains all the JAXB stuff that the frameworks need and I have installed a new module javax.validation.api2 which basically contains version 1.1 (needed by the frameworks) instead of the provided-by-default version 1.0.</p>
  
  <p>In my application.ear file I have added the 10 or so jars containing the OSGi bundles to the root folder. I get the following log for each jar inside the ear: <code>org.jboss.osgi.framework] (MSC service thread 1-3) JBOSGI011001: Bundle installed: mybundle</code></p>
  
  <p>So basically everything looks OK from the OSGi perspective <strong>but</strong> my actual application still doesn't see the libraries. The ear looks like this:</p>
  
  <ul>
  <li>META-INF
  <ul>
  <li>application.xml</li>
  </ul></li>
  <li>mybundle1.jar</li>
  <li>mybundle2.jar</li>
  <li>mybundle3.jar</li>
  <li>MyEJB.jar</li>
  </ul>
  
  <p>The application.xml dutifully contains the ejb entry for MyEJB.jar, however the EJB code can not actually see the code of the bundles, it will throw ClassNotFoundException.</p>
  
  <p>If I move all my bundles to the <code>lib</code> folder inside the ear file, my EJB code will find the classes, but no OSGi magic happens so the jars don't find one another.</p>
  
  <p>So now I'm trying to figure out how to have the OSGi modules loaded <strong>and</strong> visible for my EJB.</p>
  
  <p><strong>UPDATE 3</strong></p>
  
  <p>I have added all the bundles to the <code>META-INF/application.xml</code> file as "java" modules, however this does something weird.</p>
  
  <p>The package <code>MyEJB</code> has a direct code dependency to say <code>bundle1</code> but <strong>not</strong> to any other bundles. Now <code>bundle1</code> in turn has a code dependency to other bundles and those bundles to yet other bundles etc.</p>
  
  <p>With the jars in my application.xml, it no longer throws an exception that it can not find the class in <code>bundle1</code> (yay!) buuut...now I get a ClassNotFoundException from <code>bundle2</code> which is referenced by <code>bundle1</code>...</p>