简体   繁体   English

如何将bundle配置/属性文件放在/ etc karaf文件夹中

[英]How to place bundle configuration/property file in /etc karaf folder

I want to deploy all the configuration files of the bundles deployed in karaf be placed in the karaf etc folder. 我想部署karaf中部署的bundle的所有配置文件放在karaf etc文件夹中。 I want that when the bundles conf files are change are notice by the karaf. 我希望当捆绑包文件发生变化时,karaf会注意到这一点。

I have a distribution which consist of several features an example of a feature of the XML.I already tried several things eg I add the conf file into the feature as below, but this dose not work. 我有一个由几个功能组成的发行版,一个XML功能的例子。我已经尝试了几个例子,例如我将conf文件添加到功能中,如下所示,但这不起作用。

<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 have tried: 我试过的一些事情:

http://karaf.922171.n3.nabble.com/OSGi-bundle-configuration-file-td4025438.html 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 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 the file with a specific path as is shown here: 我不想复制具有特定路径的文件,如下所示:

have anyone an idea how to do this? 有谁知道如何做到这一点?

UPDATE UPDATE

To achieve that the configuration file get deployed in the etc folder so the bundle can be reconfigured externally, I have done it in 3 steps: 为了实现配置文件部署在etc文件夹中以便可以在外部重新配置bundle,我已经完成了3个步骤:

Building the configuration file: ( Works ) 构建配置文件:( Works

To make the config file addressable by Maven I added the following part in the bundle pom. 为了使配置文件可由Maven寻址,我在bundle pom中添加了以下部分。 In this way the config file is deploy on the repository: 通过这种方式,配置文件在存储库上部署:

pom.xml 的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>

Deploy the file in the karaf etc ( Works ) 在karaf etc部署文件( Works

To deploy the config file in the karaf etc folder I added the <configfile> in the feature file as following: 要在karaf etc文件夹中部署配置文件,我在功能文件中添加了<configfile> ,如下所示:

features.xml features.xml

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

Capture configuration change: ( not working ) 捕获配置更改:( 不工作

To capture the change of a config file I add the code you suggested (@Donald_W). 要捕获配置文件的更改,我添加您建议的代码(@Donald_W)。 The problem is that I get just notifications of files are on the folder deploy but not in etc . 问题是我得到的文件通知是在文件夹deploy但不在etc I debug this code and I find out that for the files in etc are called specifically the "listeners" of those files. 我调试这个代码,我发现对于etc中的文件特别称为这些文件的“监听器”。 I don't know then how I can become a listener of a file deployed in the etc 我不那么知道我可以成为部署在一个文件中的监听器etc

You can use the Felix File Installer which is built into Karaf, which will give you callbacks when files change under etc . 您可以使用内置于Karaf中的Felix文件安装程序,它可以在文件更改etc时为您提供回调。

Once you publish a service which implements ArtifactInstaller into the OSGi service registry it will be detected by the FileInstaller - ie the Whiteboard pattern . 一旦将实现ArtifactInstaller的服务发布到OSGi服务注册表中,它将被FileInstaller检测到 - 即白板模式

<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 would work just as well) would be: 示例代码(使用iPojo但Blueprint / DS也可以正常工作)将是:

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 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'll need a unique file extension. 您需要一个唯一的文件扩展名。 .cfg will be captured by the ConfigurationAdmin service which you say you don't want to use. .cfg将由您不想使用的ConfigurationAdmin服务捕获。

Something like .props (as above) would do the trick. .props (如上所述)之类的东西可以.props

As an aside, you really should look at using ConfigurationAdmin . 顺便说一句,你真的应该看看使用ConfigurationAdmin It's incredibly powerful. 它非常强大。 There are commands built into karaf for managing the configuration, and any changes you make will persist back to the .cfg files. karaf中内置了用于管理配置的命令,您所做的任何更改都将持久保存回.cfg文件。

Eureka! 找到了!

As I mentioned in the UPDATE the step 1 (build configuration file with maven) and 2 (deploy the conf file etc ) was working but the bundle and the configuration file were not connected. 正如我在UPDATE中提到的,步骤1(使用maven构建配置文件)和2(部署conf文件etc )工作正常但捆绑包和配置文件未连接。 Apparently the reason was that the configuration file was install with another PID as the service which was registering to it. 显然原因是配置文件安装了另一个PID作为注册它的服务。 To solve this I registered the ManageService to the PID of the configurated file deployed. 为了解决这个问题,我将ManageService注册到部署的可配置文件的PID。 In my case looks as following: 在我看来如下:

 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 which implements the ManageService and the PID must be the same as is deployed configuration in the etc , in this case "MQTTBackboneProtocol" due to the feature definition indicate the configuration file as: this是实现ManageService的类,并且PID必须与etc部署配置相同,在这种情况下,由于功能定义, "MQTTBackboneProtocol"将配置文件指示为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在apache karaf中安装和运行osgi bundle - how to install and run osgi bundle in apache karaf 如何避免karaf加载默认的解析包 - how to avoid karaf load the default resolve bundle 在Karaf中以编程方式设置JAAS配置(Kerberos)文件 - Setting JAAS Configuration (Kerberos) file programmatically in Karaf 如何摆脱OSGi捆绑软件的相应配置(“ service.pid”等)中不存在的属性? - How to get rid of OSGi bundle's properties that are not exist in their corresponding configuration (“service.pid”, etc.)? 如何在OSGi包中使用属性文件 - How to use a property file in an OSGi bundle Karaf捆绑软件加载优先级 - Karaf bundle load precedence 如何在运行时从程序包中以编程方式配置karaf / etc文件? - how to configure karaf/etc files during runtime from bundles programatically? 如何在Maven项目中忽略从部署到karaf的一个捆绑包 - how to ignore one bundle from deployement to karaf in maven project 在Netbeans和Karaf中开发OSGI捆绑包时如何管理依赖项? - How to manage dependencies when developing OSGI bundle in Netbeans and Karaf? 如何在 java 中的可执行 jar 文件中捆绑配置文件 - How to bundle configuration files in an executable jar file in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM