How to place configuration file / package properties in / etc karaf folder

I want to deploy all configuration files of packages deployed in karaf to karaf folder etc

. I want the case where the confples files are added caraf.

I have a distribution consisting of several functions, an example of an XML function. I've tried several things already, for example. I am adding a conf file to the function as shown below, but this dose is not working.

<feature name="gc-backbone-mqtt" version="${linksmart.gc.version}">
    <feature version="${linksmart.gc.version}">gc-backbone-router</feature>
    <bundle>mvn:org.eclipse.paho/org.eclipse.paho.client.mqttv3/1.0.0</bundle>
    <feature version="${linksmart.gc.version}">gc-type-tunnelled</feature>
    <configfile finalname="/etc/mqttBackboneProtocol.cfg">mvn:eu.linksmart.gc/backbone.mqtt.impl/${linksmart.gc.version}/mqttprotocol.properties</configfile>
    <bundle>mvn:eu.linksmart.gc/backbone.mqtt.impl/${linksmart.gc.version}</bundle>
</feature>

      

Some of the things I've tried:

http://karaf.922171.n3.nabble.com/OSGi-bundle-configuration-file-td4025438.html

http://www.liquid-reality.de/display/liquid/2011/09/23/Karaf+Tutorial+Part+2+-+Using+the+Configuration+Admin+Service

I don't want to copy a file with a specific path as shown here:

Anyone have an idea how to do this?

UPDATE

To ensure that the config file is deployed in a folder etc

so that the package is migrated externally, I did it in 3 steps:

Config file creation: ( Jobs )

To make the config file address Maven, I added the following part to the pom. Thus, the config file is deployed to the repository:

pom.xml

 <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attach-artifact</goal>
                        </goals>
                        <configuration>
                            <artifacts>
                                <artifact>
                                    <file>src/main/resources/mqttprotocol.properties</file>
                                    <type>cfg</type>
                                    <classifier>configuration</classifier>
                                </artifact>
                            </artifacts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

      

Expand the file to karaf etc

( Works )

To expand the config file in the karaf folder etc

, I added <configfile>

functions to the file as follows:

features.xml

</feature>
    <feature name="gc-backbone-mqtt" version="${linksmart.gc.version}">
        <feature version="${linksmart.gc.version}">gc-backbone-router</feature>
        <bundle>mvn:org.eclipse.paho/org.eclipse.paho.client.mqttv3/1.0.0</bundle>
        <bundle>mvn:org.apache.felix/org.apache.felix.fileinstall/3.2.8</bundle>
        <configfile finalname="/etc/MQTTBackboneProtocol.cfg">mvn:eu.linksmart.gc/network.backbone.protocol.mqtt.impl/${linksmart.gc.version}/cfg/configuration</configfile>
        <feature version="${linksmart.gc.version}">gc-type-tunnelled</feature>          <bundle>mvn:eu.linksmart.gc/network.backbone.protocol.mqtt.impl/${linksmart.gc.version}</bundle>
    </feature>

      

Write the config config: ( doesn't work )

To record the change to the config file, I am adding the code you suggested (@Donald_W). The problem is that I only get notifications about files in the folder deploy

, not in etc

. I am debugging this code, and I find out that for files in etc

these files "listeners" are specially named. Then I don't know how I can become a listener of a file deployed toetc

+3


source to share


2 answers


Eureka!

As I mentioned in the UPDATE, step 1 (config file with maven) and 2 (expand conf file etc

) worked, but package and config file were not related. Apparently the reason is that the config file was installed with a different PID as the service that registered it. To solve this problem, I registered ManageService

in the PID of the deployed config file. In my case it looks like this:

 Hashtable <String, Object> properties = new Hashtable<String, Object>();
 properties.put(Constants.SERVICE_PID, "MQTTBackboneProtocol");
 context.registerService (ManagedService.class.getName(),this , properties);

      



Where this

is the class that implements ManageService

, and the PID must be the same as the deployed configuration in etc

, in this case, "MQTTBackboneProtocol"

because of the function definition, specifies the configuration file as:

<configfile finalname="/etc/MQTTBackboneProtocol.cfg">mvn:eu.linksmart.gc/network.backbone.protocol.mqtt.impl/${linksmart.gc.version}/cfg/configuration</configfile>

      

0


source


You can use the Felix File Installer that is built into Karaf, which will give you callbacks when files change in etc

.

After publishing a service that implements an ArtifactInstaller

OSGi service in the registry, it will be discovered using FileInstaller

- that is, Board Map .

<dependency>
    <groupId>org.apache.felix</groupId>
    <artifactId>org.apache.felix.fileinstall</artifactId>
    <version>3.4.2</version>
    <scope>provided</scope>
</dependency>

      

Sample code (using iPojo, but Blueprint / DS will work just as well):

package com.example.deployer.internal;

import org.apache.felix.fileinstall.ArtifactInstaller;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Provides;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@Instantiate()
@Component(name = "propsDeployer")
@Provides(specifications = ArtifactInstaller.class)
public class PropsDeployer implements ArtifactInstaller {
    Logger logger = LoggerFactory.getLogger(JsonDeployer.class);

    @Override
    public void install(File artifact) throws Exception {
        logOutput("Installing",artifact);
    }

    @Override
    public void update(File artifact) throws Exception {
        logOutput("Updating",artifact);
    }

    @Override
    public void uninstall(File artifact) throws Exception {
        logger.info("Uninstalling artifact: {}", artifact.getName());
    }

    @Override
    public boolean canHandle(File artifact) {
        return artifact.getName().endsWith(".props");
    }

    private void logOutput(String action, File artifact) throws IOException {
        logger.info(action + " artifact: {}", artifact.getName());
        Properties props = new Properties();
        FileReader in = null;
        try {
            in = new FileReader(artifact.getCanonicalFile());
            props.load(in);
        } finally {
            if (in != null) {
                in.close();
            }
        }

        // Do whatever you want here:

        for(Object key: props.keySet()) {
            logger.info(action + " Property received: {} {}",key,props.get(key));
        }
    }
}

      



Should give you an output like this:

2015-04-27 20:16:53,726 | INFO  | ime/karaf/deploy | PropsDeployer                     | 101 - com.example.scratch.deployer - 1.0.0.SNAPSHOT | Updating artifact: my.json
2015-04-27 20:16:53,728 | INFO  | ime/karaf/deploy | PropsDeployer                     | 101 - com.example.scratch.deployer - 1.0.0.SNAPSHOT | Updating Property received: myprop myval
2015-04-27 20:16:53,728 | INFO  | ime/karaf/deploy | PropsDeployer                     | 101 - com.example.scratch.deployer - 1.0.0.SNAPSHOT | Updating Property received: hello world

      

You will need a unique file extension..cfg will be captured by the ConfigurationAdmin service, which you say you don't want to use.

Something like .props

(as above) would do the trick.

As an aside, you really should be looking at usage ConfigurationAdmin

. It's incredibly powerful. There are commands built into karaf to manage configuration, and any changes you make will be saved in files .cfg

.

+2


source







All Articles