Currently I’m fooling around with Groovy a little and since I’ve got some OSGi-Applications I was wandering if I could use Groovy to write some OSGi-Services. Here are my thoughts:

  • Start of with getting the Groovy-Plugin for your Eclipse-IDE:
  • Create Groovy-library-bundle: Download Groovy. Create a bundle: File | New | Project | Plug-in Development | Plug-in from existing JAR archives and select the Jar: groovy-all-1.0.jar from the embeddable directory from your Groovy distribution. A good name for this bundle would probably be org.codehaus.groovy.
  • Next create your bundle that leverages this Groovy bundle: Create a new OSGi-Bundle: File | New | Project | Plug-in Project, select that it’s supposed to be targeted on an OSGi-framework and give it a nice name (com.luebken.groovyservice). In this new Bundle add the dependency to our groovy-library-bundle in the Require-Bundle: org.codehaus.groovy
  • Now right-click on the new project and select Groovy | Add Groovy-Nature. Since we import the Groovy classes from our library-bundle remove newly imported library from the Java-Build-Path: Project | Properties | Java Build-Path | Libraries (commons-io, commons-lang, groovy-all). Make sure that the Groovy-Scripts are compiled to some folder like “bin-groovy” (Project | Properties) and that the Groovy-compiler is enabled (Window Preferences).
  • Lets create our GroovyService (File | New | Groovy | Groovy Class) and some cool functionality like: class GroovyService { void sayHello(){ println "A groovy hello to the world" } }
  • Next use this service in the start-method of the activator: public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception { new GroovyService().sayHello(); }
  • The last thing you have to do is adding bin-groovy/ to the classpath in the Bundle-ClassPath: bin-groovy/, .
  • Now you can start the OSGi-Framewirk with the two bundles and you should see: osgi> A groovy hello to the world.
  • If you don’t want to code yourself see:
If you know OSGi, than you are waiting for the real OSGi-Service. To all the others: OSGi has its own service mechanism to register and find services. But this shouldn’t be a problem:
  • First create a second bundle that uses the service like: com.luebken.groovyservice.client. Than export the package from the GroovyService in the original bundle and import it in the new client-bundle.
  • Now all you have to do is register the service in the activator class (com.luebken.groovyservice) : public void start(BundleContext context) throws Exception { groovyService = new GroovyService(); context.registerService(GroovyService.class.getName(), groovyService, null); }
  • and get the service (com.luebken.groovyservice.client): public void start(BundleContext context) throws Exception { serviceReference = context.getServiceReference(GroovyService.class.getName()); GroovyService service = (GroovyService) context.getService(serviceReference); service.sayHello(); }
  • Again if you don’t want to code get the sources:
So will we code our OSGi services in Groovy? What do you think?

Update 27.01.:
As John Wilson pointed out there is no reason why the Activator shouldn't be a Groovy-class. See the following source, where the Activator in the client is a real Groovy Class: