[英]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.