views:

239

answers:

4

Hello, everyone!

I have successfully managed to start Apache Felix from code and register an own Bundle.

Following relation between OSGI-projects is needed:

[OsgiInterface] -- provides interfaces.

[OsgiModuleA] -- (bundle) provides an implementation of those interfaces.
knows [OsgiInterface]

[OsgiUsage] -- makes use of one or more bundle.
knows [OsgiInterface] and [OsgiModuleA]

Now I have problems registering a service which implements an interface. I would guess that my entries in manifest.mf files are wrong.

Additional information

It would be very kind, if someone could look at the code in my previous question

Let me refer to this question:

I tried to create a third project OsgiInterfaces, which provides an interface SomeInterface in the package interfaces. This project is known by both OsgiModuleA and OsgiUsage.

OsgiModuleA: manifest.mf has now an additional value interfaces for the entry Import-Package:. Furthermore, there is an instance of SomeInterface provided to the activator.

When the bundle is started, an NoClassDefFoundError occurs: the interface SomeInterface is not known.

EDIT:

Now, that the error is fixed, I can tell, that the most important part was:

map.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
    "my.interfaces; version=1.0.0");

Without this, I got ClassCastException.

A: 

I would say use bnd directly or maven-bundle-plugin to create OSGI enabled jars.

It's easier than writing the OSGI manifest yourself(typos, mistakes, missing imports/exports)

Trying wrapping the jars with bnd as a start.

John Doe
Sorry, I can't use Maven, and I need to use plain OSGI standard.
java.is.for.desktop
+1  A: 

I understand that you have SomeInterface in another bundle, right? Then you must also export that package in that bundle's manifest, eg.

Export-Bundle: interfaces

But you really should have a look at the bnd tool mentioned in another answer. This generates standard OSGi manifests.

akr
Yes, I did `Export-Bundle: interfaces` already. Doesn't help. OK, perhaps I'll have a look at Bnd, or iPOJO (seems to be something in same direction).
java.is.for.desktop
Actually, one of the errors was, that I didn't exported `interfaces`.
java.is.for.desktop
+1  A: 

I suggest you look at the iPOJO project. This make using Felix much easier. http://felix.apache.org/site/apache-felix-ipojo.html

Peter Lawrey
+2  A: 

In the most basic form, services are registered in Java code, not using manifest or any other file. This usually happens in your BundleActivator.

Long      i     = new Long(20);    // the service implementation
Hashtable props = new Hashtable();
props.put("description", "This an long value");
bundleContext.registerService(Long.class.getName(), i, props);

I suggest you read a tutorial, like the one at Knopflerfish

An alternative is using Declarative Services, or the new Blueprint facility. Using either of these (or other non-standardized systems) you will declare your services in a (usually XML) file, instead of writing code to interact with the services registry.

But you should probably figure out the fundamentals manually first.

[OsgiUsage] -- makes use of one or more bundle. knows [OsgiInterface] and [OsgiModuleA]

It should not be necessary for the bundle that uses a service to know about the bundle that provides it. Both of them just need to know the service interface. In fact, bundles should not need to know about other bundles at all. They only need to import packages, and consume or provide services.

Thilo
Thanks, I'll try as soon as time permits!
java.is.for.desktop
Great! Works! Discovered, with the help of your information, that I had also to place my `interfaces` in an OSGI-bundle.
java.is.for.desktop