我已经创建了ConfigAdmin加载了一些属性。之后我就救了他们。我的问题是:我如何获得我存储的属性? 我在Activator.java中创建了ConfigAdmin:
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;
}
}
}
答案 0 :(得分:1)
您的意图是否真的要创建工厂配置?如果要为同一工厂pid创建多个配置,则只需要它。 如果您只是想创建一个简单的配置,那么只需使用admin.getConfiguration(oid)就可以像现在一样更新配置。
如果您想在之后阅读配置,只需再次获取。如果要使用此配置配置捆绑包,通常会创建ManagedService并将其发布。见http://liquid-reality.de/display/liquid/2011/09/23/Karaf+Tutorial+Part+2+-+Using+the+Configuration+Admin+Service