Chapter 4. Developing Applications

Table of Contents

Anatomy of a Bundle
Creating PARs and Web Applications
PARs
Web Application Bundles
Creating Plans
Creating the Plan XML File
Artifact Names
Supplying Properties to Artifacts
Using the Plan
Plans and Scoping
Creating and Using Configuration Artifacts
Creating the Properties File
Updating Your Application
Adding the Configuration Artifact to a Plan
Using Spring, Spring DM, and Blueprint
Programmatic Access to the Servlet Context
Programmatic Access to Web Features
Web Application Manifest Processing
Working with Dependencies
Importing Libraries
Importing Bundles
Scoping Bundles in an Application
Defining Libraries
Installing Dependencies
Application Trace
Application Versioning

Applications that take advantage of the OSGi capabilities of Virgo are typically comprised of multiple bundles. Each bundle may have dependencies on other bundles. Furthermore, each bundle exposes only certain packages and services. In this chapter, we look at how to create bundles, import and export appropriate functionality, and create artifacts to deploy web applications on the Virgo Server for Apache Tomcat.

Anatomy of a Bundle

Tip

This is an abbreviated introduction to OSGi bundles. Please refer to the Spring Dynamic Modules for OSGi documentation for full details.

An OSGi bundle is simply a jar file with metadata that describe additional characteristics such as version and imported and exported packages.

A bundle exports types and publishes services to be used by other bundles:

  • Types: via the OSGi Export-Package directive,

  • Services: via Spring-DM’s <service ... /> XML namespace element.

A bundle may import types and services exported by other bundles:

  • Types: via the OSGi Import-Package directive,

  • Services: via Spring-DM’s <reference ... /> XML namespace element.

Let’s see an example from the PetClinic sample application. The following listing shows the MANIFEST.MF file for the org.springframework.petclinic.infrastructure.hsqldb bundle.

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: PetClinic HSQL Database Infrastructure
Bundle-SymbolicName: org.springframework.petclinic.infrastructure.hsqldb
Bundle-Version: 1.0
Import-Library: org.springframework.spring;version="[2.5,2.6]"
Import-Bundle: com.springsource.org.apache.commons.dbcp;version="[1.2.2.osgi,1.2.2.osgi]",
 com.springsource.org.hsqldb;version="[1.8.0.9,1.8.0.9]"
Import-Package: javax.sql
Export-Package: org.springframework.petclinic.infrastructure
		

The org.springframework.petclinic.infrastructure.hsqldb bundle expresses its dependencies on the javax.sql package, the Commons DBCP and HSQLDB bundles, and the Spring library (we will examine the details of the library artifact in Defining Libraries). The Commons DBCP bundle is imported at a version of exactly 1.2.2.osgi and the HSQLDB bundle is imported at a version of exactly 1.8.0.9. The Spring library is imported at a version between 2.5 inclusive and 2.6 exclusive.

Note that you do not specify the bundle that will provide the imported packages. Virgo will examine the available bundles and satisfy the required dependencies.

The following osgi-context.xml file from the PetClinic sample’s org.springframework.petclinic.repository.jdbc bundle declares a service published by the bundle and references a service published by another bundle.

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/osgi"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/osgi  
        http://www.springframework.org/schema/osgi/spring-osgi.xsd
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <service id="osgiClinic" ref="clinic" interface="org.springframework.petclinic.repository.Clinic" />
    <reference id="dataSource" interface="javax.sql.DataSource"/>

</beans:beans>

The service element publishes the clinic bean (a regular Spring bean declared in the module-context.xml file) and specifies org.springframework.petclinic.repository.Clinic as the type of the published service.

The reference elements define a dataSource bean that references a service published by another bundle with a an interface type of javax.sql.DataSource.