0

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.

cornr
  • 653
  • 4
  • 20

4 Answers4

1

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...

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
0

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.

Christian Schneider
  • 19,420
  • 2
  • 39
  • 64
  • I see, this is definitely a probleme since ArtifactListener is instantiated in my code (and my classloader) and is therefore not the ArtifactListener the File Install Bundle excpects. – cornr Feb 17 '16 at 09:59
  • There is also another problem. _File Install_ does not track Services of type ArtifactListener. Therefore I would need to implement a Bundle which depends on _File Install_ and registers a `ArtifactTransformer`. – cornr Feb 17 '16 at 10:06
0

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.

Jérémie B
  • 10,611
  • 1
  • 26
  • 43
0

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.

Tinus Tate
  • 2,237
  • 2
  • 12
  • 33