Search code examples
javaosgiapache-felixosgi-bundle

How to filter OSGi Bundles in Plugin folder with Apache Felix


We are loading OSGi Bundles with Apache Felix and its subproject Apache File Install from a given folder. In our case it is possible that in this folder will be bundles with an Import-Package which is not on the container's classpath. This results in an error log message (every 2 seconds). (This is ok, the bundle should not be loaded).

My question is: Is there a possibility to filter bundles before they are installed?

I checked the implementation of org.apache.felix.fileinstall.internal.DirectoryWatcher and figuered out that I probably want a org.apache.felix.fileinstall.ArtifactListener:

final ArtifactListener myListener = new ArtifactListener() {
    @Override
    public boolean canHandle(final File artifact) {
        return bundleFullfillsPrecoditions(artifact);
    }
};

Unfortunatly I do not manage to register that listener correctly (and canHandle is never been called). I tried to register it as a service on the BundleContext:

//Initialize Felix Framework
org.osgi.framework.launch.Framework osgiFramework = this.createFramework(configuration);
osgiFramework.init();
osgiFramework.start();

//Register Listener?
osgiFramework.getBundleContext().registerService(ArtifactListener.class, myListener, null);

//Start File Install Bundle
org.osgi.framework.Bundle pluginFolderWatcher = osgiFramework.getBundleContext().installBundle(getFolderWatcherJarPath());

pluginFolderWatcher.start();

Maybe its the wrong way, or I missed something. Do you have ideas? Thanks in advance.


Solution

  • I think the problem is that you try to register the listener from outside the OSGi framework. That can only work if you export the package org.apache.felix.fileinstall as a system package export.

    Even then though you have to be careful as the fileinstall bundle will also bring this package.

    So the safer way would be to implement the listener inside a bundle and install this bundle.