7 Replies Latest reply on Jun 10, 2013 3:45 AM by ampiefnb

    Using CDI in OSGi Bundles?

    thehashcoder

      Hi Everyone,

       

      I am trying to use CDI inside my OSGi bundles and I'm stuck. I am trying to use simple @Inject in my code (not @Publish, @OSGiService provided by Weld-OSGi). I have a very simple class ATM with one attribute Transport. Transport have 2 implementations SoapAtmTransport and StandardAtmTransport (@Default). My ATM class is as follows.

       

       

      public class AutomatedTellerMachine {
      
      
      
          @Inject
          private ATMTransport transport;
      
      
          public void deposit(BigDecimal bd) {
              System.out.println("deposit called");
              transport.communicateWithBank(null);
          }
      
          public void withdraw(BigDecimal bd) {
              System.out.println("withdraw called");
              transport.communicateWithBank(null);
          }
      
          public ATMTransport getTransport() {
              return transport;
          }
      
      
          public void setTransport( ATMTransport transport) {
              this.transport = transport;
          }
      
      }
      

       

      I am calling this class from my Activator.

       

       

      Below is my MANIFEST.mf

       

       

      Manifest-Version: 1.0
      Bundle-ManifestVersion: 2
      Bundle-Name: ATMBundle
      Bundle-SymbolicName: ATMBundle
      Bundle-Version: 1.0.0.qualifier
      Bundle-Activator: com.ctl.test.ATMActivator
      Import-Package: org.osgi.framework;version="1.3.0",
       javax.inject,javax.enterprise.inject
      Bundle-RequiredExecutionEnvironment: JavaSE-1.6
      

       

       

      I have added the following lines to the standalone.xml

       

       

      <property name="org.osgi.framework.system.packages.extra">
                          javax.inject,javax.enterprise.inject
      </property>
      

       

      But when I try to deploy my bundle I'm getting following error.

       

       

      20:27:55,612 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-4) JBAS015877: Stopped deployment ATMBundle.jar in 13ms
      20:28:35,597 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-1) JBAS015876: Starting deployment of "ATMBundle.jar"
      20:28:36,956 INFO  [org.jboss.weld.deployer] (MSC service thread 1-2) JBAS016002: Processing weld deployment ATMBundle.jar
      20:28:36,972 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-2) MSC000001: Failed to start service jboss.deployment.unit."ATMBundle.jar".POST_MODULE: org.jboss.msc.service.StartException in service jboss.deployment.unit."ATMBundle.jar".POST_
      MODULE: JBAS018733: Failed to process phase POST_MODULE of deployment "ATMBundle.jar"
              at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:123) [jboss-as-server-7.1.2.Final-redhat-1.jar:7.1.2.Final-redhat-1]
              at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA-redhat-1.jar:1.0.2.GA-redhat-1]
              at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA-redhat-1.jar:1.0.2.GA-redhat-1]
              at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0_23]
              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0_23]
              at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_23]
      Caused by: java.lang.NullPointerException
              at org.jboss.as.weld.deployment.processors.WeldPortableExtensionProcessor.deploy(WeldPortableExtensionProcessor.java:85)
              at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:116) [jboss-as-server-7.1.2.Final-redhat-1.jar:7.1.2.Final-redhat-1]
              ... 5 more
      
      20:28:37,003 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS015870: Deploy of deployment "ATMBundle.jar" was rolled back with failure message {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"ATMBundle.jar\".POST_MODULE" =>
       "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"ATMBundle.jar\".POST_MODULE: JBAS018733: Failed to process phase POST_MODULE of deployment \"ATMBundle.jar\"
          Caused by: java.lang.NullPointerException"}}
      20:28:37,050 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015877: Stopped deployment ATMBundle.jar in 49ms
      

       

      I have heard there is something called Weld-OSGi. But I couldn't find any resource which can help me to get this thing started.

       

      I'm attaching my project here along with source. Could some one please help me out onm this?

        • 1. Re: Using CDI in OSGi Bundles?
          alesj

          I have heard there is something called Weld-OSGi. But I couldn't find any resource which can help me to get this thing started.

          https://github.com/alesj/core/tree/osgi5

           

          I'll release this officially next week, under first Weld 1.2.x version.

          • 2. Re: Using CDI in OSGi Bundles?
            thehashcoder

            Hi Ales,

             

                 Thanks for the response.

             

                 So does this mean I CANNOT use CDI in OSGI bundles for the time being? Along with JBoss? I don't want to use Weld-OSGi specific annotations like @OSGiService,@Publish (or @Register) etc. Just want to use plain CDI like @Inject.

             

            Thanks

            Ranis MK

            • 3. Re: Using CDI in OSGi Bundles?
              thomas.diesler

              We have limited support for CDI/OSGi in the current AS7 master already

               

              https://github.com/tdiesler/jboss-as/tree/master/testsuite/integration/osgi/src/test/java/org/jboss/as/test/integration/osgi/cdi

               

              Weld/OSGi will also supports proxing of OSGi service. For that you'd have to wait a little.

              • 4. Re: Using CDI in OSGi Bundles?
                alesj

                I don't want to use Weld-OSGi specific annotations like @OSGiService,@Publish (or @Register) etc. Just want to use plain CDI like @Inject.

                Atm that's not the case.

                There was a huge discussion about this on the JBoss OSGi forums - pitch your comments, objections, etc there.

                It's not yet set in stone, so it might still be changed.

                • 5. Re: Using CDI in OSGi Bundles?
                  ampiefnb

                  Hi Thomas, Ales.

                  My apologies for reopening this old thread. I have just done a POC where we found Blueprint to be simply too restrictive for our requirements. We are hoping Weld-OSGI will be our answer, but I am really struggling to get any information on this. Do you have any updates for us, links I can go to or perhaps some early builds I can look at? I desperately need to give my manager some suggestions or we may have to give up on OSGI altogether which would be less than ideal. And after all the  XML I had to do for Blueprint I am simply not strong enough to handle this disappointment too .

                  • 6. Re: Using CDI in OSGi Bundles?
                    alesj

                    Weld 1.2.0.Beta1 includes CDI+OSGi pieces.

                    Or check Weld sources directly: https://github.com/weld/core/tree/1.2

                    • 7. Re: Using CDI in OSGi Bundles?
                      ampiefnb

                      Thanks Ales, I got a basic Weld-OSGI (1.2.0.Beta1)/JBoss-OSGI component working. However, before I go any further I just want to check about two things I had to do which worried me a bit:

                      1. I encountered a ClassCastException on the javassist Proxy class, which was the Weld subsytem in JBoss interfering with Weld-OSGI. I therefore disabled the Weld subsystem.

                      2. I encountered a ClassNotFoundError:org.jboss.weld.environment.osgi.impl.extension.service.WeldOSGiExtension in  org.jboss.weld.environment.osgi.impl.integration.Weld.initialize(). I then manually added an import to this package in the weld-osgi-core-integration manifest.

                      Am I missing something or did I take the correct steps?