Using CDI in OSGi Bundles?
thehashcoder Oct 5, 2012 11:20 AMHi 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?