【发布时间】:2015-11-29 22:38:10
【问题描述】:
我正在尝试将 Apache Felix File Install 与 Felix 的嵌入式版本一起使用。基本思想很简单,我有一个可以使用标准java -jar app.jar 启动的 jar 应用程序文件,该应用程序将启动 Apache Felix 框架,然后查看hot deploy 文件夹,安装、更新和删除位于/在运行时从该文件夹放置/更新/删除。
我目前已经设法创建了启动嵌入式 Felix 的功能,如果我通过 BundleContext.installBundle() 指定它们,我可以部署包,但我无法从热文件夹中动态获取 jar 包。
这是我目前拥有的:
public static void main(String[] args) throws Exception {
System.setProperty("felix.fileinstall.noInitialDelay", "true");
System.setProperty("felix.fileinstall.poll", "1000");
System.setProperty("felix.fileinstall.dir", "./hot-deploy");
System.out.println("Building OSGi Framework");
FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
Map<String, String> config = new HashMap<>();
// make sure the cache is cleaned
config.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
// more properties available at: http://felix.apache.org/documentation/subprojects/apache-felix-service-component-runtime.html
config.put("ds.showtrace", "true");
config.put("ds.showerrors", "true");
Framework framework = frameworkFactory.newFramework(config);
framework.start();
// declarative services dependency is necessary, otherwise they won't be picked up!
loadScrBundle(framework);
BundleContext context = framework.getBundleContext();
List<Bundle> installedBundles = new LinkedList<>();
//installedBundles.add(context.installBundle("file:./Sandbox/osgiTest/module-a/target/module-a-1.0-SNAPSHOT.jar"));
for (Bundle bundle : installedBundles) {
if (bundle.getHeaders().get(Constants.FRAGMENT_HOST) == null) {
bundle.start();
}
}
try {
framework.waitForStop(0);
} finally {
System.exit(0);
}
}
private static void loadScrBundle(Framework framework) throws URISyntaxException, BundleException {
URL url = Activator.class.getClassLoader().getResource("org/apache/felix/scr/ScrService.class");
if (url == null) {
throw new RuntimeException("Could not find the class org.apache.felix.scr.ScrService");
}
String jarPath = url.toURI().getSchemeSpecificPart().replaceAll("!.*", "");
System.out.println("Found declarative services implementation: " + jarPath);
framework.getBundleContext().installBundle(jarPath).start();
}
【问题讨论】:
-
我认为主要问题是您没有将系统道具传递给 felix 框架本身,这意味着 felix 将无法获取您的“felix.fileinstall.dir”属性。请参阅github.com/apache/felix/blob/… 了解 felix 启动器本身如何处理系统道具。
-
我查看了代码并尝试将
SYSTEM_PROPERTIES_PROP设置为指向定义felix.fileinstall.dir属性的config.cfg文件的系统属性。但是,我什么也没得到。
标签: java maven osgi osgi-bundle apache-felix