如何获取存储在ConfigAdmin中的属性? [英] How can I get properties stored in ConfigAdmin?
问题描述
我已经创建了ConfigAdmin,并加载了一些属性.之后,我保存了它们.我的问题是:如何获取已存储的属性?
我已经在Activator.java中创建了ConfigAdmin:
I've created the ConfigAdmin loaded some properties. After that I've saved them. My question is: how can I get the properties that I have stored?
I've created the ConfigAdmin in the Activator.java:
public class Activator implements BundleActivator {
private String configFile = "API.properties";
@Override
public void start(BundleContext bundleContext) throws Exception {
InputStream stream = (bundleContext.getBundle().getResource(configFile)).openStream();
Properties properties = new Properties();
properties.load(stream);
createConfigAdmin(properties);
}
@Override
public void stop(BundleContext bundleContext) throws Exception {
}
private boolean createConfigAdmin(Properties properties, BundleContext context) {
try {
Dictionary<String, String> props = new Hashtable<String, String>();
ServiceReference reference = context.getServiceReference(ConfigurationAdmin.class.getName());
ConfigurationAdmin admin = (ConfigurationAdmin) context.getService(reference);
Configuration configuration = admin.createFactoryConfiguration(pid.configAdminPID, null);
for(final String name: properties.stringPropertyNames())
props.put(name, properties.getProperty(name));
configuration.update(props);
return true;
} catch(Exception e)
{
e.printStackTrace();
return false;
}
}
}
推荐答案
您真的打算创建工厂配置吗?仅当您要为同一工厂pid创建多个配置时才需要它. 如果您只想创建一个简单的配置,则只需使用admin.getConfiguration(oid),您就可以以与现在相同的方式更新配置.
Was your intention really to create a factory config? You only need it if you want to create several configs for the same factory pid. If you simply want to create a simple configuration then just use admin.getConfiguration(oid) you can update the configuration in the same way you do now.
如果您以后要阅读配置,只需再次获取即可.如果要使用此配置来配置捆绑软件,通常将创建一个ManagedService并将其发布.参见
If you want to read the configuration afterwards you simply get it again. If you want to configure a bundle with this configuration you typically will create a ManagedService and publish it. See http://liquid-reality.de/display/liquid/2011/09/23/Karaf+Tutorial+Part+2+-+Using+the+Configuration+Admin+Service
这篇关于如何获取存储在ConfigAdmin中的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!