How can single instance of an OSGI factory configuration be read from Java in CQ

☆樱花仙子☆ 提交于 2020-01-05 10:25:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!