【问题标题】:How to load external properties file through OSGi blueprint property-placeholder and Java DSL如何通过 OSGi 蓝图属性占位符和 Java DSL 加载外部属性文件
【发布时间】:2015-08-09 02:12:06
【问题描述】:

我在 Apache servicemix 中安装了一个使用 apache 蓝图进行配置的包。我正在使用位于 /config 文件夹中的外部属性文件 abc.cfg 并按如下方式加载:

通过蓝图

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel-cxf="http://camel.apache.org/schema/blueprint/cxf"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/blueprint/core"
xsi:schemaLocation="
    http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
    http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd
    http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
    http://camel.apache.org/schema/blueprint/cxf http://camel.apache.org/schema/blueprint/cxf/camel-cxf.xsd"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0">

<cm:property-placeholder id="myProperties" persistent-id="abc" />

通过 java DSL

public class MyActivator implements BundleActivator {

    @Override
    public void start(final BundleContext context) throws Exception {
        final ServiceReference serviceReference = context.getServiceReference(ConfigurationAdmin.class.getName());
        if (serviceReference != null) {

            final ConfigurationAdmin admin = (ConfigurationAdmin) context.getService(serviceReference);
            final Configuration configuration = admin.getConfiguration("abc");
            final Dictionary<String, Object> configurations = configuration.getProperties();

            if (configurations == null) {
                throw new CustomException("Exception in loading properties file");
            }
            populateProperties(configurations);
        }
    }
}

一切正常,但现在我需要将属性文件移动到自定义位置以将属性文件从不同的包中分离出来。所以我将 abc.cfg 移到 /config/myFolder/ 中,但我无法以任何一种方式为我的包指定新位置。我尝试使用 ext:property-placeholder 但它不起作用,可能是因为我用错了(找不到任何全面的东西来理解它)。 因此,请指导我如何在 cm:property-placeholder 中以及通过 java DSL 中的配置管理服务为我的属性文件指定位置。另外,我不确定是否可以在我的包中以两种不同的方式加载相同的属性文件。

【问题讨论】:

    标签: java apache-karaf blueprint-osgi apache-servicemix


    【解决方案1】:

    蓝图 cm:property-placeholde 和 configuration-admin 服务都不使用您添加到 etc 文件夹的文件。 cm 只是使用配置管理服务的另一种方式。
    felix FileInstaller 会从 ServiceMix 实例的 etc 文件夹中读取 cfg 文件,并将这些属性传播到配置管理服务。
    因此,在您的情况下,您需要向 FileInstaller 添加另一个配置以从另一个路径读取。
    这可以通过添加一个新的配置文件来完成:

    org.apache.felix.fileinstall-mySpecialDir.cfg
    

    添加要观看的新文件夹的位置:

    felix.fileinstall.dir = myNewSpecialDirectory-to-be-watched
    

    如果需要,再加上一些。 可以找到它的文档here

    【讨论】:

    • 非常感谢。这完美地工作,现在我正确理解了这一点。我在这个问题上摆弄了太久,主要是因为初学者缺乏好的学习资源。
    • 有一些关于 osgi 的初学者书籍和一本 karaf 食谱。这些可能会有所帮助。不要忘记 OSGi 的实际应用。
    猜你喜欢
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多