29

I'd like to be able to easily start an OSGi framework (preferably Equinox) and load up any bundles listed in my pom from a java main.

Is this possible? If so, how?

It seems like the pax tools would do this, but I can't seem to find any documentation indicating such. I know I can start up Equinox like so:

BundleContext context = EclipseStarter.startup( ( new String[] { "-console" } ), null );

But I'd like to do more - like I said: load more bundles in, maybe start some services, etc.

Cœur
  • 37,241
  • 25
  • 195
  • 267
javamonkey79
  • 17,443
  • 36
  • 114
  • 172

3 Answers3

55

Any OSGi framework (R4.1 or later) can be started programmatically using the FrameworkFactory API:

ServiceLoader<FrameworkFactory> ffs = ServiceLoader.load(FrameworkFactory.class);
FrameworkFactory ff = ffs.iterator().next();
Map<String,Object> config = new HashMap<String,Object>();
// add some params to config ...
Framework fwk = ff.newFramework(config);
fwk.start();

The OSGi framework is now running. Since Framework extends Bundle you can call getBundleContext and call all of the normal API methods to manipulate bundles, register services, etc. For example

BundleContext bc = fwk.getBundleContext();
bc.installBundle("file:/path/to/bundle.jar");
bc.registerService(MyService.class.getName(), new MyServiceImpl(), null);
// ...

Finally you should simply wait for the framework to shutdown:

fwk.stop();
fwk.waitForStop(0);

To reiterate, this approach works for any OSGi framework including Equinox and Felix just by putting the framework JAR on the classpath.

edwardw
  • 12,652
  • 3
  • 40
  • 51
Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
  • That's quite helpful, thanks. I was struggling with how to start and stop the framework (redirecting System.in is not that appealing). My only holdout on accepting is that I'd like to be able to load my bundles from my maven classpath without having to hardcode a bunch of paths and whatnot. Then again, I may be wishing for too much :) – javamonkey79 Jan 12 '11 at 21:29
  • What do you mean by the Maven classpath? – Neil Bartlett Jan 12 '11 at 22:18
  • Really any dependencies in my POM - I'd like to be able to have them detected and installed. – javamonkey79 Jan 12 '11 at 23:32
  • Well you need to get that information out of the POM. That's a Maven question. Does Maven offer an API you can call? Or can you just parse the XML? I have no idea about that. EDIT: the maven URL handler that KitsuneYMG referenced (see his comment) looks promising. Install that as a service then do `installBundle("mvn://repo/groupId/artifactId")`. Bear in mind though that at runtime you will need all transitive dependencies, not just first level build-time dependencies. – Neil Bartlett Jan 13 '11 at 10:39
  • What about in this case ... http://stackoverflow.com/questions/6522285/nullpointerexception-when-trying-to-programaticaly-install-a-bundle-in-equinox – Boltimuss Jun 29 '11 at 15:59
  • Hoe can you get the same bundlecontext but witouht starting a new framework instance and getting the current one instead. – user2133558 Feb 04 '20 at 21:46
  • @user2133558 Please clarify what you are asking, I didn't understand. Probably it should be posted as a new StackOverflow question rather than as a comment on an 9 year old answer. – Neil Bartlett Feb 06 '20 at 11:35
6

This thread might be a bit stale, but anyway...

Pax has excellent support for maven urls, it even has a wrap url handler allowing you to dynamically convert non-osgi jar to nice tidy bundles.

http://wiki.ops4j.org/display/paxurl/Mvn+Protocol

    <dependency>
        <groupId>org.ops4j.pax.url</groupId>
        <artifactId>pax-url-wrap</artifactId>
        <version>1.2.5</version>        
    </dependency>
    <dependency>
        <groupId>org.ops4j.pax.url</groupId>
        <artifactId>pax-url-mvn</artifactId>
        <version>1.2.5</version>        
    </dependency>

The command would then be:

install -s mvn:groupId:artifactId:version:classifier

Note: chicken-egg scenario - you have to install these using a file: url handler first or put them into an autodeploy directory.

Karaf has this all build in to it's distro, so maybe have a look at Karaf launcher source?

2nd note: deploying snapshots are enable by appending @snapshots to the repo URL, configuration is managed via ConfigAdmin

In terms of managing all your POM defined dependencies have a look at Karaf features - there's a plugin that would enable to generate a features XML file which can then be used to deploy your entire app: http://karaf.apache.org/manual/2.1.99-SNAPSHOT/developers-guide/features-maven-plugin.html

Further more this XML artifact can be deployed to your OBR, so you can take a vanilla Felix/Equinox/Karaf setup, add the mvn url handler and configure with your company's mvn repo then provision the entire app =)

earcam
  • 6,662
  • 4
  • 37
  • 57
4

Edit: Realized you wanted to start from inside java. Shame on me for not reading close enough

Check out this link. http://www.eclipsezone.com/eclipse/forums/t93976.rhtml

Essentially

public static void main(String args[]) throws Exception {
  String[] equinoxArgs = {"-console","1234","-noExit"};
  BundleContext context = EclipseStarter.startup(equinoxArgs,null);
  Bundle bundle = context.installBundle(
    "http://www.eclipsezone.com/files/jsig/bundles/HelloWorld.jar");
  bundle.start();
}

Edit: Maven

It seems that https://groups.google.com/group/spring-osgi/web/maven-url-handler?pli=1 contains an OSGi URl Handlers Service that can take URLS of the following format and load bundles from them ( mvn://repo/bundle_path )

KitsuneYMG
  • 12,753
  • 4
  • 37
  • 58
  • I've got most of this already, and had considered loading from an http url too - do you happen to know how to load bundles from maven? – javamonkey79 Jan 12 '11 at 21:30
  • @javamonkey79 I've never used maven. But, https://groups.google.com/group/spring-osgi/web/maven-url-handler?pli=1 looks promissing – KitsuneYMG Jan 12 '11 at 21:32
  • @KitsuneYMG: The google link is broken.. Do you have any other link that I can use? Thanks for the help.. – arsenal Aug 22 '13 at 20:02
  • @javamonkey79 Sorry. I haven't kept up with this stuff since I was moved to projects on different languages. A quick search didn't turn up anything though. – KitsuneYMG Aug 23 '13 at 14:20