I am using the following trick. If you specify the โtargetโ attribute in the service link, but leave it as an empty string, then the component property with the same name but the suffix โ.targetโ will be used at runtime.
In the example below, I select the JDBC source dynamically through the .cfg file in the Karaf container. The "datasourcefactory.target" property is automatically entered into the "target" attribute of the "datasourcefactory" link.
Caveat: I really don't know if this trick is officially supported or just the SCR Felix feature. I meant to look at it in the specification to see if it is mentioned ... +1 to any comment that clarifies its validity!
@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" )
source share