问题
I need to read the specific child instance of an OSGi factory configuration. I believe it can't be accessed with the Service PID of the factory configuration so there should be a way to reference the child configuration via Java.
Can anyone please help in providing a sample code or a way to do this?
回答1:
Below is an example. "WSConnection" is an OSGI config where we can configure multiple configs. and the Helper class will help you pick the one you wanted. "configuration.id" is one of the properties for each of the OSGI config. Let me know if you need more details.
@Service(value = WSConnection.class)
@Component(immediate = true, label = "WS Factory", description = "WS
Connection Factory", configurationFactory = true, policy =
ConfigurationPolicy.REQUIRE, metatype = true)
@Properties({
@Property(name = "configuration.id", value = "", label = "Configuration ID", description = "Configuration ID to reference this configuration")
})
public class WebServiceConnection {
....
....
}
public class WSHelper extends WCMUse {
...
...
@Override
public void activate() throws Exception {
setProperties();
}
private void setProperties() {
BundleContext bundleContext = FrameworkUtil.getBundle(WSConnection.class).getBundleContext();
ServiceReference configurationAdminReference = bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
if (configurationAdminReference != null) {
ConfigurationAdmin confAdmin = (ConfigurationAdmin) bundleContext.getService(configurationAdminReference);
try {
Configuration conf[] = confAdmin.listConfigurations("("+ConfigurationAdmin.SERVICE_FACTORYPID+"="+WSConnection.class.getName()+")");
for (Configuration c : conf){
Dictionary<String,Object> props = c.getProperties();
this.configurationId = props.get("configuration.id").toString();
break;
}
}
} catch (Exception e) {
log.error("Error getting Web Service URL", e);
}
}
}
来源:https://stackoverflow.com/questions/30751407/how-can-single-instance-of-an-osgi-factory-configuration-be-read-from-java-in-cq