views:

96

answers:

2

My sample classes:

public class MyModel implements Model
{
    :
    :
}

public class SingleModelProvider implements ModelProvider
{
    public SingleModelProvider(Model providedModel, List actions)
    {
          :
    }
}

The plan is to reuse the SingleModelProvider class in several bundles to provide different implementations of the ModelProvider. What I need to accomplish in each bundle is to simply instantiate the SingleModelProvider with the appropriate parameters to the constructor. A pretty simple scenario using any DI framework. I would like to register the ModelProvider services using DS (Declarative Services) if possible without having to write the boilerplate code in an Activator.

Is this possible?

I can't seem to find any documentation on how to accomplish this since the class declaration in DS does not seem to allow for constructor arguments (or setters for that matter).

Do I use a factory? I am not sure if that is worthwhile since it may make the case no simpler than using an Activator and publishing service manually.

A: 

Is there a specific reason why you want to use DS?

You could also use the OSGI Blueprint services as described in the OSGI Service Compendium version 4.2, 121. It provides the best of two worlds: DI and easy service publishing / consuming.

In DS the only option as far as i know is to use the factory, the bind / unbind methods don't accept user classes. (As described in the OSGI Service Compendium version 4.2, 112.4.5)

davyM
I came across that yesterday after asking this question and it looks very promising. We were using Spring DM but stopped using it in favor of something supported natively in the platform. This looks like the best alternative since it does appear to meet all my needs. Do you know if the latest version of Eclipse 3.5.x supports this revision.
Robin
Well, in fact OSGI BluePrint Services is based on Spring DM, and Spring DM will be one of the reference implementations (See http://static.springsource.org/osgi/docs/2.0.0.M1/reference/html-single/#blueprint).Blueprint services works with the extender pattern, so you only need to have a bundle with blueprint implementation, it is not coupled to eclipse.The following article will give you a quick start, so you can judge for yourself: http://www.ibm.com/developerworks/opensource/library/os-osgiblueprint/index.html
davyM
+1  A: 

DS does support setters. Here is an example of the DS xml based on the example in your question.

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" name="SampleModelProvider">
   <implementation class="test.SampleModelProvider"/>
   <reference bind="setModel" cardinality="1..1" interface="test.Model" name="Model" policy="static" unbind="unsetModel"/>
   <reference bind="setList" cardinality="1..1" interface="test.ActionList" name="ActionList" policy="static" unbind="unsetList"/>
   <service>
      <provide interface="test.ModelProvider"/>
   </service>
</scr:component>

Using constructors arguments goes somewhat against the dynamic nature of OSGi. Services and bundles can be started and stopped at anytime. OSGi friendly code needs to understand this and have symmetric methods for handling the setting and unsetting of the dependencies.

One question for you: In your system, who is responsible for creating the Model objects and List of actions that you want each provider to receive? Are they available as OSGi services? The example DS that I provided assumes that they are OSGi services.

James Branigan