8

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

Markus
  • 457
  • 1
  • 10
  • 20
  • duplicate of: http://stackoverflow.com/questions/4673406/programatically-start-osgi-equinox i would say, that the linked to question's accepted answer is also more fitting as answer to this question, the the ones listed on this page. – hoijui Oct 05 '11 at 09:51

6 Answers6

8

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
  • 9,371
  • 4
  • 33
  • 42
  • 1
    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 May 26 '09 at 17:32
  • 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 May 26 '09 at 17:48
  • 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 Jun 02 '09 at 16:19
  • 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 Jun 03 '09 at 09:00
5

In OSGi 4.1 this is standardized. Have a look at http://njbartlett.name/2011/03/07/embedding-osgi.html which explains how to embed any OSGi container.

Leen Toelen
  • 389
  • 6
  • 12
2

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
  • 1,867
  • 14
  • 15
  • 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 May 25 '09 at 09:28
  • 1
    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 May 26 '09 at 10:33
2

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

jassuncao
  • 4,695
  • 3
  • 30
  • 35
0

I recently created a simple proof-of-concept of how to embed Equinox OSGi runtime in Java. For details please follow my project in Github https://github.com/sarxos/equinox-launcher

Bartosz Firyn
  • 2,664
  • 2
  • 21
  • 18