Hi Jõrg,

You appear to be mixing APIs. With SCR u no longer use an BundleActivator. In 
this case the Activator is called before the SCR life cycle completes and the 
reference is available.. You should replace your start method with an activate 
method that is also annotated with @Activate. 

The activate method is called once the dependencies are satisfied. 

Look up the doco on activate, deactivate and modified for more details. 

Best Regards,
Scott ES

Scott England-Sullivan
Principal Consultant
FuseSource
Web: fusesource.com
Twitter: sully6768

On Aug 30, 2012, at 6:28 AM, Jörg Wille <[email protected]> wrote:

> I am trying out a really easy declarative OSGi example with Netbeans and
> the Maven SCR Plugin following this
> tutorial<http://netbeans.org/kb/docs/javaee/maven-osgi-declarativeservices.html>(
> http://netbeans.org/kb/docs/javaee/maven-osgi-declarativeservices.html).
> 
> 
> Here is my code (Here <http://www.uploadmb.com/dw.php?id=1346326024> is a
> link for both NetBeans projects):
> 
> 1.) Netbeans Maven OSGi Bundle Project "MavenHelloService OSGi Bundle"
> 
>     1.a) Interface HelloService.java in com.joerg.mavenhelloservice.api
> 
>      public interface HelloService {
> 
>          public String sayHello(String name);
> 
>      }
> 
>     1.b) Installer.java
> 
>      public class Installer implements BundleActivator {
> 
>          @Override
> 
>           public void start(BundleContext context) throws Exception {
> 
>               String userName = context.getProperty("user.name");
> 
>               System.out.println("User Name: " + userName);
> 
>           }
> 
>        …
> 
>     1.c) Implematation HelloImpl.java
> 
>      public class Installer implements BundleActivator {
> 
>          @Component(name="hello-service")
> 
>          @Service
> 
>          public class HelloImpl implements HelloService {
> 
>              @Override
> 
>              public String sayHello(String name) {
> 
>                  return "Hello " + name;
> 
>              }
> 
>          }
> 
> 
> 
> 2.) Netbeans Maven OSGi Bundle Project "HelloClient OSGi Bundle"
> 
>     2.a) Activator.java
> 
>      @Component(name="hello-service-consumer")
> 
>      public class Activator implements BundleActivator {
> 
>          @Reference
> 
>          private HelloService helloService;
> 
>          @Override
> 
>          public void start(BundleContext context) throws Exception {
> 
>              System.out.println(helloService.sayHello("Duke"));
> 
>          }
> 
>        …
> 
> 
> 3.) The Maven SCR Plugins generates following XML Files in the jars.
> 
> 3.a) In MavenHelloService-1.0-SNAPSHOT.jar serviceComponents.xml:
> 
> <?xml version="1.0" encoding="UTF-8"?><components xmlns:scr="
> http://www.osgi.org/xmlns/scr/v1.0.0";>
> 
>    <scr:component enabled="true" name="hello-service">
> 
>        <implementation class="com.joerg.mavenhelloservice.HelloImpl"/>
> 
>        <service servicefactory="false">
> 
>            <provide
> interface="com.joerg.mavenhelloservice.api.HelloService"/>
> 
>        </service>
> 
>        <property name="service.pid" value="hello-service"/>
> 
>    </scr:component>
> 
> </components>
> 
> 3.b) In HelloClient-1.0-SNAPSHOT.jar serviceComponents.xml:
> 
> <?xml version="1.0" encoding="UTF-8"?><components xmlns:scr="
> http://www.osgi.org/xmlns/scr/v1.0.0";>
> 
>    <scr:component enabled="true" name="hello-service-consumer">
> 
>        <implementation class="com.joerg.helloclient.Activator"/>
> 
>        <property name="service.pid" value="hello-service-consumer"/>
> 
>        <reference name="helloService"
> interface="com.joerg.mavenhelloservice.api.HelloService" cardinality="1..1"
> policy="static" bind="bindHelloService" unbind="unbindHelloService"/>
> 
>    </scr:component>
> 
> </components>
> 
> 
> I use following software versions:
> 
> Netbeans 7.2 on Mac OS X 10.8.1 with Oracle Java 1.7.0_06 (64bit)
> 
> Maven 4.17.1 (installed with Netbeans)
> 
> Apache Felix Framework 3.0.7 (as Dependency for Service and Client)
> 
> Apache Felix Annotations 1.6.0 (as Dependency for Service and Client)
> 
> Maven SCR Plugin 1.7.4 (added to pom.xml for Service and Client)
> 
> Apache Felix Framework 4.0.3 (on Terminal as OSGi playground)
> 
> 
> In Terminal I start felix "java -jar /bin/felix.jar" and install+start
> bundles:
> 
> start
> file:/Volumes/workspace/MavenHelloService/target/MavenHelloService-1.0-SNAPSHOT.jar
> 
> start
> file:/Volumes/workspace//HelloClient/target/HelloClient-1.0-SNAPSHOT.jar
> 
> 
> When starting the HelloClient I get:
> 
> java.lang.NullPointerException
> 
> at com.joerg.helloclient.Activator.start(Activator.java:17)
> 
> at
> org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:645)
> 
> at org.apache.felix.framework.Felix.activateBundle(Felix.java:1977)
> 
> 
> Where Activator.java:17 is:
> System.out.println(helloService.sayHello("Duke"));
> 
> 
> Am I missing anything? Or are my scr declarations wrong?

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to