如何通过 OSGi 蓝图 属性-placeholder 和 Java DSL 加载外部属性文件
How to load external properties file through OSGi blueprint property-placeholder and Java DSL
我在 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);
}
}
}
一切正常,但现在我需要将 属性 文件移动到自定义位置以将 属性 文件与不同的包分开。所以我在 /config/myFolder/ 中移动了 abc.cfg 但我无法以任何一种方式为我的包指定新位置。我尝试使用 ext:属性-placeholder 但它没有用,可能是因为我用错了(找不到任何全面的东西来理解它)。
因此,请指导我如何在 cm:property-placeholder 中以及如何通过 java DSL 中的配置管理服务指定我的属性文件的位置。另外,我不确定是否可以在我的包中以两种不同的方式加载相同的属性文件。
蓝图 cm:property-placeholde 和配置管理服务都没有使用您添加到 etc 文件夹中的文件。 cm 只是使用配置管理服务的另一种方式。
felix FileInstaller 会从 ServiceMix 实例的 etc 文件夹中读取 cfg 文件,并将这些属性传播到 Configuration Admin 服务。
因此,在您的情况下,您需要向 FileInstaller 添加另一个配置以从另一个路径读取。
这可以通过添加一个新的配置文件来完成:
org.apache.felix.fileinstall-mySpecialDir.cfg
添加要观看的新文件夹的位置:
felix.fileinstall.dir = myNewSpecialDirectory-to-be-watched
如果需要,再加一些。
可以找到它的文档 here
我在 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);
}
}
}
一切正常,但现在我需要将 属性 文件移动到自定义位置以将 属性 文件与不同的包分开。所以我在 /config/myFolder/ 中移动了 abc.cfg 但我无法以任何一种方式为我的包指定新位置。我尝试使用 ext:属性-placeholder 但它没有用,可能是因为我用错了(找不到任何全面的东西来理解它)。 因此,请指导我如何在 cm:property-placeholder 中以及如何通过 java DSL 中的配置管理服务指定我的属性文件的位置。另外,我不确定是否可以在我的包中以两种不同的方式加载相同的属性文件。
蓝图 cm:property-placeholde 和配置管理服务都没有使用您添加到 etc 文件夹中的文件。 cm 只是使用配置管理服务的另一种方式。
felix FileInstaller 会从 ServiceMix 实例的 etc 文件夹中读取 cfg 文件,并将这些属性传播到 Configuration Admin 服务。
因此,在您的情况下,您需要向 FileInstaller 添加另一个配置以从另一个路径读取。
这可以通过添加一个新的配置文件来完成:
org.apache.felix.fileinstall-mySpecialDir.cfg
添加要观看的新文件夹的位置:
felix.fileinstall.dir = myNewSpecialDirectory-to-be-watched
如果需要,再加一些。 可以找到它的文档 here