blob: 9751ccb0808afbf5e0a0dce867eae3bd7a98e0e2 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<section id="osgi.service">
<title>Publishing an OSGi Service</title>
<para>
At the end of the previous step, a dependency was created on an OSGi Service Registry exposed instance of
<classname>greenpages.Directory</classname>. The application would not start because no other bundle was
contributing an instance of this service to the Service Registry.
</para>
<para>Stop the server instance before proceeding.</para>
<section id="osgi.service.implementation">
<title>Add Implementation</title>
<para>
In this step Spring&rsquo;s context scanning is added which will create an instance of the
<classname>DirectoryImpl</classname> class.
</para>
<para>
Open the <classname>greenpages.internal.DirectoryImpl</classname> class in the <literal>greenpages.app</literal>
project. Add the <interfacename>@Component</interfacename> annotation to the class:
<programlisting language="java"><![CDATA[@Component("directory")
public class DirectoryImpl implements Directory {
…]]>
</programlisting>
generating imports with Eclipse&rsquo;s help if necessary.
</para>
<para>
Open the <filename>META-INF/spring/module-context.xml</filename> in the <literal>greenpages.app</literal>
project. Add component scanning to this file:
<programlisting language="xml"><![CDATA[<context:component-scan base-package="greenpages.internal"/>]]>
</programlisting>
</para>
<para>
When complete, go to the next step.
</para>
</section>
<section id="osgi.service.publish">
<title>Publish OSGi Service</title>
<para>
In this step the <classname>DirectoryImpl</classname> instance is published to the OSGi Service
Registry.
</para>
<para>
Open the <filename>META-INF/spring/osgi-context.xml</filename> file. Add the
<literal><![CDATA[<osgi:service/>]]></literal> tag to publish the <literal>directory</literal> bean with an
interface of <interfacename>greenpages.Directory</interfacename>.
<programlisting language="xml"><![CDATA[<osgi:service ref="directory" interface="greenpages.Directory"/>]]>
</programlisting>
</para>
</section>
<section id="osgi.server.working">
<title>A Working Web Application</title>
<para>
Start (or restart) the @webserv@ instance from the <literal>Servers</literal> view. If the @greenpages@ PAR was not
removed before, it will be automatically deployed, otherwise deploy it as before. There should be no errors reported.
When @greenpages@ is deployed successfully, open a web browser and navigate to
<ulink url="http://localhost:8080/greenpages">http://localhost:8080/greenpages</ulink>. On
the home page type <literal>wilkinson</literal> into the search field and press <emphasis>Submit</emphasis>.
Unlike the previous attempt, this should return a list (of size 1) of search results. From here, select
<emphasis>view</emphasis> to get the <quote>detailed</quote> listing.
<mediaobject>
<imageobject role="fo">
<imagedata fileref="images/web-module/greenpages-listing-success.png" format="PNG" align="center" width="15cm"/>
</imageobject>
<imageobject role="html">
<imagedata fileref="images/web-module/greenpages-listing-success.png" format="PNG" align="center"/>
</imageobject>
</mediaobject>
This uses a stub implementation of the <classname>Directory</classname> interface which only knows about <quote>Andy Wilkinson</quote>.
</para>
<para>
The web interface is complete enough. Go to the next chapter to see the middle tier implementation.
</para>
</section>
</section>