Tinwor
Tinwor

Reputation: 7983

How can I get properties stored in 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;
        }
    }
}

Upvotes: 0

Views: 588

Answers (1)

Christian Schneider
Christian Schneider

Reputation: 19626

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

Upvotes: 1

Related Questions