Home > general > Grails on OSGi: Released Grails OSGi plugin 0.2

Grails on OSGi: Released Grails OSGi plugin 0.2

I just released version 0.2 of my OSGi plugin for Grails. The OSGi plugin is released under the Apache License 2.0.

The Grails OSGi plugin adds support to package and run a Grails application as an OSGi bundle. The bundle(s) may be run in an ad hoc assembled OSGi container or deployed to an external OSGi app server such as SpringSource DM Server, Eclipse Virgo, or Apache Karaf.

See my blog series for earlier thoughts and information for running a Grails application in an OSGi environment as well as concepts of and basic information on OSGi.

This post and the following installments of the series will show how to use the plugin and present some examples.

Installation

Simply call grails install-plugin osgi to install the OSGi plugin.

Usage

Creating an OSGi bundle from the Grails application

grails bundle
grails prod bundle

Running the bundle

grails run-bundle
grails prod run-bundle

The application can be accessed at http://localhost:8080/myapp-0.1/.

Note: at first start the OSGi runtime is assembled, which may take some time, while Ivy is downloading the Internet…

Note: if the bundle exists, it won’t be re-created automatically after changes. So for now use parameter -forceBundle after changing something in the Grails app: grails run-bundle -forceBundle.

Accessing the bundle context

The bundle context is the primary interface to the OSGi framework. It can be used to find and register services and interact the environment.

The bundle context is available from Grails’ parent ApplicationContext as bean bundleContext. It can be injected into all Spring-managed beans, i.e. all Grails artifacts, like Controllers, Services, etc.

In order to get a reference to the BundleContext, an artifact needs to define a reference with the name bundleContext.

Example: controller accessing bundle context

class OsgiTestController {
    def bundleContext
    def index = {
        def bundles = []
        if (bundleContext) {
            bundles = bundleContext.bundles
        }
        else {
            flash.message = "bundleContext NOT available"
        }
        [ bundles: bundles ]
    }
}

Changes since version 0.1

  • using Spring DM instead of Pax Web as Web extender
  • updated Apache Felix WebConsole
  • replaced (most) dependencies from bundle by OSGi bundles from the SpringSource Enterprise Bundle Repository (EBR)
  • provide access to OSGi BundleContext to artifacts (controllers, services, …) via Spring injection as bean bundleContext

How it works

In order to be a valid OSGi bundle, the application is war’ed and provided with the necessary bundle manifest headers (see scripts/_Events.groovy for details).
The bundle can be created using grails bundle.

The OSGi runtime is assembled in the target/osgi directory and uses the Eclipse Equinox OSGi framework, Spring DM.

In the current version, most libraries from WEB-INF/lib are replaced by equivalent OSGi bundles from the SpringSource Enterprise Bundle Repository (EBR). Only Hibernate and Grails libs retain in the bundle.

Grails jars already are OSGi bundles, but they cannot currently be installed cleanly in a OSGi container, as they contain circular dependencies and some other dependencies can not be satisfied (e.g. the Radeox library used by the Grails Wiki is no longer maintained, but it shouldn't be referenced from the Grails runtime anyway...).

There are bundles for Hibernate but due to a bug (possibly in Equinox), Hibernate Cache can not be resolved by the Spring ORM module (see "SPR-7003":https://issues.springsource.org/browse/SPR-7003 for a description of this problem).

The parent ApplicationContext has been replaced in web.xml by an OSGi-aware variant: OsgiBundleXmlWebApplicationContext. It knows how to export and import OSGi services as/from Spring beans through Spring XML files. These features are currently hidden, but they will be exposed in the next version of the OSGi plugin and made easier through the BeanBuilder DSL and the OSGi namespace.

Roadmap

  • make bundle generation configurable (e.g. include/exclude dependencies, ...)
  • make OSGi runtime created by grails run-bundle and grails assemble-osgi-runtime configurable
  • support auto-reloading of changed artifacts
  • create sub class of GrailsApplicationContext, which implements ConfigurableOsgiBundleApplicationContext
  • export main Grails beans and application context as OSGi service
  • export services as OSGi service (via static expose = 'osgi')
  • implement deployment to SpringSource DM Server or its successor Eclipse Virgo
  • implement deployment to Apache Karaf
  • Spring DM has been donated to Eclipse, so it should be replaced by its successor Eclipse Gemini

Getting around the OSGi runtime

Web Console

The "Felix Web Management Console":http://felix.apache.org/site/apache-felix-web-console.html provides excellent insight into the inner workings. It can be accessed at http://localhost:8081/system/console/ with user "admin" and password "admin" (Note: the web console runs on a different port!).

Shell Console

The command grails run-bundle drops the user in the Equinox Shell (press RETURN if you don't see the osgi> prompt). Running grails run-bundle -remoteConsole [port] opens a console with telnet access on the specified port or on port 8023, if omitted. Only a single user can use the console at any time.

Fork me on GitHub
Categories: general Tags:
  1. Klaus Baumecker
    July 23rd, 2010 at 15:47 | #1

    Hi Wolfgang,

    I have trouble to install the latest version of the plugin. The dependency resolution stops at the following packages:

    org.springframework.osgi#spring-osgi-core;2.0.0.M1: not found
    org.springframework.osgi#spring-osgi-io;2.0.0.M1: not found

    Do I need to add an extra maven repo?

    Regards,
    klaus.

  2. jetztgradnet
    July 24th, 2010 at 12:46 | #2

    Hi Klaus,

    Spring DM 2.0.0.M1 seems to have vanished from SpringSource Enterprise Bundle Repository (EBR). I will shortly release a new version with fixed dependency resolution.

    Regards,

    Wolfgang

  3. jetztgradnet
    July 27th, 2010 at 00:30 | #3

    Hi Klaus,

    I just released an updated plugin, see new blog post regarding release of Grails OSGi plugin 0.2.1

    Regards,

    Wolfgang

  1. July 12th, 2010 at 21:03 | #1
  2. July 13th, 2010 at 06:32 | #2