Lance
Lance

Reputation: 145

Embedded Felix not able to cast activator to BundleActivator

Problem

I'm having a problem instantiating my OSGi bundle activator in the Felix OSGi container. When I attempt to start the bundle I get an error that I can't cast the activator type to org.osgi.framework.BundleActivator despite the fact this is what the parent class is defined as in the project.

I've included org.osgi:org.osgi.core:5.0.0 in the activated bundles so that the class that has the activator can be STARTED. Should I need to reference the OSGi library in the OSGi bundle or should the Felix container provide them for me?

I have the same problem whether running Felix embedded or from the command line.

** Solution **

It turns out the version of OSGi in Felix is older than the latest standards. As I'm new to OSGi I just grabbed all of the latest things and started developing. I've moved back from version 5.0.0 to 4.3.0. The only difference I notice is the lack of nice generics in the API.

Now that my classes are compatible they can be resolved when started in the container. Previous problems were just due to the version of the library.

Background

I have a Maven built project that wasn't starting because it couldn't find dependencies to org.osgi.framework(vesrion>=1.7.0)(!(version>=2.0.0)).

The project configuraiton is

Main
  SubProjectApi
  SubProjectImpl
  SubProjectIntegrationTest

Where SubProjectImpl has a dependency to SubProjectApi. SubProjectApi has a bundle built using the maven-bundle-plugin but doesn't contain any OSGi reference. SubProjectImpl references the Api module and provides a BundleActivator. To resolve the OSGi classes the project has a dependency to org.osgi:org.osgi.core:5.0.0. Everything compiles fine.

The project SubProjectIntegrationTest provides a set of unit tests to ensure the SubProjectImpl works properly within an embedded Felix environment. I've followed the guide for setting up the Felix instance (creating Activators, referencing the required Jars, etc.). Because the testing environment and the OSGi both need the same API classes I've used the org.osgi.framework.system.packages.extra configuration option to share the API classes.

When the test was started the following error was produced

ERROR: Bundle dsto.lod.simr.core [12] 
    Error starting file:target/SubProjectImpl.jar 
    (org.osgi.framework.BundleException: Unresolved constraint in bundle
    project.impl [12]: Unable to resolve 12.0: 
    missing requirement [12.0] osgi.wiring.package; 
    (&(osgi.wiring.package=org.osgi.framework)(version>=1.7.0)(!(version>=2.0.0))))

I thought the Felix environment would automatically provide those classes. I added the org.osgi.core module into the environment the problem changed to the one I have with casting types.

Upvotes: 2

Views: 3152

Answers (2)

kapex
kapex

Reputation: 29959

I've had the problem issue in a Maven based project, where the imports seemingly were correct. The issue was that some incorrect version of the interface got cached. It worked after clearing the cache.

To always clear the cache on startup, I've used the configuration parameter org.osgi.framework.Constants.FRAMEWORK_STORAGE_CLEAN:

Map<String, String> config = new HashMap<>();
config.put(Constants.FRAMEWORK_STORAGE_CLEAN, 
           Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
Framework framework = frameworkFactory.newFramework(config);

Upvotes: 0

BJ Hargrave
BJ Hargrave

Reputation: 9384

You should not install the Core API bundle. Those packages are exported by the framework implementation. If you install the bundle and resolve against the bundle, then your bundle and the framework implement will be using different Class objects for these classes.

You also need to compile against a version of the core API whose version is less than or equal to the version supported by the framework implementation. It looks like you compiled against core 5.0 while the framework implementation is for core 4.3 (or some other version below 5.0).

Upvotes: 4

Related Questions