How to load external properties file through OSGi

2019-06-01 04:47发布

问题:

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:

via blueprint

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

via 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);
        }
    }
}

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.

回答1:

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