如何从CQ中的Java中读取OSGI工厂配置的单个实例 [英] How can single instance of an OSGI factory configuration be read from Java in CQ

查看:59
本文介绍了如何从CQ中的Java中读取OSGI工厂配置的单个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要阅读OSGi工厂配置的特定子实例. 我认为无法使用出厂配置的服务PID进行访问,因此应该有一种方法可以通过Java引用子配置.

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?

推荐答案

下面是一个示例. "WSConnection"是一个OSGI配置,我们可以在其中配置多个配置.并且Helper类将帮助您选择想要的人. "configuration.id"是每个OSGI配置的属性之一.让我知道您是否需要更多详细信息.

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

 }

这篇关于如何从CQ中的Java中读取OSGI工厂配置的单个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆