You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Jörg Wille <jo...@gmail.com> on 2012/08/30 13:28:38 UTC

OSGi newbie struggles with NPE

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?

Re: OSGi newbie struggles with NPE

Posted by Jörg Wille <jo...@gmail.com>.
Hi Brian,
thank you for fast reply.

2012/8/30 Brian Topping <to...@codehaus.org>

> Congrats on getting this far, you are probably very close.
>
> Make sure your bundles are both installed and started.  I don't know the
> commands in Felix to check this, but you should be able to install the
> webconsole at the very least to see it.
>
> The exception gets thrown when starting the 2'nd bundle. In Felix you can
install+start bundles with:
start file:/Volumes/workspace/MavenHelloService/target/MavenHelloService-1.0-SNAPSHOT.jar
start file:/Volumes/workspace//HelloClient/target/HelloClient-1.0-SNAPSHOT.jar


> Next, be sure that the Service Component Runtime has found your
> configurations and properly acted on them.  I use Blueprint and am not sure
> of the SCR specifics, but there should be some way to ensure that SCR is
> started on those bundles (or find out why it hasn't).
>
>
It has started, because scr generates "serviceComponents.xml" files in both
bundles. I have included contents of both in my previous mail.


> If you are still having problems at that point, it would help with the
> exception to know where "line 17" is in your code listings.
>

> Where Activator.java:17 is:
> System.out.println(helloService.sayHello("Duke"));


>
> Brian
>
>
I have attached the Netbeans projects. Maybe you can have a look - I tried
many different scr annotations and combinations.
The generated files look reasonable. But when starting in felix it always
crashes.

Thanks,
Joerg


>  On Aug 30, 2012, at 2:28 PM, Jörg Wille <jo...@gmail.com> 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: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>
>

Re: OSGi newbie struggles with NPE

Posted by Brian Topping <to...@codehaus.org>.
Congrats on getting this far, you are probably very close.

Make sure your bundles are both installed and started.  I don't know the commands in Felix to check this, but you should be able to install the webconsole at the very least to see it.  

Next, be sure that the Service Component Runtime has found your configurations and properly acted on them.  I use Blueprint and am not sure of the SCR specifics, but there should be some way to ensure that SCR is started on those bundles (or find out why it hasn't). 

If you are still having problems at that point, it would help with the exception to know where "line 17" is in your code listings.  

Brian

On Aug 30, 2012, at 2:28 PM, Jörg Wille <jo...@gmail.com> 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: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: OSGi newbie struggles with NPE

Posted by Scott England-Sullivan <su...@gmail.com>.
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 <jo...@gmail.com> 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: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org