开发者

get list of services implementations with OSGi declarative services

开发者 https://www.devze.com 2023-04-10 21:17 出处:网络
I have a very simple example of declarative services. I\'m following this tutorial http://www.eclipsezone.com/eclipse/forums/t97690.html?start=0. Every thing is working as expected. However, I cannot

I have a very simple example of declarative services. I'm following this tutorial http://www.eclipsezone.com/eclipse/forums/t97690.html?start=0. Every thing is working as expected. However, I cannot figure out how I can make the "SampleImporter" (which is the bundle that is expected to use other bundles' services) aware of the list of "SampleExporter" (bundle providing a service). In other words, I want the "SamlpeImporter" to see the ID of the bundle(s) that it is eventually using. This information is very useful for my application.

here is the XML file for SampleExporter:

<?xml version="1.0"?>
<component name="samplerunnable">
<implementation class="org.example.ds.SampleRunnable"/>
<property name="ID" value="expoter" />
<service>
<provide interface="java.lang.Runnable"/>
</service>

while for the SampleImporter:

<?xml version="1.0"?>
<component name="commandprovider1">
<implementation class="org.example.ds.SampleCommandProvider1"/>
<service>
<provide interface="org.eclipse.osgi.framework.console.CommandProvider"/>
</service>
<reference name="RUNNABLE"
    interface="java.lang.Runnable"
    bind="setRunnable"
    unbind="unsetRunnable"
    cardinality="0..1"
    policy="dynamic"/>
</component>

In the Importer side, I have the following function:

public class SampleCommandProvider1 implements CommandProvider {
    private Runnable runnable;
public synchronized void setRunnable(Runnable r) {
    runnable = r;
}
public synchronized void unsetRunnable(Runnable r) {
    runnable = null;
}
public synchronized void _run(CommandInterpreter ci) {
    if(runnable != null) {
            runnable.run();
    } else {
        ci.println("Error, no Runnable available");
    }
}
public String getHelp() {
    return "\trun - execute a Runnable service";
}

}

This works fine but then if I want to get the value of the property, using

public synchronized void setRunnable(Runnable r, Map properties)

or

public synchronized void setRunnable(Runnable r, ServiceReference reference)

the method run of the exporter is never called which means that the bind func开发者_StackOverflowtion (setRunnable is not called).Hwever, using the console command "services" I see that the exporter bundle is used by the imporeter one. Also, using ss and ls I can see that the component eporter is "satisfied". What is wrong with my implementetion?

Thanks in advance

Cheers

Marie


The following bind signature is not supported by any version of DS:

public void setRunnable(Runnable r, ServiceReference ref)

Instead you will have to take only the ServiceReference and use either the ComponentContext or BundleContext to access the service instance object.

Alternatively if you want a more POJO-style way of accessing service properties, the following bind signature is allowed in DS 1.1 (but not in DS 1.0):

public void setRunnable(Runnable r, Map properties)

To access DS 1.1 features, you need to add the correct namespace to your XML as follows:

<component xmlns='http://www.osgi.org/xmlns/scr/v1.1.0' name='...'>

By the way, I wrote this original article a very long time ago! These days I would use bnd annotations to avoid having to write the XML document by hand.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号