如何在OSGI出厂配置的单个实例可以在CQ从Java读取(How can single instan

2019-10-23 07:36发布

我需要读取一个OSGi出厂配置的具体子实例。 我相信它不能与出厂配置的服务PID进行访问,所以应该通过Java来引用子配置的一种方式。

任何人都可以请提供样品代码还是有办法做到这一点帮助?

Answer 1:

下面是一个例子。 “WSConnection”是一个OSGI的配置,我们可以配置多个CONFIGS。 和辅助类会帮助你挑选你想要的人。 “configuration.id”是为每个OSGI配置的属性之一。 让我知道,如果你需要更多的细节。

@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);
       }
    }

 }


文章来源: How can single instance of an OSGI factory configuration be read from Java in CQ