Hacking on GraphHopper - a Java road routing engine. Peter has posted 62 posts at DZone. You can read more from them at their website. View Full User Profile

Plugable Swing – A Hello World OSGi Example

09.17.2009
| 13544 views |
  • submit to reddit

There are a lot of OSGi applications available, but a minority of them is aimed at the client side. Here I will list some on Swing:

  1. http://felix.apache.org/site/apache-felix-application-demonstration.html
  2. http://danielrohe.wordpress.com/2009/04/01/swing-application-on-osgi/
  3. http://www.toedter.com/blog/?p=33
  4. http://code.google.com/p/osgi-in-action/
  5. sip-communicator

Today I tried the same and had luck. It was very fast (~1h) to get all working what I wanted, because I shamelessly stole the code from the example of Apache Felix. It is even more easier than this example but should have the same effect: it shows how one could use OSGi even for a Swing application.

There is a host application called Swing On OSGi – Host with one JMenu which does not change (static) and one (dynamic) JMenu which could be changed from the available plugins (here only one: Swing On OSGi – PluginTest). The source code (Apache License 2) can be checkout via

svn checkout https://timefinder.svn.sourceforge.net/svnroot/timefinder/branches/swingosgi swingosgi

… you will need maven. After ‘mvn install’ for both projects you will only need to start the host application via your IDE or via

java -jar target/host-1.0.0.jar

Then if you see the “dynamic menu” with one menu entry (which was added from the plugin) all is fine:

helloOSGiWorld

If you have problems while building this send me an email or comment here.

I got the plugins even working in my timetabling application TimeFinder which uses the client-side framework called Spring Rich Client. The first plugin for Spring Rich Client - read more.

From http://karussell.wordpress.com

Published at DZone with permission of its author, Peter Karussell.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Mike P(Okidoky) replied on Thu, 2009/09/17 - 12:30pm

Without having to dive in and download and try out stuff, I'm assuming that you've been able to create a swing application with a dynamic component that can be "redeployed"?

Would this be a way to have dynamically re-programmable components in a running application without having to restart the host application?

 

Peter Karussell replied on Thu, 2009/09/17 - 1:56pm

Yes, exactly. It could be used for updates and plugins.

Per-Erik Svensson replied on Mon, 2009/09/21 - 3:38am

We actually use it for plugins. Not that we have too many plugins but the two we do have are communicating via OSGi. We are also moving over more and more of our own internal code to OSGi-bundles to get an easy way of updating. Good to see others that have made the observation that this is a great way of using OSGi/Felix.

Also, I'm kind of missing Equinox/Eclipse in the list of client-side uses of OSGi. ;)

http://www.2c8.com/en

Peter Karussell replied on Mon, 2009/09/21 - 10:59am

>We are also moving

Why are you moving? Advantages? 

> Also, I'm kind of missing Equinox/Eclipse in the list of client-side uses of OSGi. ;) 

yes, you can see examples with this in the next post, where I got OSGi it working with Spring Rich Client. Did you try to get it working with Spring DM or the mentioned alternatives (in the next post)? Or do you simply use directly the BundleContext interface?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.