2015-05-27 40 views
0

我在Apache servicemix中安装了一个使用apache blueprint进行配置的包。我使用的是外部属性文件abc.cfg位于/ config文件夹,并如下加载:通过Java DSL通过蓝图如何通过OSGi blueprint属性占位符和Java DSL加载外部属性文件

<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" /> 

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

回答

4

蓝图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

+0

非常感谢你。这工作完美,现在我明白这一点。我摆弄这个太久了,主要是因为缺乏对初学者的良好学习资源。 – ishan

+0

对于初学者和卡拉夫食谱,有一些关于osgi的书籍。这些可能有帮助。不要忘记OSGi在行动。 –