Introducing groovyx.osgi: OSGi DSL for Groovy
Programming for and in an OSGi environment is sometimes hard. Getting started is hard as well.
groovyx.osgi helps both setting up an OSGi environment and simplifies programming by providing support for easy access to OSGi services, etc.
Setting up an OSGi environment
Setting up an OSGi environment is hard work: you have to download an OSGi implementation (usually Apache Felix or Eclipse Equinox, but there are others as well). Then there are many dependencies to download. There is an excellent tool, which simplifies the startup part: Pax Runner. Pax Runner can resolve dependencies from URLs or Maven repositories and start the OSGi framework.
One problem remains: you need to download Pax Runner and the configuration is a (set of) static text file(s). To simplify things and allow scripting in the “recipes”, the module groovyx.osgi.runtime (README) provides a nice Groovy DSL for creating and starting an OSGi framework.
- provision bundles from filesystem, web, Maven repositories
- run Eclipse Equinox or Apache Felix OSGi runtimes
- flexible Groovy DSL, fully programmable
- lifecycle callback closures
Simplified Programming Model
The OSGi programming model is based on services (or micro-services, as Peter Kriens likes to call them), which are accessed via the BundleContext. Services are dynamic, they can come and go any time during the lifetime of an OSGi application. This is both a strength, as an application can be extended or updated without application shutdown, and a weakness, as it requires careful handling of service dynamics.
Looking up and using a service requires a lot of boiler plate code. A simple example in Java:
The equivalent Groovy code using module
groovyx.osgi looks like this:
- simplifies working with OSGi service registry
- safe access to OSGi services, without fear for OSGi dynamics
- Service finder DSL
- OSGi service filter DSL.
An overview on basic usage of Groovy with OSGi can be found at the Groovy Homepage.
Source code and binaries
You can either download
0.1 from GitHub or install it using Grape, Ivy, or Maven from the Maven repository at https://github.com/jetztgradnet/repository/raw/master.
A simple example using Grape:
This script is executable with nothing but Groovy (>= 1.7.5) installed.
The following items are planned for the next releases:
- simplified service registration
- simplified use of ServiceTracker, ManagedService, and ManagedServiceFactory
- improve docs
- add support for pre-defined provisioning profiles
- add support for OSGi ConfigurationAdmin
- improve handling of external OSGi runtimes
- improve docs
- migrate usage of Spring DM to its successor Eclipse Gemini Blueprint and Gemini Web
- add support for easy access to OSGi services
OSGi provides no silver bullet for modularization, but it greatly helps, once you wrapped your head around bundles, services and dependencies. It’s not always an easy road, but there are many helpful tools and people out there. I hope,
groovyx.osgi can help you to create great applications. Have fun playing in your OSGi sandbox and don’t forget to send me your thoughts. Any feedback is greatly appreciated, either in the comments or open a issue at GitHub.