Hello, OSGi, Part 2: Introduction to Spring Dynamic Modules

Build a service-oriented application using Spring and OSGi

1 2 3 4 5 6 7 Page 2
Page 2 of 7

In Spring DM, at the time of startup the org.springframeork.osgi.bundle.extender queries all of the existing bundles in the resolved state to learn if any of them is Spring powered. The extender bundle considers a bundle to be Spring powered if it has the Spring-Context manifest header, or if it has XML files in its META-INF/spring folder.

Upon finding a Spring-powered bundle, the org.springframeork.osgi.bundle.extender loads the Spring configuration files in that bundle to create an application-context object for it. While creating the application-context object, the extender also checks whether the bundle is exporting any OSGi services. If so, it exports that Spring bean to the OSGi shared-service registry. If a bundle is importing an OSGI service, it finds the service and adds it as a normal Spring bean in the application context for that bean. The extender also registers a listener for bundle events so that whenever any OSGi bundle goes into a resolved state it checks whether it is Spring powered and follows the same steps.

All of this will become more clear as you develop your first Spring DM application, starting with setting up the environment in the next section.

Setting up your Spring DM development environment

The Spring Dynamic Modules framework 1.0 supports the development of OSGi bundles compliant with OSGi Specification Release 4 and above and JDK 1.4 and above. Spring DM is also tested against the three open source OSGi container implementations: Equinox 3.2.2, Felix 1.0.1, and Knopflerfish 2.0.3, so you can execute your code in any of them.

In developing the examples for this article I have used Eclipse 3.3, which has the Equinox 3.3 OSGi container embedded. The Eclipse IDE provides excellent support for developing, executing, and debugging OSGi bundles. I tested the sample code for this article using JDK 1.5. Please follow these steps for setting up your development environment using the Eclipse IDE:

  1. Download Spring DM, which is in release 1.0.2 at the time of this writing. Be sure to download the spring-osgi-1.0.2-with-dependencies.zip or most current equivalent.
  2. Extract the contents of spring-osgi-1.0.2-with-dependencies.zip onto your machine, say in your c:\software folder.
  3. Start up your Eclipse IDE. When asked to select a workspace, select an empty folder such as c:\sample\springdm.
  4. Import all the necessary Spring DM JARs for the project. Right-click in the Package Explorer and click on Import --> Plug-in Development --> Plug-ins and Fragments, then click Next. The Import Plug-ins and Fragments dialog will open.
  5. In this dialog, uncheck the target platform checkbox (as specified in the Preferences).
  6. The Browse button for the Plug-in Location should now be enabled; click on it and select C:\software\spring-osgi-1.0.2\dist, as shown in Figure 1.

    Importing Spring DM in Eclipse.
    Figure 1. Importing Spring DM JARs (click to enlarge)
  7. Click Next and the Selection dialog will open; add the following three plug-ins to your Plug-ins and Fragments to import:
    • org.springframeork.osgi.bundle.core
    • org.springframeork.osgi.bundle.extender
    • org.springframeork.osgi.bundle.io
    Now click Finish. Eclipse will import these three bundles into your workspace, where you should be able to see them in the Package Explorer view.
  8. Next, you want to import few more bundles from C:\software\spring-osgi-1.0.2\lib, so go back and repeat Steps 4 and 5. In Step 6, choose a plug-in location equal to C:\software\spring-osgi-1.0.2\lib and click Next.
  9. In the Selection dialog you will see more plug-ins available; select the following:
    • org.springframeork.bundle.spring.aop
    • org.springframeork.bundle.spring.beans
    • org.springframeork.bundle.spring.context
    • org.springframeork.bundle.spring.core
    • org.springframeork.bundle.spring.jdbc
    • org.springframeork.bundle.spring.tx
    • org.springframeork.osgi.aopalliance.osgi
    • backport-util-concurrent.osgi-3.0-SNAPSHOT.jar (if you're using JDK 1.4)
    Click Finish when you're done and Eclipse will import these seven bundles into your workspace.

Apache Commons Logging is the logging framework for Spring DM. Spring DM provides Apache Commons Logging, as well as Apache Log4j JARs, in the OSGi bundle format. It seems there is some issue with these bundles, however, for I was not able to import them using Eclipse. If you encounter the same problem, take the following steps to create your own OSGi bundle containing these two JARs:

  1. From within Eclipse, click on File --> New --> Project -> Plug-in Development --> Plug-in from existing JAR archives, then click Next. Eclipse will open the JAR Selection dialog.
  2. In JAR Selection, click Add External and you should get the File Selection dialog box. Here you can select commons-logging.jar and log4j-1.2.14.jar. Your JAR Selection dialog should look like the screenshot in Figure 2.

    Select the JARs to include in the plugin.
    Figure 2. Select the JARs to include in the plugin (click to enlarge)
    Click Next and Eclipse will take you to the Plug-in Project Properties dialog.
  3. In the Plug-in Project Properties dialog, enter a project name -- something like org.apache.commons.logging. In the Target Platform section select OSGi Framework --> Standard and click Finish.

With this you have set up your Eclipse environment for developing Spring Dynamic Modules plugins. Are you ready to try developing a Hello World application using Spring DM?

1 2 3 4 5 6 7 Page 2
Page 2 of 7