6

When I use a reference in DS I can specify a target that will allow me to narrow down which service instance I want. The problem is that all the examples show static queries that must be done at code time. Is there a way to do a dynamic query (maybe pull in a property from Configuration Admin)?

If DS does not support this is there another OSGi dependency injection framework (blueprint, iPojo, etc) that will support this?

2 Answers 2

7

You can always use the target attribute of the reference to peform a first level filter. The if your bind method has the signature

void <method-name>(<parameter-type>, Map);

Then you can run any dynamic filter over the Map which contains the services properties. If the filter does not match, you can ignore that component (for the time being.)

In the alternate, since the component's configuration can contain the reference's target filter, you can modify the component's configuration to alter the target filter.

1
  • I played around with your first suggestion and it seems like it will fit our needs just fine. Thanks. Apr 27, 2012 at 21:15
5

I use the following trick. If you specify a "target" attribute on the service reference but leave its value as the empty string, then a component property with the same name but a ".target" suffix will be used at runtime.

In the example below, I select my JDBC source dynamically via a .cfg file in my Karaf container. The "datasourcefactory.target" property is injected automatically into the "target" attribute of the "datasourcefactory" reference.

Caveat: I don't actually know if this trick is officially supported or just a Felix SCR feature. I've been meaning to look this up in the spec to see if it's mentioned... +1 to any comment that clarifies its legality!

    @Component(
            name = "...",
            specVersion = "1.1",
            policy = ConfigurationPolicy.REQUIRE,
            metatype = true
    )
    @Property(name = "dataSourceFactory.target",
            value = "",
            label = "dataSourceFactory target",
            description = "An OSGi service filter to select the data source provider. "+
                    "For example: '(&(osgi.jdbc.driver.name=derby)(objectClass=org.osgi.service.jdbc.DataSourceFactory))' where 'derby' is the important bit and the rest is boilerplate. "+
                    "See DataSourceFactory.OSGI_JDBC_DRIVER_(NAME,CLASS,VERSION)."
    )
    @Reference(
            name = "dataSourceFactory",
            referenceInterface = org.osgi.service.jdbc.DataSourceFactory.class,
            cardinality = ReferenceCardinality.MANDATORY_UNARY,
            target = "", // must be overwritten by configuration property named "dataSourceFactory.target"
            bind = "bindDataSourceFactory",
            unbind = "unbindDataSourceFactory"
    )
1
  • 6
    Section 112.3.4 of the OSGi 4.2 Service Compendium mentions: "The deployer can also set the target property by establishing a configuration for the component which sets the value of the target property. This allows the deployer to override the target property in the component description." - this is an official feature. May 3, 2012 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.