JavaFX and OSGi, part 2

In my previous blog entry, I described a method to execute an OSGi frameworkin a JavaFX runtime. In this entry, I show how you can write OSGi Bundlescontaining JavaFX code, and how they can be installed into the OSGiplatform running in the JavaFX runtime.

The idea behind this code might be a bit strange and dumb (why would onewant to write an OSGi bundle containing JavaFX and deploy it in anOSGi container on top of JavaFX?) but I have two reasons whythis is really a practicaluse case.
In the hopefully not too distant future, we might have moredevices that come with JavaFX preinstalled. My HTC Diamond phone, for example,has a JavaFX platform. If I want to install an OSGi framework onto this phone,it has to run on top of the JavaFX platform.
Also, more developers are getting used with OSGi. The JavaFX platform providesinteresting front-end capabilities for OSGi based services. With JavaFX,front-end development is in general much easier than with any (A)WTframework I know.

The OsgiFX framework

Running an OSGi framework on top of JavaFX is described inthis blogentry, and the code can be found athttp://code.google.com/p/osgifx.Note that I made some major changes to the code since I wrote the previousblog entry — I hope to find some time to discuss those in a next entry.

When starting the OSGiFX application, an OSGi Framework is created(I use the Knopflerfish framework),and an org.osgifx.FxServiceListener instance is registered,listening for implementations of org.osgifx.view.JavaFxView
This interface acts as a bridge between the registered OSGi services andthe OSGiFX application that will manage and display the services.JavaFxView is a plain Java Interface, with two methods:

  • getTitle() provides the title of the registered application
  • getRootNode() provides a javafx node that is the container for allui-stuff in the application.

The OSGiFX application maintains a list of registered JavaFxView implementations,and will display an application when the user clicks on a name — yes, it issort of a Service Browser on top of OSGi.

Applications

I wrote a silly OSGi bundle that registers a JavaFXView implementationcontaining a JavaFX custom node with a nice circle.
You can find the code and manifest file athttp://lodgon.com/johan/javafxbundle.tarAgain, I hope to find more time later to document this better, but I thinkthe code in the BundleActivator is self-explaining:

  override function start (bc: BundleContext) {    println ("[JVDBG] Starting bundledemoactivator");    var view: MyView = MyView{};    var impl: JavaFxViewImpl = new JavaFxViewImpl (view);    var props :Dictionary= new Properties();    bc.registerService(JavaFxView.class.getName(),impl,props);  }

The MyView class contains a CustomNode with the circle, and theJavaFxViewImpl class will return this CustomNode when its getRootNode()method is called.

When bundling this code into an OSGi bundle, we have to make sure we importthe needed packages. The JavaFX compiler creates some dependencies onimplementation packages, these should be exported by the OSGiFX platformand imported by the bundles that rely on them.

This was a very quick intro to what I have been doing with OSGi and JavaFX,feel free to mail me if you have questions or remarks (johan at lodgon dot com).