Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

Using multiple configuration files in OSGi with equinox launcher

I want to use separate configuration files for different bundles instead of using a single configuration/config.ini file. I am following this doc https://www.eclipse.org/equinox/documents/quickstart-framework.php .

I am not able to figure out how to achieve this. Any suggestion would be very helpful.

Thanks in advance.

question from:https://stackoverflow.com/questions/65918469/using-multiple-configuration-files-in-osgi-with-equinox-launcher

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Following links solve the problem I was facing-

https://www.eclipse.org/forums/index.php/t/1072082/

https://git.eclipse.org/c/om2m/org.eclipse.om2m.git/tree/org.eclipse.om2m.site.mn-cse/configurations/services/lifx.basedriver.properties

https://git.eclipse.org/c/om2m/org.eclipse.om2m.git/tree/org.eclipse.om2m.sdt/org.eclipse.om2m.sdt.home.lifx/src/main/java/org/eclipse/om2m/sdt/home/lifx/impl/Activator.java

Steps I followed to achieve this -

  1. Create configurations/services/<config_file_name>.properties having configurations in key-value pair. For example - property1 property1_value
  2. Import package org.osgi.service.cm for your bundle
  3. Use the following code
package <package_name>;

import java.util.Dictionary;
import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedService;

public class Activator implements BundleActivator, ManagedService {

    private static BundleContext context;
    private ServiceRegistration managedServiceServiceRegistration;

    static BundleContext getContext() {
        return context;
    }

    public void start(BundleContext bundleContext) throws Exception {
        Activator.context = bundleContext;
        Dictionary properties = new Hashtable<>();
        properties.put(Constants.SERVICE_PID, "config_file_name");
        managedServiceServiceRegistration = 
            bundleContext.registerService(ManagedService.class.getName(), this, properties);
    }

    public void stop(BundleContext bundleContext) throws Exception {
        Activator.context = null;
    }

    @Override
    public void updated(Dictionary properties) throws ConfigurationException {
        try {
            System.out.println(properties.get("property1"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...