| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>4.7 Publishing an OSGi Service</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="Creating an application with EclipseRT Virgo Web Server"><link rel="up" href="ch04.html" title="4. The Web Module"><link rel="prev" href="ch04s06.html" title="4.6 Referencing an OSGi Service"><link rel="next" href="ch05.html" title="5. The Middle Tier"><!--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.7 Publishing an OSGi Service</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch04s06.html">Prev</a> </td><th width="60%" align="center">4. The Web Module</th><td width="20%" align="right"> <a accesskey="n" href="ch05.html">Next</a></td></tr></table><hr></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="osgi.service"></a>4.7 Publishing an OSGi Service</h2></div></div></div><p> |
| At the end of the previous step, a dependency was created on an OSGi Service Registry exposed instance of |
| <code class="classname">greenpages.Directory</code>. The application would not start because no other bundle was |
| contributing an instance of this service to the Service Registry. |
| </p><p>Stop the server instance before proceeding.</p><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="osgi.service.implementation"></a>Add Implementation</h3></div></div></div><p> |
| In this step Spring’s context scanning is added which will create an instance of the |
| <code class="classname">DirectoryImpl</code> class. |
| </p><p> |
| Open the <code class="classname">greenpages.internal.DirectoryImpl</code> class in the <code class="literal">greenpages.app</code> |
| project. Add the <code class="interfacename">@Component</code> annotation to the class: |
| </p><pre class="programlisting">@Component(<span class="hl-string">"directory"</span>) |
| <span class="hl-keyword">public</span> <span class="hl-keyword">class</span> DirectoryImpl <span class="hl-keyword">implements</span> Directory { |
| … |
| </pre><p> |
| generating imports with Eclipse’s help if necessary. |
| </p><p> |
| Open the <code class="filename">META-INF/spring/module-context.xml</code> in the <code class="literal">greenpages.app</code> |
| project. Add component scanning to this file: |
| </p><pre class="programlisting"><<span class="hl-tag">context:component-scan</span> <span class="hl-attribute">base-package</span>=<span class="hl-value">"greenpages.internal"</span>/> |
| </pre><p> |
| </p><p> |
| When complete, go to the next step. |
| </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="osgi.service.publish"></a>Publish OSGi Service</h3></div></div></div><p> |
| In this step the <code class="classname">DirectoryImpl</code> instance is published to the OSGi Service |
| Registry. |
| </p><p> |
| Open the <code class="filename">META-INF/spring/osgi-context.xml</code> file. Add the |
| <code class="literal"><osgi:service/></code> tag to publish the <code class="literal">directory</code> bean with an |
| interface of <code class="interfacename">greenpages.Directory</code>. |
| </p><pre class="programlisting"><<span class="hl-tag">osgi:service</span> <span class="hl-attribute">ref</span>=<span class="hl-value">"directory"</span> <span class="hl-attribute">interface</span>=<span class="hl-value">"greenpages.Directory"</span>/> |
| </pre><p> |
| </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="osgi.server.working"></a>A Working Web Application</h3></div></div></div><p> |
| Start (or restart) the Web Server instance from the <code class="literal">Servers</code> 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 |
| <a class="ulink" href="http://localhost:8080/greenpages" target="_top">http://localhost:8080/greenpages</a>. On |
| the home page type <code class="literal">wilkinson</code> into the search field and press <span class="emphasis"><em>Submit</em></span>. |
| Unlike the previous attempt, this should return a list (of size 1) of search results. From here, select |
| <span class="emphasis"><em>view</em></span> to get the “<span class="quote">detailed</span>” listing. |
| </p><div class="mediaobject" align="center"><img src="images/web-module/greenpages-listing-success.png" align="middle"></div><p> |
| This uses a stub implementation of the <code class="classname">Directory</code> interface which only knows about “<span class="quote">Andy Wilkinson</span>”. |
| </p><p> |
| The web interface is complete enough. Go to the next chapter to see the middle tier implementation. |
| </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="ch04s06.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch04.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch05.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">4.6 Referencing an OSGi Service </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 5. The Middle Tier</td></tr></table></div></body></html> |