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

标签 java osgi apache-karaf karaf

我要部署karaf中部署的bundle的所有配置文件放在karaf中etc文件夹。我希望当束 conf 文件更改时,karaf 会通知。

我有一个由几个特性组成的发行版,一个 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>

我尝试过的一些事情:

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

我不想复制具有特定路径的文件,如下所示:

有人知道怎么做吗?

更新

为了实现配置文件部署在etc文件夹,以便可以在外部重新配置 bundle ,我分 3 个步骤完成了:

构建配置文件:(有效)

为了使 Maven 可以寻址配置文件,我在 bundle 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>

在 karaf 中部署文件 etc (有效)

在 karaf 中部署配置文件 etc文件夹我添加了 <configfile>在特征文件中如下:

特征.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>

捕获配置更改:(不工作)

为了捕获配置文件的更改,我添加了您建议的代码 (@Donald_W)。问题是我只收到文件在文件夹 deploy 上的通知但不在etc .我调试这段代码,发现 etc 中的文件被特别称为这些文件的“监听器”。我不知道如何才能成为部署在 etc 中的文件的监听器

最佳答案

您可以使用 Karaf 内置的 Felix File Installer,它会在 etc 下的文件更改时给您回调。

一旦您将实现 ArtifactInstaller 的服务发布到 OSGi 服务注册表中,它就会被 FileInstaller 检测到 - 即 Whiteboard pattern .

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

示例代码(使用 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));
        }
    }
}

应该给你这样的输出:

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

您需要一个唯一的文件扩展名。 .cfg 将被您说不想使用的 ConfigurationAdmin 服务捕获。

.props(如上)之类的东西就可以了。

顺便说一句,您真的应该考虑使用ConfigurationAdmin。它非常强大。 karaf 中内置了用于管理配置的命令,您所做的任何更改都将保存回 .cfg 文件。

关于java - 如何将包配置/属性文件放在/etc karaf 文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29790859/

相关文章:

java - 应为 BEGIN_ARRAY 但为 BEGIN_OBJECT retrofit2

java - 将数据传递到 OSGI 包

apache-karaf - 迁移到 Servicemix 5 后忽略配置文件

java - ServiceMix 4.2 教程

java - 绘图仪 : What is fixed - the height or the width

java - 尽管将 'disableCNCheck' 设置为 true,但 https URL 主机名与通用名称 (CN) 不匹配

java - 用于在字符串中查找日期的正则表达式

java - OSGi - 低级服务 API

java - 调用抽象类@Activate方法(apache felix)

java - 在 apache felix (osgi) 中集成 xero (发票网关)