0
votes

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.

4

4 Answers

1
votes

The only real answer is: don't use FileInstall for production use cases, but write your own management agent that does what you actually want. It's not that hard to install bundles...

0
votes

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.

0
votes

I don't think it's possible with the current implementation of fileinstall :

fileinstall take the first ArtifactListener which can-handle an artifact, and i don't see any "ordered property". The BundleTransformer return true if the jar is a valid bundle (in regard of his manifest, not the solvability of his requirements). If this listener is register before your own listener, then your listener will never be called.

0
votes

I'm currently using java WatchService that monitors a directory, if something is added to directory i check the jar and if everything is ok i move the jar to the fileinstall hotdeploy directory. Fileinstall sees the jar and installs it.

Maybe a poor man solution, but it does the job.