Applications that take advantage of the OSGi capabilities of the Virgo Tomcat Server 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 Tomcat Server.
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:
via the OSGi
<service ... /> XML namespace element.
A bundle may import types and services exported by other bundles:
via the OSGi
<reference ... /> XML namespace element.
Let’s see an example from the PetClinic sample application. The following listing shows the
MANIFEST.MF file for the
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="[184.108.40.206,220.127.116.11]" Import-Package: javax.sql Export-Package: org.springframework.petclinic.infrastructure
org.springframework.petclinic.infrastructure.hsqldb bundle expresses its dependencies on
javax.sql package, the Commons DBCP and HSQLDB bundles, and the Spring library (we will examine
the details of the library artifact in the section called “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
18.104.22.168. 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. The Virgo Tomcat Server will examine the available bundles and satisfy the required dependencies.
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>
service element publishes the
(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.
reference elements define a
dataSource bean that references
a service published by another bundle with a an interface type of