如何通过 OSGi 蓝图属性占位符和 Java DSL 加载外部属性文件 [英] How to load external properties file through OSGi blueprint property-placeholder and Java DSL

查看:27
本文介绍了如何通过 OSGi 蓝图属性占位符和 Java DSL 加载外部属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a bundle installed in Apache servicemix that is using apache blueprint for configuration. I am using an external properties file abc.cfg located in /config folder and is being loaded as follows:

通过蓝图

<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 中的配置管理服务.另外,我不确定是否可以在我的包中以两种不同的方式加载相同的属性文件.

Everything works fine but now i need to move the property file in custom location to segregate property files from different bundles. So i moved abc.cfg in /config/myFolder/ but i am unable to specify the new location to my bundle in either ways. I tried using ext:property-placeholder but it didn't work, probably because i am using it wrong(couldn't find anything comprehensive to understand it). So please guide me on how can i specify location for my properties file in cm:property-placeholder and through configuration admin service in java DSL. Also, i am not sure if it is ok to load the same properties file in two different ways in my bundle.

推荐答案

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

Neither the blueprint cm:property-placeholde nor the configuration-admin service use the file you added to the etc folder. The cm is just another way of using the configuration admin service.
The felix FileInstaller does read cfg files from the etc folder of your ServiceMix instance and does propagate those properties to the Configuration Admin service.
So in your case you'll need to add another configuration to the FileInstaller to read from another path.
This can be done by adding a new configuration file:

org.apache.felix.fileinstall-mySpecialDir.cfg

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

where you add the new folder to be watched:

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

如果需要,再加一些.它的文档可以在这里

plus some more if needed. Documentation for it can be found here

这篇关于如何通过 OSGi 蓝图属性占位符和 Java DSL 加载外部属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆