| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>4. Developing Applications</title><link rel="stylesheet" href="css/stylesheet.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"><link rel="home" href="index.html" title="Virgo Programmer Guide"><link rel="up" href="index.html" title="Virgo Programmer Guide"><link rel="prev" href="ch03s04.html" title="3.4 A Guide to Forming Bundles"><link rel="next" href="ch04s02.html" title="4.2 Creating PARs and Web Applications"><!--Begin Google Analytics code--><script type="text/javascript"> |
| var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); |
| document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); |
| </script><script type="text/javascript"> |
| var pageTracker = _gat._getTracker("UA-2728886-3"); |
| pageTracker._setDomainName("none"); |
| pageTracker._setAllowLinker(true); |
| pageTracker._trackPageview(); |
| </script><!--End Google Analytics code--></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">4. Developing Applications</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch03s04.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ch04s02.html">Next</a></td></tr></table><hr></div><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="developing-applications"></a>4. Developing Applications</h2></div></div></div><p> |
| 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. |
| </p><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="developing-applications-bundle"></a>4.1 Anatomy of a Bundle</h2></div></div></div><div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Tip"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="images/tip.gif"></td><th align="left">Tip</th></tr><tr><td align="left" valign="top"><p> |
| This is an abbreviated introduction to OSGi bundles. Please refer to the |
| <a class="ulink" href="http://static.springframework.org/osgi/docs/current/reference/html/" target="_top"> |
| Spring Dynamic Modules for OSGi documentation</a> for full details. |
| </p></td></tr></table></div><p> |
| An OSGi bundle is simply a jar file with metadata that describe |
| additional characteristics such as version and imported and exported packages. |
| </p><p> |
| A bundle exports types and publishes services to be used by other bundles: |
| </p><div class="itemizedlist"><ul type="disc"><li><p> |
| <span class="bold"><strong>Types</strong></span>: |
| via the OSGi <code class="code">Export-Package</code> directive, |
| </p></li><li><p> |
| <span class="bold"><strong>Services</strong></span>: |
| via Spring-DM’s <code class="code"><service ... /></code> XML namespace element. |
| </p></li></ul></div><p> |
| A bundle may import types and services exported by other bundles: |
| </p><div class="itemizedlist"><ul type="disc"><li><p><span class="bold"><strong>Types</strong></span>: |
| via the OSGi <code class="code">Import-Package</code> directive,</p></li><li><p><span class="bold"><strong>Services</strong></span>: |
| via Spring-DM’s <code class="code"><reference ... /></code> XML namespace element.</p></li></ul></div><p> |
| Let’s see an example from the PetClinic sample application. The following listing shows the |
| <code class="filename">MANIFEST.MF</code> file for the <code class="code">org.springframework.petclinic.infrastructure.hsqldb</code> bundle. |
| </p><pre class="programlisting"> |
| Manifest-Version: 1.0 |
| <span class="bold"><strong>Bundle-ManifestVersion</strong></span>: 2 |
| <span class="bold"><strong>Bundle-Name</strong></span>: PetClinic HSQL Database Infrastructure |
| <span class="bold"><strong>Bundle-SymbolicName</strong></span>: org.springframework.petclinic.infrastructure.hsqldb |
| <span class="bold"><strong>Bundle-Version</strong></span>: 1.0 |
| <span class="bold"><strong>Import-Library</strong></span>: org.springframework.spring;version="[2.5,2.6]" |
| <span class="bold"><strong>Import-Bundle</strong></span>: 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]" |
| <span class="bold"><strong>Import-Package</strong></span>: javax.sql |
| <span class="bold"><strong>Export-Package</strong></span>: org.springframework.petclinic.infrastructure |
| </pre><p> |
| The <code class="code">org.springframework.petclinic.infrastructure.hsqldb</code> bundle expresses its dependencies on |
| the <code class="code">javax.sql</code> package, the Commons DBCP and HSQLDB bundles, and the Spring library (we will examine |
| the details of the library artifact in <a class="xref" href="ch04s08.html#developing-applications-defining-libraries" title="Defining Libraries">the section called “Defining Libraries”</a>). 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. |
| </p><p> |
| 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. |
| </p><p> |
| The following <code class="filename">osgi-context.xml</code> file from the PetClinic sample’s |
| <code class="code">org.springframework.petclinic.repository.jdbc</code> bundle declares a service published by the bundle and |
| references a service published by another bundle. |
| </p><pre class="programlisting"><<span class="hl-tag">?xml version="1.0" encoding="UTF-8"?</span>> |
| <<span class="hl-tag">beans:beans</span> <span class="hl-attribute">xmlns</span>=<span class="hl-value">"http://www.springframework.org/schema/osgi"</span> |
| <span class="hl-attribute">xmlns:xsi</span>=<span class="hl-value">"http://www.w3.org/2001/XMLSchema-instance"</span> |
| <span class="hl-attribute">xmlns:beans</span>=<span class="hl-value">"http://www.springframework.org/schema/beans"</span> |
| <span class="hl-attribute">xsi:schemaLocation</span>=<span class="hl-value">"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"</span>> |
| |
| <<span class="hl-tag">service</span> <span class="hl-attribute">id</span>=<span class="hl-value">"osgiClinic"</span> <span class="hl-attribute">ref</span>=<span class="hl-value">"clinic"</span> <span class="hl-attribute">interface</span>=<span class="hl-value">"org.springframework.petclinic.repository.Clinic"</span> /> |
| <<span class="hl-tag">reference</span> <span class="hl-attribute">id</span>=<span class="hl-value">"dataSource"</span> <span class="hl-attribute">interface</span>=<span class="hl-value">"javax.sql.DataSource"</span>/> |
| |
| <<span class="hl-tag">/beans:beans</span>></pre><p> |
| The <code class="code">service</code> element publishes the <code class="code">clinic</code> bean |
| (a regular Spring bean declared in the <code class="filename">module-context.xml</code> file) and specifies |
| <code class="code">org.springframework.petclinic.repository.Clinic</code> as the type |
| of the published service. |
| </p><p>The <code class="code">reference</code> elements define a <code class="code">dataSource</code> bean that references |
| a service published by another bundle with a an interface type of <code class="code">javax.sql.DataSource</code>. |
| </p></div></div><!--Begin LoopFuse code--><script src="http://loopfuse.net/webrecorder/js/listen.js" type="text/javascript"></script><script type="text/javascript"> |
| _lf_cid = "LF_48be82fa"; |
| _lf_remora(); |
| </script><!--End LoopFuse code--><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch03s04.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ch04s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">3.4 A Guide to Forming Bundles </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 4.2 Creating PARs and Web Applications</td></tr></table></div></body></html> |