[英]How to place bundle configuration/property file in /etc karaf folder
我想部署karaf中部署的bundle的所有配置文件放在karaf etc
文件夹中。 我希望当捆绑包文件发生变化时,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
我不想复制具有特定路径的文件,如下所示:
有谁知道如何做到这一点?
UPDATE
为了实现配置文件部署在etc
文件夹中以便可以在外部重新配置bundle,我已经完成了3个步骤:
构建配置文件:( Works )
为了使配置文件可由Maven寻址,我在bundle pom中添加了以下部分。 通过这种方式,配置文件在存储库上部署:
的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
部署文件( Works )
要在karaf etc
文件夹中部署配置文件,我在功能文件中添加了<configfile>
,如下所示:
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>
捕获配置更改:( 不工作 )
要捕获配置文件的更改,我添加您建议的代码(@Donald_W)。 问题是我得到的文件通知是在文件夹deploy
但不在etc
。 我调试这个代码,我发现对于etc
中的文件特别称为这些文件的“监听器”。 我不那么知道我可以成为部署在一个文件中的监听器etc
您可以使用内置于Karaf中的Felix文件安装程序,它可以在文件更改etc
时为您提供回调。
一旦将实现ArtifactInstaller
的服务发布到OSGi服务注册表中,它将被FileInstaller
检测到 - 即白板模式 。
<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
(如上所述)之类的东西可以.props
。
顺便说一句,你真的应该看看使用ConfigurationAdmin
。 它非常强大。 karaf中内置了用于管理配置的命令,您所做的任何更改都将持久保存回.cfg
文件。
找到了!
正如我在UPDATE中提到的,步骤1(使用maven构建配置文件)和2(部署conf文件etc
)工作正常但捆绑包和配置文件未连接。 显然原因是配置文件安装了另一个PID作为注册它的服务。 为了解决这个问题,我将ManageService
注册到部署的可配置文件的PID。 在我看来如下:
Hashtable <String, Object> properties = new Hashtable<String, Object>();
properties.put(Constants.SERVICE_PID, "MQTTBackboneProtocol");
context.registerService (ManagedService.class.getName(),this , properties);
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.