0

I have got the standard bndtools plugin for eclipse installed, up and also running the apache felix osgi runtime. I am trying to learn about declarative services (DS) components. Apparently, before there were annotations (for which an example is given in the tutorial for bndtools), components were written using xml data. That is what I am trying to do.

Here is the simple class (which will be published as a DS component): "HelloComponent.java"

package org.osgi.book.ds.minimal;

public class HelloComponent {
    public HelloComponent(){
        System.out.println("HelloComponent created.");
    }
}

Here is the xml file that makes a component declaration: "minimal.xml"

<?xml version="1.0" encoding="UTF-8"?>
<!-- minimal.xml -->

<scr:component xmlns:scr="http://www.osgi.org/xlmns/scr/v1.1.0" immediate="true">
    <implementation class="org.osgi.book.ds.minimal.HelloComponent"/>
</scr:component>

And here is the .bnd file that's supposed to be used by the bndtools to generate the jar files which will eventually be published to the OSGi runtime: "minimal_ds.bnd"

Private-Package: org.osgi.book.ds.minimal

Include-Resource: minimal.xml
Service-Component: minimal.xml

Note that I have the following bundles up and running in the host runtime OSGi container:

   ID|State      |Level|Name
    0|Active     |    0|System Bundle (4.4.1)
    2|Active     |    1|Apache Felix Gogo Command (0.14.0)
    3|Active     |    1|Apache Felix Gogo Runtime (0.12.1)
    4|Active     |    1|Apache Felix Gogo Shell (0.10.0)
    5|Active     |    1|BookReaderOSGiInPractice.minimal_ds (0.0.0.201509091856)
   15|Active     |    1|Apache Felix Configuration Admin Service (1.8.0)
   16|Active     |    1|Apache Felix Declarative Services (1.8.2)
   17|Active     |    1|osgi.enterprise (4.2.0.201003190513)
   18|Active     |    1|osgi.residential (4.3.0.201111022239)

Despite everything being actively running, I can't figure out why the DS component is not being initialized (to which I should see the console output: "HelloComponent created."). Any help is appreciated.

Finally, here is the project directory structure:

BookReaderInPractice
|
|- src
|   |- org.osgi.book.ds.minimal
|           |_ HelloComponent.java
|
|- minimal_ds.bnd
|
|- minimal.xml
apil.tamang
  • 2,545
  • 7
  • 29
  • 40

1 Answers1

1

Update (Edited):

updated as suggested by Neil Bartlett: Turns out the answer was even simpler: as written in my comment, there was a typo in the xml namespace of the DS xml file: "xlm" instead of "xml".

Original answer:

I guess there are two things going wrong here:

  1. the file minimal.xml is not copied into the generated bundle jar (located in the "generated" folder)
  2. the framework does not know about minimal.xml

To fix this, put the following lines into minimal_ds.bnd:

Include-Resource: minimal.xml
Service-Component: minimal.xml

Furthermore, instead of using the constructor of HelloComponent, create a method like this which will be called when activating the component:

public void activate() {...}
evandor
  • 799
  • 1
  • 10
  • 23
  • The lines you recommended are already included in minimal_ds.bnd. Furthermore, when I view the generated jar, minimal.xml is also included, alongwith the java src package. Using the activate method did not output any console messages either. Thanks for trying though... – apil.tamang Sep 10 '15 at 13:50
  • Oops... completely missed that, sorry. But I spotted a typo in your xml namespace of minimal.xml now: try "http://www.osgi.org/xmlns/scr/v1.1.0" and it will work ;) – evandor Sep 10 '15 at 14:03
  • @evandor I recommend editing your answer to include this actual solution, to help people reading this page in future. – Neil Bartlett Sep 10 '15 at 16:44