使用 equinox 启动器在 OSGi 中使用多个配置文件

问题描述

我想为不同的包使用单独的配置文件,而不是使用单个 configuration/config.ini 文件。我正在关注此文档 https://www.eclipse.org/equinox/documents/quickstart-framework.php .

我不知道如何实现这一点。任何建议都会非常有帮助。

提前致谢。

解决方法

以下链接解决了我面临的问题-

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

我为实现这一目标而遵循的步骤 -

  1. 创建配置/服务/.properties 具有键值对中的配置。例如 - property1 property1_value
  2. 为您的捆绑包导入包 org.osgi.service.cm
  3. 使用以下代码
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();
        }
        
    }

}