Apache Felix 文件 从部署文件夹安装 jar

Apache Felix File Install jar from deploy folder

我正在尝试将 Apache Felix File Install 与 Felix 的嵌入式版本一起使用。基本思路很简单,我有一个可以使用标准 java -jar app.jar 启动的 jar 应用程序文件,该应用程序将启动 Apache Felix 框架,然后在 hot deploy 文件夹中安装、更新和删除 OSGi 包在运行时来自该文件夹的 in/placed/updated/removed。

我目前已经设法创建启动嵌入式 Felix 的能力,如果我通过 BundleContext.installBundle() 指定它们,我可以部署它们,但我无法从热文件夹。

这是我目前拥有的:

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 AutoProcessor 并将属性传递给它以及框架。

final Map<String, String> config = new HashMap<>();
// Probably there is a much better way to o this...
System.getProperties().forEach((key, value) -> config.put(key.toString(), value.toString()));

// Set the properties
config.put(AutoProcessor.AUTO_DEPLOY_DIR_PROPERTY, "hot-deploy");
config.put(AutoProcessor.AUTO_DEPLOY_ACTION_PROPERTY, "install,update,start");
config.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
config.put(Constants.FRAMEWORK_STORAGE, "cache");

然后实例化框架和运行 AutoProcessor,如下所示:

final FrameworkFactory factory = new org.apache.felix.framework.FrameworkFactory();
final Framework framework = factory.newFramework(config);

try
{
    framework.init();

    AutoProcessor.process(config, framework.getBundleContext());

    FrameworkEvent event;

    do
    {
        framework.start();
        event = framework.waitForStop(0L);

    } while (event.getType() == 128);

}
catch (final Throwable e)
{
    e.printStackTrace();
}

AutoProcessor.process 然后调用 AutoProcessor.processAutoDeploy,它会在启动时自动部署捆绑包。 如果不调用 AutoProcessor.process,它将无法工作,这可能是您的问题。

事实证明 felix.fileinstall 本身是一个包,必须在主机应用程序中启动它才能监视目录。从我最初的实施开始,需要做的就是安装并启动 fileinstall 包:

installedBundles.add(context.installBundle("file:path/to/fileinstall.jar"));