How can I get properties stored in ConfigAdmin?

穿精又带淫゛_ 提交于 2019-12-12 01:38:04

问题


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

回答1:


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.

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



来源:https://stackoverflow.com/questions/30582580/how-can-i-get-properties-stored-in-configadmin

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