tags:

views:

681

answers:

4

Hi there!

Can anybody give me an example how to use the osgi framework classes? I haven't a clue how to use those classes ...

BR,

Markus

+2  A: 

The specification does not define how to instantiate, configure and start an OSGi framework. Therefore running OSGi framework from your usual Java code is always specific for the given framework implementation (Equinox, Felix, Knopplerfish,...).

It's reasonably easy to embed Apache Felix (an open-source OSGi framework) into your application.

See http://felix.apache.org/site/launching-and-embedding-apache-felix.html for more information.

Pavol Juhos
I'm not sure if I really want to use another OSGi framework beside Equinox. I'm searching for an example that explains how to use the class org.eclipse.osgi.framework.internal.core.OSGi. Til now I only found an incomplete example in a german forum: http://www.java-forum.org/plattformprogrammierung/81133-custom-open-services-gateway-initiative-launcher-mit-config-ini.html
Markus
You should not use classes from the package you mentioned. This package is "internal" and not exported from the bundle.Access to the OSGi things in equinox is normally performed via the intefaces in org.osgi.framework.
jens
+2  A: 

See the project equinox-headless-service. It has code to launch equinox.

jassuncao
+5  A: 

It dependes on which OSGi implementation you are using. I use Eclipse Equinox and start the framework from within a regular java class. The Eclipse jar (called org.eclipse.osgi_longversion.jar) has a class called org.eclipse.core.runtime.adaptor.EclipseStarter. This will boot your OSGi framework.

Properties props = new Properties();
// add some properties to config the framework
EclipseStarter.setInitialProperties(props);
BundleContext context = EclipseStarter.startup(new String[]{},null);

You need some properties to configure the framework. You can see all the documented properties here. Once you call startup, the BundleContext you receive is the System Bundle context, so you can install/start/stop bundles from here.

If you set all the properties, you won't have to pass any arguments to startup().

You can download all Equinox and other bundles from the Equinox website.

omerkudat
Thanks! Now I was able to run the equinox framework with some bundles from within my java code :) But I'm still a little bit confused: If I use the parameter -console to run the Equinox console and stop the system bundle afterwards, the thread doesn't stop until I send a command to the console. Mayber there some kind of loop inside the system bundle that waits for a new command?!?
Markus
Stopping bundles, even the System bundle, doesn't necessarily tell the application to shutdown. The console, when you issue "close", what it really does is to stop all the bundles, unregister services and handlers, etc, and then call System.exit(0).
omerkudat
Are there any special properties that must be set to be able to use the EclipseStarter class from command line? Currently it is only possible for me to use this class if I run my program directly from the IDE. If I export it to a jar file the starter class loads the system bundle instead of the osgi services bundle ... therefore it is not possible to install a bundle ...
Markus
These are the basic properties I set before starting the Framework:osgi.console= (if number, goto port, if empty, goto stdout)osgi.noShutdown=true (this is an app, do not close if not Eclipse)osgi.clean=true (clear the bundle cache on startup)osgi.bundles=(comma delimited list of paths to bundles to load)
omerkudat