<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>wwerner&#039;s blog</title>
	<atom:link href="http://blog.wolfgang-werner.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.wolfgang-werner.net</link>
	<description>tutorials, tips and trivia on eclipse, osgi, web &#38; xml technologies</description>
	<lastBuildDate>Tue, 10 Jan 2012 14:11:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Rumors of my demise have been greatly exaggerated</title>
		<link>http://blog.wolfgang-werner.net/rumors-of-my-demise-have-been-greatly-exaggerated/</link>
		<comments>http://blog.wolfgang-werner.net/rumors-of-my-demise-have-been-greatly-exaggerated/#comments</comments>
		<pubDate>Tue, 10 Jan 2012 14:11:44 +0000</pubDate>
		<dc:creator>Wolfgang Werner</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.wolfgang-werner.net/?p=651</guid>
		<description><![CDATA[While I did not post anything here during the last year (was a pretty short year, anyway :-/), I actually did do some writing: (German) A piece on the Equinox OSGi Console, basically the same as in an earlier post here. Published in eclipse Magazin 2.11. HTML, PDF (German) Devising an RESTful architecture based on [...]]]></description>
			<content:encoded><![CDATA[While I did not post anything here during the last year (was a pretty short year, anyway :-/), I actually did do some writing:<br />
<ul>
	<li>(German) A piece on the Equinox <span class="caps">OSG</span>i Console, basically the same as in an <a href="http://blog.wolfgang-werner.net/osgi-console-in-helios-monkey-see-monkey-do-and-a-strange-loop/">earlier post here</a>. Published in eclipse Magazin 2.11. <a href="http://it-republik.de/jaxenter/artikel/Von-alten-Konsolen-Affen-und-seltsamen-Schleifen-4093.html" target="_blank"><span class="caps">HTML</span></a>, <a href="http://www.bridging-it.de/fileadmin/img/news/06.2011_eclipse_Magazin_Strom_allein_ist_nicht_genug.pdf" target="_blank"><span class="caps">PDF</span></a></li>
	<li>(German) Devising an <span class="caps">REST</span>ful architecture based on <span class="caps">OSG</span>i &amp; Restlet using e-mobility charging station software as business case. Published in eclipse Magazin 6.11. <a href="http://it-republik.de/jaxenter/artikel/Zukunftssicher-unterwegs-mit-OSGi-und-REST-4150.html" target="_blank"><span class="caps">HTML</span></a>, <a href="http://www.bridging-it.de/fileadmin/img/news/06.2011_eclipse_Magazin_Strom_allein_ist_nicht_genug.pdf" target="_blank"><span class="caps">PDF</span></a></li>
	<li>A review on <a href="http://www.dzone.com/reviews/postgis-action" target="_blank">PostGIS in Action on dzone</a>.</li>
</ul>

<p>&lt;INSERT 2012 <span class="caps">WRITE</span>-MORE <span class="caps">RESOLUTION HERE</span>&gt;</p>]]></content:encoded>
			<wfw:commentRss>http://blog.wolfgang-werner.net/rumors-of-my-demise-have-been-greatly-exaggerated/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RESTful HTTP with JAX-RS</title>
		<link>http://blog.wolfgang-werner.net/restful-http-with-jax-rs/</link>
		<comments>http://blog.wolfgang-werner.net/restful-http-with-jax-rs/#comments</comments>
		<pubDate>Sat, 13 Nov 2010 22:32:22 +0000</pubDate>
		<dc:creator>Wolfgang Werner</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[jaxrs]]></category>
		<category><![CDATA[rest]]></category>

		<guid isPermaLink="false">http://blog.wolfgang-werner.net/?p=576</guid>
		<description><![CDATA[In this post, we'll look into creating a RESTful HTTP interface using JAX-RS. We'll see that it is pretty easy to expose ordinary java methods through HTTP.

We'll create a trivial model of a movie database and expose it via RESTful HTTP services as JSON and XML using JAX-RS, a little JAXB, Apache CXF, maven and Spring. We'll see that we need very little code to achieve that. This post focuses on the service front-end, the data is not persisted. We'll add JPA based persistence in a later post.]]></description>
			<content:encoded><![CDATA[<div class='toc wptoc'>
<h2>Contents</h2>
<ol class='toc-odd level-1'>
	<li>
		<a href="#Preparation">Preparation</a>
	</li>
	<li>
		<a href="#The_Model">The Model</a>
	</li>
	<li>
		<a href="#First_Service">First Service</a>
	</li>
	<li>
		<a href="#Wiring_it_up">Wiring it up</a>
	</li>
	<li>
		<a href="#Application">Application</a>
	</li>
	<li>
		<a href="#Take_it_for_a_test_drive">Take it for a test drive</a>
	</li>
	<li>
		<a href="#Some_data">Some data</a>
	</li>
	<li>
		<a href="#More_Services">More Services</a>
	</li>
	<li>
		<a href="#Reading_is_always_easy...">Reading is always easy...</a>
	</li>
	<li>
		<a href="#I_rest_my_case"><span style="font-weight: normal;">I rest my case</span></a>
	</li>
</ol>
</ol>
</div>
<br />
<span id="Preparation"><h2>Preparation</h2></span>
To follow along, you&#8217;ll need a <span class="caps">JDK</span> 1.6, &Acirc;&nbsp;Eclipse Helios <span class="caps">JEE </span>and Maven. M2eclipse won&#8217;t hurt. To get started, grab the <span class="caps">POM </span>file from <a href="https://github.com/wwerner/JAXRSTutorial/raw/master/pom.xml">https://github.com/wwerner/JAXRSTutorial/raw/master/pom.xml</a>.

<p>The complete source code is available on <a href="https://github.com/wwerner/JAXRSTutorial/" target="_blank">github</a>. There is one branch for each step so you can easily switch back and forth and compare your state with the expected one.</p>

<p>The <span class="caps">POM </span>defines a number of dependencies.&Acirc;&nbsp;The most notable ones are&Acirc;&nbsp;CXF <sup class='footnote'><a href='#fn-576-1' id='fnref-576-1' onclick='return fdfootnote_show(576)'>1</a></sup>,&Acirc;&nbsp;Jackson <sup class='footnote'><a href='#fn-576-2' id='fnref-576-2' onclick='return fdfootnote_show(576)'>2</a></sup> and&Acirc;&nbsp;Spring <sup class='footnote'><a href='#fn-576-3' id='fnref-576-3' onclick='return fdfootnote_show(576)'>3</a></sup>.</p>

<p>Run the following commands on the shell.</p>

<p>This will create the eclipse project we&#8217;ll be working on. I put in a little hack using the antrun plugin to create the source folders that would have been there if we started from an archetype. I&#8217;m pretty sure there is a better way to do this, please leave a pointer if you know.</p>

<p><span class="caps">CXF </span>comes with quite a load of dependencies of it&#8217;s own, so you might want to get a coffee<sup class='footnote'><a href='#fn-576-4' id='fnref-576-4' onclick='return fdfootnote_show(576)'>4</a></sup> while maven downloads the internets.</p>

<script src="http://gist.github.com/661747.js"></script>

<p>After mvn finished, you should have the same file structure as in this branch: <a href="https://github.com/wwerner/JAXRSTutorial/tree/stage2">https://github.com/wwerner/JAXRSTutorial/tree/stage2</a></p>

<p>Import the created project into your workspace using&Acirc;&nbsp;Import-&gt;Existing Projects. The project should look like this in the project explorer view:</p>

<img class="alignnone size-full wp-image-598" title="stage2_workspace" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/11/stage2_workspace.png" alt="" width="255" height="167" />
<span id="The_Model"><h2>The Model</h2></span>
For our movie database, we&#8217;ll only have two model classes: Actor and Movie. A movie consists of a title and the cast which maps the movie&#8217;s roles to Actors. The poor Actor can only afford one property: his name.

<p>We&#8217;ll put all of this in a new package in our project: net.wolfgangwerner.tutorial.jaxrs.model in src/main/java.</p>

<script src="http://gist.github.com/662446.js"></script>

<script src="http://gist.github.com/662451.js"></script>

See <a href="https://github.com/wwerner/JAXRSTutorial/tree/stage3" target="_blank">https://github.com/wwerner/JAXRSTutorial/tree/stage3</a>.<br />
<span id="First_Service"><h2>First Service</h2></span>
Now, what do we do to expose our model using <span class="caps">HTTP</span>? Let&#8217;s start with a trivial &#8216;Hello World&#8217; service to outline the concepts. &Acirc;&nbsp;We start with a new package called net.wolfgangwerner.tutorial.jaxrs.service and add a class called MovieService.

<script src="http://gist.github.com/662598.js"></script>

<p>Now, the only thing we have to do to expose this method is to add some <span class="caps">JAX</span>-RS annotations, @GET, which tells the runtime that this method should be called on <span class="caps">GET </span>requests and @Path which defines the <span class="caps">URI </span>(template) for which this method should be called. Additionally, we&#8217;ll add @Produces to make explicit which content type we are returning.</p>

<script src="http://gist.github.com/76a2d65ec11dd6feb29e.js"></script>

See&Acirc;&nbsp;<a href="https://github.com/wwerner/JAXRSTutorial/tree/stage4">https://github.com/wwerner/JAXRSTutorial/tree/stage4</a>.<br />
<span id="Wiring_it_up"><h2>Wiring it up</h2></span>
We could now configure the <span class="caps">JAX</span>-RS servlet<sup class='footnote'><a href='#fn-576-5' id='fnref-576-5' onclick='return fdfootnote_show(576)'>5</a></sup> and deploy it into a servlet container. In this case, we also need to make sure that we have a <span class="caps">JAX</span>-RS implementation in the classpath.

<p>Anyway, I&#8217;d like to do it a little different<sup class='footnote'><a href='#fn-576-6' id='fnref-576-6' onclick='return fdfootnote_show(576)'>6</a></sup>: We&#8217;ll use Spring IoC with the <span class="caps">JAX</span>-RS configuration namespace provided by <span class="caps">CXF </span>to define an <span class="caps">HTTP </span>server and publish our resource.</p>

<p>Create the file application-context.xml for Spring in src/main/resources and put in the following content.</p>

<script src="http://gist.github.com/674677.js"></script>

<p>Now what&#8217;s in there? First interesting thing is the declaration of the jaxrs namespace from <span class="caps">CXF</span>: xmlns:jaxrs=&#8221;http://cxf.apache.org/jaxrs. Then we define two beans, one for our service class and one for the jackson provider that will be used to render <span class="caps">JSON </span>representations.</p>

<p>The next part configures an <span class="caps">HTTP </span>server (CXF comes with jetty<sup class='footnote'><a href='#fn-576-7' id='fnref-576-7' onclick='return fdfootnote_show(576)'>7</a></sup>) to run on localhost port 9001. We tell the runtime to look for services in the service bean we defined earlier and we register the jackson <span class="caps">JSON </span>provider with this server.</p>

See<a href=" https://github.com/wwerner/JAXRSTutorial/tree/stage5" target="_blank"> https://github.com/wwerner/JAXRSTutorial/tree/stage5</a>.<br />
<span id="Application"><h2>Application</h2></span>
Next thing is to have an application we can start. For this tutorial, we do not run inside a web container but standalone. We&#8217;ll configure a server later on via spring, for now we just need something to start our application context.

<script src="http://gist.github.com/662585.js"></script>

<p>The application does nothing more than to look for a spring xml configuration file on the classpath and to start the configured context.</p>

See <a href="https://github.com/wwerner/JAXRSTutorial/tree/stage6" target="_blank">https://github.com/wwerner/JAXRSTutorial/tree/stage6</a>.<br />
<span id="Take_it_for_a_test_drive"><h2>Take it for a test drive</h2></span>
Your workspace should now look like this:

<p><img class="alignnone size-full wp-image-602" title="stage6_workspace" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/11/stage6_workspace.png" alt="" width="316" height="311" /></p>

<p>Select the Application class and choose Run as.. -&gt; Java Application<sup class='footnote'><a href='#fn-576-8' id='fnref-576-8' onclick='return fdfootnote_show(576)'>8</a></sup></p>

<p>The console output should tell you that spring wired the dependencies, jetty started and is listensing on port 9001 as you told it to in the application context.</p>

<p><img class="alignnone size-full wp-image-604" title="stage6_start" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/11/stage6_start.png" alt="" width="742" height="231" /></p>

<p>If &Acirc;&nbsp;you followed my earlier tutorials, you&#8217;ll know Poster<sup class='footnote'><a href='#fn-576-9' id='fnref-576-9' onclick='return fdfootnote_show(576)'>9</a></sup>, a firefox addon to make <span class="caps">HTTP </span>requests. If you don&#8217;t have it, get it or &Acirc;&nbsp;use the <span class="caps">HTTP </span>speaking tool of your choice instead <sup class='footnote'><a href='#fn-576-10' id='fnref-576-10' onclick='return fdfootnote_show(576)'>10</a></sup>.</p>

<p>Use Poster to <span class="caps">GET </span>http://localhost:9001/hello . Remember we defined the server port in application-context.xml and path via the @Path annotation in MovieService.</p>

<p><img class="alignnone size-full wp-image-606" title="stage6_poster" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/11/stage6_poster.png" alt="" width="565" height="362" /></p>

<p>Let&#8217;s try another thing: We can ask our service which kinds of interaction it supports using the <span class="caps">OPTIONS HTTP </span>verb<sup class='footnote'><a href='#fn-576-11' id='fnref-576-11' onclick='return fdfootnote_show(576)'>11</a></sup>.</p>

<p>This tells us that we can execute <span class="caps">GET, HEAD </span>and <span class="caps">OPTIONS </span>requests on our /hello <span class="caps">URL</span>:</p>

<p><img class="alignnone size-full wp-image-607" title="stage6_options" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/11/stage6_options.png" alt="" width="551" height="533" /></p>

<p>This is what it would look like with cURL:</p>

<p>R<script src="http://gist.github.com/675316.js"></script></p>

That was pretty easy. Boring, too. Let&#8217;s see how we can map actual object hierarchies to different formats and write data back to our application.<br />
<span id="Some_data"><h2>Some data</h2></span>
Before we add some more interesting services, let&#8217;s create some test data we can retrieve first. We&#8217;ll simply do so in the constructor of our service. In a later post, we&#8217;ll factor out a data store component that uses <span class="caps">JPA </span>for persistance. Stay tuned.]

<p>First we create a couple of actors, then we associate them with the movie using the cast map.</p>

<script src="http://gist.github.com/675617.js"></script>

<a href="https://github.com/wwerner/JAXRSTutorial/tree/stage7" target="_blank">https://github.com/wwerner/JAXRSTutorial/tree/stage7</a><br />
<span id="More_Services"><h2>More Services</h2></span>
Lets create a service that returns a movie by it&#8217;s id and can provide the result in <span class="caps">JSON </span>and <span class="caps">XML </span>format. The good message is that we don&#8217;t have to worry about marshalling and unmarshalling of our business objects. We can simply return Movie from our service method. Let&#8217;s give it a try and see what happens. Add the following method to our service class and call it using Poster/cURL/whatever.

<script src="http://gist.github.com/675623.js"></script>

<p>Note that we added an <span class="caps">URL </span>parameter {id} to the @Path annotation. We also added the @PathParam annotation to the id parameter in our method<sup class='footnote'><a href='#fn-576-12' id='fnref-576-12' onclick='return fdfootnote_show(576)'>12</a></sup>.</p>

<p>That&#8217;s the result<sup class='footnote'><a href='#fn-576-13' id='fnref-576-13' onclick='return fdfootnote_show(576)'>13</a></sup>: <script src="http://gist.github.com/675627.js"></script></p>

<p>We got <span class="caps">JSON, </span>as this is the first type defined in our @Produces annotation and the client did not send any preferences. Let&#8217;s add a request header that says &#8216;I&#8217;d rather take <span class="caps">XML&#8217;</span><sup class='footnote'><a href='#fn-576-14' id='fnref-576-14' onclick='return fdfootnote_show(576)'>14</a></sup>.</p>

<p><img class="alignnone size-full wp-image-610" title="stage7_xmlrequest" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/11/stage7_xmlrequest.png" alt="" width="564" height="379" /></p>

<p>This returns &#8230; &#8230; &#8230; an error? Correct. A kind of cryptic one as well:</p>


<pre>No message body writer has been found for response class Movie.</pre>


<p>This means that the jax-rs runtime did not find anything to serialize our Movie. Jackson, the <span class="caps">JSON </span>processor is kinda smart, it just uses reflection to render <span class="caps">JSON.</span> But for <span class="caps">XML, </span>we need to Do Stuff&#8482;. How do we get that body writer? We don&#8217;t need to. We can just use <span class="caps">JAXB </span>annotations in our model to link our data to an <span class="caps">XML </span>representation. The first thing to do is to add the @XmlRootElement annotation on both our model classes.</p>

<script src="http://gist.github.com/675638.js"></script>

<p>If you run this, you might note that the cast may be displayed before the title. That&#8217;s fugly. Add@XmlType(propOrder={&#8220;title&#8221;, &#8220;cast&#8221;}) &Acirc;&nbsp;to the movie class, restart the application and you are good. The nice thing about the <span class="caps">JAXB </span>bindings is that they are also evaluated by jackson. Add the annotation @XmlElement(name=&#8221;who&#8221;) to the getName() method of Actor and see what happens.</p>

<script src="http://gist.github.com/675651.js"></script>

<script src="http://gist.github.com/675657.js"></script>

<p>Note that the &#8220;name&#8221; element changed in both representations.</p>

Now you might argue that the default representations are still fugly. <span class="caps">JAXB </span>provides a nice mechanism to produce arbitrary output: @XmlAdapter. The approach is basically that you create a java class that produces the required <span class="caps">XML </span>output and an adapter that converts your business objects to the representation objects and vice versa. This seems overkill at first, but it is incredibly powerful. <a href="http://twitter.com/bdoughan" target="_blank">@bdoughan</a> wrote a nice <a href="http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html" target="_blank">post</a> on this if you want to dig deeper.<br />
<span id="Reading_is_always_easy8230"><h2>Reading is always easy&#8230;</h2></span>
&#8230; writing back is more interesting. Well, not in this case. It&#8217;s exactly the same. Just swap the @GET for a @POST and @Produces for @Consumes, pass the Movie as parameter, add some logic to save the value and you are done. Ok, we&#8217;re not really persisting stuff, so just storing in memory may not count. We&#8217;ll address persistence via <span class="caps">JPA </span>in a later post.

<p>To see which data we have stored on the server, we&#8217;ll also add a new service that returns all movies. Note that (typed) collections are handled transparently as well.</p>

<p>To make it a little more interesting, we return a useful response that tells us which resource has been created<sup class='footnote'><a href='#fn-576-15' id='fnref-576-15' onclick='return fdfootnote_show(576)'>15</a></sup>.</p>

<script src="http://gist.github.com/675677.js"></script>

<p>Just copy either the <span class="caps">JSON </span>or <span class="caps">XML </span>representation and <span class="caps">POST </span>it to the /movies <span class="caps">URL </span>using a new id. This will create a new movie and return it&#8217;s <span class="caps">URL.</span> You can also <span class="caps">GET </span>/movies to see a complete list of movies available.</p>

<p><img class="alignnone size-full wp-image-626" title="stage8_post" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/11/stage8_post.png" alt="" width="565" height="453" /></p>

<p><img class="alignnone size-full wp-image-625" title="stage8_post2" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/11/stage8_post2.png" alt="" width="510" height="525" /></p>

<script src="http://gist.github.com/675727.js"></script>

<a href="https://github.com/wwerner/JAXRSTutorial/tree/stage8" target="_blank">https://github.com/wwerner/JAXRSTutorial/tree/stage8</a><br />
<span id="I_rest_my_case"><h2><span style="font-weight: normal;">I rest my case</span></h2></span>
<span style="font-weight: normal;">We saw that it is pretty easy to expose simple java methods through <span class="caps">HTTP.</span></span>

<p>Two weeks before writing this post I gave a talk at the company I work with<sup class='footnote'><a href='#fn-576-16' id='fnref-576-16' onclick='return fdfootnote_show(576)'>16</a></sup> about <span class="caps">REST </span>and common <span class="caps">REST </span>misconceptions that also featured a live hacking session with the stuff you just read. Then I thought that I could reuse it for a blog. Then I went to Eclipse Summit Europe<sup class='footnote'><a href='#fn-576-17' id='fnref-576-17' onclick='return fdfootnote_show(576)'>17</a></sup> where <a href="http://twitter.com/doug_clarke" target="_blank">@doug_clarke</a> used <span class="caps">JAX</span>-RS in his <a href="http://www.eclipsecon.org/summiteurope2010/sessions/?page=sessions&amp;id=1692" target="_blank">talk</a>, then <a href="http://twitter.com/vogella" target="_blank">@vogella</a> published a <a href="http://www.vogella.de/articles/REST/article.html">tutorial </a>on <span class="caps">JAX</span>-RS as well<sup class='footnote'><a href='#fn-576-18' id='fnref-576-18' onclick='return fdfootnote_show(576)'>18</a></sup>.</p>

<p>I like to believe that it is an interesting topic more than I like to believe it is grossly redundant. Feel free to disagree, here&#8217;s my take on it anyway. As a side note, it&#8217;s&Acirc;&nbsp;actually&Acirc;&nbsp;only 89 lines of java code. That is pretty lean, I&#8217;d say. You can get each and every of these lines&Acirc;&nbsp;<a href="https://github.com/wwerner/JAXRSTutorial">here</a>. Awesome, isn&#8217;t it?</p>

<p>Also note that as we speak, a new version of the <a href="https://jsr311.dev.java.net/"><span class="caps">JAX</span>-RS/JSR311</a> specification is proposed <a href="http://markmail.org/message/wgm3hj3rrva3j6jo?q=list:net.java.dev.jsr311.dev">here</a>.</p>

<p>I&#8217;ll follow up on this post with adding <span class="caps">JPA</span>&Acirc;&nbsp;persistence&Acirc;&nbsp;to our movie store. Stay tuned if you are interested.</p>

<p>As bedtime candy, here are some of the slides<sup class='footnote'><a href='#fn-576-19' id='fnref-576-19' onclick='return fdfootnote_show(576)'>19</a></sup> I used in my talk.</p>


<a href='http://blog.wolfgang-werner.net/restful-http-with-jax-rs/1990-2/' title='1990'><img width="150" height="150" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/11/19901-150x150.jpg" class="attachment-thumbnail" alt="1990" title="1990" /></a>
<a href='http://blog.wolfgang-werner.net/restful-http-with-jax-rs/myth-2/' title='myth'><img width="150" height="150" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/11/myth1-150x150.jpg" class="attachment-thumbnail" alt="myth" title="myth" /></a>
<a href='http://blog.wolfgang-werner.net/restful-http-with-jax-rs/restafari-2/' title='RESTafari'><img width="150" height="150" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/11/RESTafari1-150x150.jpg" class="attachment-thumbnail" alt="RESTafari" title="RESTafari" /></a>
<a href='http://blog.wolfgang-werner.net/restful-http-with-jax-rs/soap-2/' title='soap'><img width="150" height="150" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/11/soap1-150x150.jpg" class="attachment-thumbnail" alt="soap" title="soap" /></a>


<p>Btw., we are looking for java developers. Please feel free to contact me if you are interested.</p>

<div class='footnotes' id='footnotes-576'><div class='footnotedivider'></div><ol><li id='fn-576-1'>&Acirc;&nbsp;<a href="http://cxf.apache.org/">http://cxf.apache.org/</a> <span class='footnotereverse'><a href='#fnref-576-1'>&#8617;</a></span></li><li id='fn-576-2'>&Acirc;&nbsp;<a href="http://jackson.codehaus.org/">http://jackson.codehaus.org/</a> <span class='footnotereverse'><a href='#fnref-576-2'>&#8617;</a></span></li><li id='fn-576-3'>&Acirc;&nbsp;<a href="http://www.springsource.org/">http://www.springsource.org/</a> <span class='footnotereverse'><a href='#fnref-576-3'>&#8617;</a></span></li><li id='fn-576-4'>Or beer, depending on the time of the day <span class='footnotereverse'><a href='#fnref-576-4'>&#8617;</a></span></li><li id='fn-576-5'>&Acirc;&nbsp;<a href="http://stackoverflow.com/questions/2072295/how-to-deploy-a-jax-rs-application">http://stackoverflow.com/questions/2072295/how-to-deploy-a-jax-rs-application</a> <span class='footnotereverse'><a href='#fnref-576-5'>&#8617;</a></span></li><li id='fn-576-6'>I like different approaches. And when I play, I try to be as independent of a certain stack as possible. I want to learn which part does what instead of relying on application server magic. <span class='footnotereverse'><a href='#fnref-576-6'>&#8617;</a></span></li><li id='fn-576-7'>Of course. Nearly everything comes with jetty. <span class='footnotereverse'><a href='#fnref-576-7'>&#8617;</a></span></li><li id='fn-576-8'>Alt-Shift-X, J if you are lazy, which you should be: <a href="http://threevirtues.com/">http://threevirtues.com/</a> <span class='footnotereverse'><a href='#fnref-576-8'>&#8617;</a></span></li><li id='fn-576-9'><a href="https://addons.mozilla.org/de/firefox/addon/2691/">https://addons.mozilla.org/de/firefox/addon/2691/</a> <span class='footnotereverse'><a href='#fnref-576-9'>&#8617;</a></span></li><li id='fn-576-10'>In particular, I like cURL. curl -v -X &lt;HTTP Verb&gt; &lt;URL&gt; is your friend <span class='footnotereverse'><a href='#fnref-576-10'>&#8617;</a></span></li><li id='fn-576-11'>&Acirc;&nbsp;<a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html">http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html</a> <span class='footnotereverse'><a href='#fnref-576-11'>&#8617;</a></span></li><li id='fn-576-12'>Which I find&Acirc;&nbsp;particularly&Acirc;&nbsp;ugly. But it needs to be done. <span class='footnotereverse'><a href='#fnref-576-12'>&#8617;</a></span></li><li id='fn-576-13'>After reformatting <span class='footnotereverse'><a href='#fnref-576-13'>&#8617;</a></span></li><li id='fn-576-14'>For details see e.g.&Acirc;&nbsp;<a href="http://en.wikipedia.org/wiki/Content_negotiation">http://en.wikipedia.org/wiki/Content_negotiation</a> <span class='footnotereverse'><a href='#fnref-576-14'>&#8617;</a></span></li><li id='fn-576-15'>For details see <a href="https://cwiki.apache.org/WINK/jax-rs-request-and-response-entities.html">https://cwiki.apache.org/WINK/jax-rs-request-and-response-entities.html</a>, e.g. <span class='footnotereverse'><a href='#fnref-576-15'>&#8617;</a></span></li><li id='fn-576-16'><a href="http://bridging-it.de/">http://bridging-it.de/</a> <span class='footnotereverse'><a href='#fnref-576-16'>&#8617;</a></span></li><li id='fn-576-17'><a href="http://www.eclipsecon.org/summiteurope2010/">http://www.eclipsecon.org/summiteurope2010/</a> <span class='footnotereverse'><a href='#fnref-576-17'>&#8617;</a></span></li><li id='fn-576-18'>This guy is fast: It&#8217;s the third time he published something I was working on that point in time. <span class='footnotereverse'><a href='#fnref-576-18'>&#8617;</a></span></li><li id='fn-576-19'>Image credit:<a href=" http://rosewine.deviantart.com/#/d2sw85k"> http://rosewine.deviantart.com/#/d2sw85k</a>, <a href="http://www.etsy.com/listing/33909593/fight-club-vegan-soap-unofficial-parody">http://www.etsy.com/listing/33909593/fight-club-vegan-soap-unofficial-parody</a>, <a href="http://www.flickr.com/photos/onesec/4464260372/">http://www.flickr.com/photos/onesec/4464260372/</a>, <a href="http://www.conti-online.com/generator/www/de/de/contisoccerworld/themes/02_fanzone/04_stars/20_world_cup_stars/1990-matthaeus-de.html">http://www.conti-online.com/generator/www/de/de/contisoccerworld/themes/02_fanzone/04_stars/20_world_cup_stars/1990-matthaeus-de.html</a> <span class='footnotereverse'><a href='#fnref-576-19'>&#8617;</a></span></li></ol></div>]]></content:encoded>
			<wfw:commentRss>http://blog.wolfgang-werner.net/restful-http-with-jax-rs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>OSGi Console in Helios, Monkey See/Monkey Do and a strange loop</title>
		<link>http://blog.wolfgang-werner.net/osgi-console-in-helios-monkey-see-monkey-do-and-a-strange-loop/</link>
		<comments>http://blog.wolfgang-werner.net/osgi-console-in-helios-monkey-see-monkey-do-and-a-strange-loop/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 22:52:18 +0000</pubDate>
		<dc:creator>Wolfgang Werner</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[equinox]]></category>
		<category><![CDATA[helios]]></category>
		<category><![CDATA[management console]]></category>
		<category><![CDATA[osgi]]></category>
		<category><![CDATA[pde]]></category>

		<guid isPermaLink="false">http://blog.wolfgang-werner.net/?p=489</guid>
		<description><![CDATA[Helios comes with an OSGi console that is accessible from the GUI directly. This reminded me of the existence of an API to add custom commands to the console. I found this to be a simple and powerful concept; yet I perceive this feature to be rarely used in practice.
With equinox being used on the server more and more, adding your own custom application management commands is a simple way to provide for executing application specific maintenance tasks.  How about a command that gathers all logs, creates a heap dump, zips everything, puts it onto an FTP server and sends you the download link by mail? Or perhaps you simply want to run some sanity checks? 

In this post, we'll create a custom command for the OSGi console that simply executes a JUnit test and prints the results. 


More importantly, we'll have to snoop around in internal eclipse packages to figure out how to attach the command to the existing console. We'll try to do our snooping effectively. And we'll exploit the dynamic nature of OSGi to streamline our development/build/test cycle (and thats gonna be fun). ]]></description>
			<content:encoded><![CDATA[<div class='toc wptoc'>
<h2>Contents</h2>
<ol class='toc-odd level-1'>
	<li>
		<a href="#Preparation">Preparation</a>
	</li>
	<li>
		<a href="#Test_First">Test First</a>
	</li>
	<li>
		<a href="#Monkey_See_...">Monkey See, ...</a>
	</li>
	<li>
		<a href="#Monkey_Do">...Â Monkey Do</a>
	</li>
	<li>
		<a href="#Strange_Loops">Strange Loops</a>
	</li>
	<li>
		<a href="#Conclusion">Conclusion</a>
	</li>
</ol>
</ol>
</div>
<br />
<span id="Preparation"><h2>Preparation</h2></span>
First, you need to grab the Helios Classic Editon build<sup class='footnote'><a href='#fn-489-1' id='fnref-489-1' onclick='return fdfootnote_show(489)'>1</a></sup> as this version includes the sources as well. We&#8217;ll use the <span class="caps">OSG</span>i console for the currently running eclipse instance that is available via the UI since 3.6M7<sup class='footnote'><a href='#fn-489-2' id='fnref-489-2' onclick='return fdfootnote_show(489)'>2</a></sup>

<p>Now start eclipse, create a new workspace and go to the workbench.</p>

<p>Open the console view (Alt-Shift-Q,Q) and select &#8220;OSGi console&#8221; from the view menu. &Acirc;&nbsp;You now have an <span class="caps">OSG</span>i console that allows you to interact with the running eclipse<sup class='footnote'><a href='#fn-489-3' id='fnref-489-3' onclick='return fdfootnote_show(489)'>3</a></sup>.</p>


<a href='http://blog.wolfgang-werner.net/osgi-console-in-helios-monkey-see-monkey-do-and-a-strange-loop/osgi-console-2/' title='osgi-console-1'><img width="150" height="134" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/05/osgi-console-2-150x134.png" class="attachment-thumbnail" alt="osgi-console-1" title="osgi-console-1" /></a>
<a href='http://blog.wolfgang-werner.net/osgi-console-in-helios-monkey-see-monkey-do-and-a-strange-loop/osgi-console-1/' title='osgi-console-2'><img width="150" height="150" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/05/osgi-console-1-150x150.png" class="attachment-thumbnail" alt="osgi-console-2" title="osgi-console-2" /></a>
<a href='http://blog.wolfgang-werner.net/osgi-console-in-helios-monkey-see-monkey-do-and-a-strange-loop/osgi-console-3/' title='osgi-console-3'><img width="150" height="150" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/05/osgi-console-3-150x150.png" class="attachment-thumbnail" alt="osgi-console-3" title="osgi-console-3" /></a>


<p>Type &#8220;help&#8221; to get a list of available commands. You should do that even if you are a eclipse veteran, the list of available commands has grown considerably during the last years. There are commands to start/stop/install/refresh bundles, to view registered services, to configure runtime properties, to query the extension registry, to start and stop equinox applications and enable and disable components.</p>

Let&#8217;s assume we want to be able to run integrity tests on a live system to check whether incoming issue reports could be due to some irregularities with system load, data or deployment issues. Lets also assume that application has a set of JUnit tests to do so. We want to be able to log onto the console and see what the results of our test suite are.<br />
<span id="Test_First"><h2>Test First</h2></span>
No, not <em>that </em>test first. To add a command to run our tests we need tests first, see? We simply create a new <span class="caps">OSG</span>i bundle targeting the equinox runtime using the &#8220;New project&#8221; wizard and add some tests to it. Make sure to have at least on failing test as we&#8217;ll print only failures from our command.



<script src="http://gist.github.com/419277.js"></script>

<p>We also need some piece of code to let us run our tests. Luckily, JUnit provides a simple <span class="caps">API </span>for that<sup class='footnote'><a href='#fn-489-4' id='fnref-489-4' onclick='return fdfootnote_show(489)'>4</a></sup>.</p>

<script src="http://gist.github.com/419280.js"></script><br />
<span id="Monkey_See_8230"><h2>Monkey See, &#8230;</h2></span>
The basic steps are to implement the org.eclipse.osgi.framework.console.CommandProvider interface and to register it with the <span class="caps">OSG</span>i service registry. But this is covered in at least two&Acirc;&nbsp;tutorials by Chris Aniszczyk<sup class='footnote'><a href='#fn-489-5' id='fnref-489-5' onclick='return fdfootnote_show(489)'>5</a></sup> and his evil, quake playing twin<sup class='footnote'><a href='#fn-489-6' id='fnref-489-6' onclick='return fdfootnote_show(489)'>6</a></sup>. You should definitely read them. Less chitchat than this post.

<p>Instead of repeating this<sup class='footnote'><a href='#fn-489-7' id='fnref-489-7' onclick='return fdfootnote_show(489)'>7</a></sup>, I&#8217;d rather show you how to figure it out for yourself. Reading code is an excellent way to learn<sup class='footnote'><a href='#fn-489-8' id='fnref-489-8' onclick='return fdfootnote_show(489)'>8</a></sup>.</p>

<p>First, we need an entry point, a piece of code we know that has something to do with the console. So what do we know? Well, there is a console view, let&#8217;s go hunting for it&#8217;s implementation. To make sure that our searches include the code from the platform plug-ins, we will add them to the Java search. &Acirc;&nbsp;Helios has a handy new button in the Plug-Ins view for that. <sup class='footnote'><a href='#fn-489-9' id='fnref-489-9' onclick='return fdfootnote_show(489)'>9</a></sup></p>

<div class="wpgalleryimage_shortcode" style="width: 384px; height: 131px; float: none;"><img width="382" height="129" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/06/0-add-to-java-search.png" class="attachment-full" alt="0-add-to-java-search" title="0-add-to-java-search" /></div>

<p>Fire up the search dialog <sup class='footnote'><a href='#fn-489-10' id='fnref-489-10' onclick='return fdfootnote_show(489)'>10</a></sup>, select the Plug-in Search tab, enter &#8220;org.eclipse.ui.views&#8221; as search string and select &#8220;Search for-&gt; Extension Point&#8221;, &#8220;Limit to -&gt; References&#8221; and &#8220;External Scope -&gt; All&#8221;.</p>

<div class="wpgalleryimage_shortcode" style="width: 614px; height: 450px; float: none;"><img width="612" height="448" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/06/1-search-view.png" class="attachment-full" alt="1-search-view" title="1-search-view" /></div>

<p>This search gives us 28 matches. One match is the one we are looking for: org.eclipse.ui.console.</p>

<div class="wpgalleryimage_shortcode" style="width: 412px; height: 133px; float: none;"><img width="410" height="131" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/06/2-search-view-result.png" class="attachment-full" alt="2-search-view-result" title="2-search-view-result" /></div>

<p>Let&#8217;s open the declaration and switch over to the extensions tab. We can see the contribution the console view we were looking for, but also something that looks even more promising: An extension point called &#8220;org.eclipse.ui.console.consoleFactories&#8221;. Let&#8217;s see who contributes to this extension point. Choose &#8220;Find References&#8221; from its context menu.</p>

<div class="wpgalleryimage_shortcode" style="width: 416px; height: 326px; float: none;"><img width="414" height="324" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/06/3-search-console-factory-references.png" class="attachment-full" alt="3-search-console-factory-references" title="3-search-console-factory-references" /></div>

<div class="wpgalleryimage_shortcode" style="width: 374px; height: 119px; float: none;"><img width="372" height="117" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/06/4-search-console-factory-references-result.png" class="attachment-full" alt="4-search-console-factory-references-result" title="4-search-console-factory-references-result" /></div>

<p>Only four matches. I think we can rule out the one in team.cvs and debug.ui. &Acirc;&nbsp;We&#8217;ll find what we are looking for in the org.eclipse.pde.ui bundle<sup class='footnote'><a href='#fn-489-11' id='fnref-489-11' onclick='return fdfootnote_show(489)'>11</a></sup>.</p>

<div class="wpgalleryimage_shortcode" style="width: 336px; height: 242px; float: none;"><img width="334" height="240" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/06/5-found-osgi-console-contribution.png" class="attachment-full" alt="5-found-osgi-console-contribution" title="5-found-osgi-console-contribution" /></div>

<p>The contribution contains the implementation class, so we open this class next. Copy the qualified name of the class, press Ctrl-Shift-T and paste the class name.</p>

<div class="wpgalleryimage_shortcode" style="width: 350px; height: 138px; float: none;"><img width="348" height="136" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/06/6-open-type.png" class="attachment-full" alt="6-open-type" title="6-open-type" /></div>

<p>In the console factory, we find that a new console is instantiated in the getConsole() method. Let&#8217;s open the implementation by Ctrl-Click on the class name.</p>

<div class="wpgalleryimage_shortcode" style="width: 393px; height: 108px; float: none;"><img width="391" height="106" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/06/11-open-OSGiConsole.png" class="attachment-full" alt="11-open-OSGiConsole" title="11-open-OSGiConsole" /></div>

<p>A first glance at this class does not&Acirc;&nbsp;immediately reveal where the console commands come from. We need to dig a little deeper aka start guessing and poking around wildly. To be able to run a command, the console input needs to be evaluated, thats for sure. So lets see if we can find where the input is read. In the <span class="caps">OSG</span>i console constructure, a new anonymous ConsoleSession is created. The ConsoleSession has a method called getInput() that returns an input stream. Let&#8217;s see who calls this method: place the cursor on the method name and press Ctrl-Alt-H to open the call hierarchy.</p>

<script src="http://gist.github.com/457091.js"></script>

<div class="wpgalleryimage_shortcode" style="width: 601px; height: 149px; float: none;"><img width="599" height="147" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/06/12-call-hierarchy.png" class="attachment-full" alt="12-call-hierarchy" title="12-call-hierarchy" /></div>

<p>The first&amp;direct caller, FrameworkConsole, seems not overly interesting in terms of command definitions. Let&#8217;s see what the next one, ConsoleManger looks like. A little hidden, somewhere in the middle we find a field of the type FrameworkCommandProvider. Now we&#8217;re pretty close to what we want to know. This is the place were &#8211; at least some &#8211; console commands are defined. The constructor&#8217;s comment pretty much tells us what to do:</p>

<script src="http://gist.github.com/457103.js"></script>

<p>So we need to implement a CommandProvider and register it with the service registry. For each command we want to implement, we need to have a method called &#8220;_&lt;command name&gt;&#8221;. We&#8217;ll implement only one command that calls our test runner. Additionally, I think we should implement the the getHelp() method to describe our command to the user.</p>

<p>It may be useful for your analysis to make changes to eclipse plug-ins to see how they behave. There is a simple recipe how you can do this without the risk of doing much harm: Import the respective plug-in from the target platform to the workspace, then create a launch configuration containing the workspace instead of the target platform bundle. You can now edit the standard eclipse bundle to gain a deeper understanding. When you are done, simply delete the bundle from the workspace (and from the disk as well).</p>

<div class="wpgalleryimage_shortcode" style="width: 484px; height: 162px; float: none;"><img width="482" height="160" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/06/17-import-project-from-target.png" class="attachment-full" alt="17-import-project-from-target" title="17-import-project-from-target" /></div><br />
<span id="AcircnbspMonkey_Do"><h2>&#8230;&Acirc;&nbsp;Monkey Do</h2></span>
Use the bundle project we have created in the first chapter. Add a new dependency to &#8220;org.eclipse.osgi.framework.console&#8221;&iuml;&raquo;&iquest;, the package containing the CommandProvider interface.

<div class="wpgalleryimage_shortcode" style="width: 496px; height: 213px; float: none;"><img width="494" height="211" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/06/13-additional-dependency.png" class="attachment-full" alt="13-additional-dependency" title="13-additional-dependency" /></div>

<p>Create a new class using the new class wizard, have it implement CommandProvider and generate the inherited methods.</p>

<div class="wpgalleryimage_shortcode" style="width: 499px; height: 600px; float: none;"><img width="497" height="598" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/06/14-new-class-wizard.png" class="attachment-full" alt="14-new-class-wizard" title="14-new-class-wizard" /></div>

<p>Implement the getHelp() method so the output looks similar to the other commands. Have a look at the FrameworkCommandProvider for reference. Note that you can use Alt-Left/Alt-Right to navigate in the edit/browse location history. Useful as you can easily switch back and forth between your class and the reference class without using your mouse.</p>

<p>We call our command &#8220;check&#8221;, so we also need to implement &#8220;<em>check(CommandInterpreter intp)&#8221;. The command interpreter is used to output the results from our tests. Take a look at the &#8220;</em>*&#8221; methods in FrameworkCommandProvider for examples.</p>

<p>The complete class looks like this<sup class='footnote'><a href='#fn-489-12' id='fnref-489-12' onclick='return fdfootnote_show(489)'>12</a></sup>:</p>

<script src="http://gist.github.com/457157.js"></script>

<p>Now we need to register/de-register our command provider in the Activators start() resp. stop methods:</p>

<script src="http://gist.github.com/457181.js"></script>

Having implemented all this, it&#8217;s time to look at our options for giving it a test drive<br />
<span id="Strange_Loops"><h2>Strange Loops</h2></span>
We can either<br />
<ul>
	<li>start our bundle as eclipse application, open the console view and test our command</li>
	<li>create a minimal run configuration and start the framework with the console and test there</li>
	<li>build the bundle and install it into the running framework (the running eclipse instance)</li>
	<li>or even install the bundle directly from it&#8217;s files in the workspace</li>
</ul>
While option two is probably the most reasonable approach (for separation of concerns and debugging issues), option four is certainly the most fun<sup class='footnote'><a href='#fn-489-13' id='fnref-489-13' onclick='return fdfootnote_show(489)'>13</a></sup>. Since I wrote about run configs in an earlier post<sup class='footnote'><a href='#fn-489-14' id='fnref-489-14' onclick='return fdfootnote_show(489)'>14</a></sup> guess what we&#8217;ll do now. The only thing we need to do to make our bundle installable directly is to add the bin/ folder of our project to the runtime classpath.

<div class="wpgalleryimage_shortcode" style="width: 603px; height: 231px; float: none;"><img width="601" height="229" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/06/15-runtime-classpath.png" class="attachment-full" alt="15-runtime-classpath" title="15-runtime-classpath" /></div>

<p>Then we can use the console to install the bundle from the workspace. First, check that our command is not yet available by calling our &Acirc;&nbsp;&#8221;check&#8221; command. Our command is not found, so the help contents are displayed. Note that our command&#8217;s help text is missing as well. Then we can install the bundle if we know the project&#8217;s path on the harddrive.</p>

<div class="wpgalleryimage_shortcode" style="width: 706px; height: 231px; float: none;"><img width="704" height="229" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/06/16-project-location.png" class="attachment-full" alt="16-project-location" title="16-project-location" /></div>

<p>-</p>

<script src="http://gist.github.com/457911.js"></script>

<p>We have now &Acirc;&nbsp;installed a bundle that we are currently developing into our running eclipse instance. Isn&#8217;t that awesome? Of course you need to be careful with this approach as you can pretty much screw things up when fiddling with the hot bundle configuration of your <span class="caps">IDE, </span>but I think this approach actually makes sense when you are integrating with the <span class="caps">IDE </span>rather than building a standalone application.</p>

Besides, I just love self-referential stuff.<br />
<blockquote>What I mean by &#8220;strange loop&#8221; is &acirc;€” here goes a first stab, anyway &acirc;€” not a physical circuit but an abstract loop in which, in the series of stages that constitute the cycling-around, there is a shift from one level of abstraction (or structure) to another, which feels like an upwards movement in a hierarchy, and yet somehow the successive &#8220;upward&#8221; shifts turn out to give rise to a closed cycle. That is, despite one&#8217;s sense of departing ever further from one&#8217;s origin, one winds up, to one&#8217;s shock, exactly where one had started out. In short, a strange loop is a paradoxical level-crossing feedback loop. &#8211; Douglas R. Hofstadter</blockquote>
See also&Acirc;&nbsp;<a href="http://www.xkcd.com/555/">http://www.xkcd.com/555/</a>, don&#8217;t forget to read the tooltip.<br />
<span id="Conclusion"><h2>Conclusion</h2></span>
Besides highlighting the Host <span class="caps">OSG</span>i console included in Helios and trying to revive the memory of custom <span class="caps">OSG</span>i console commands, this article showed you how you can find out how stuff is done in eclipse by following the monkey see/monkey do rule and gave some tips how to apply this rule in practice.

<p>I&#8217;d be happy to see more custom <span class="caps">OSG</span>i console commands in the wild. As I said in the teaser, I still think this concept is underused. Also remember that the <span class="caps">OSG</span>i console can be configured to be accessible remotely using telnet. Now don&#8217;t tell me you can&#8217;t think of use cases for that.</p>

<p>I&#8217;ll push the complete sources of the example to github during the next days. In the meantime: Let me know if you get stuck.</p>

<p>I hope you enjoyed reading and that I gave you one or the other idea that may help you in future projects. Thanks for reading.</p>

<div class='footnotes' id='footnotes-489'><div class='footnotedivider'></div><ol><li id='fn-489-1'><a href="http://www.eclipse.org/downloads/packages/eclipse-classic-360/heliosr">http://www.eclipse.org/downloads/packages/eclipse-classic-360/heliosr</a> <span class='footnotereverse'><a href='#fnref-489-1'>&#8617;</a></span></li><li id='fn-489-2'><a href="http://download.eclipse.org/eclipse/downloads/drops/S-3.6M7-201004291549/eclipse-news-M7.html#osgiconsole">http://download.eclipse.org/eclipse/downloads/drops/S-3.6M7-201004291549/eclipse-news-M7.html#osgiconsole</a> <span class='footnotereverse'><a href='#fnref-489-2'>&#8617;</a></span></li><li id='fn-489-3'>The screenshots were taken on 3.6M7, it looks a little different in the final release. The draft of this post has seen some idle cycles&#8230; <span class='footnotereverse'><a href='#fnref-489-3'>&#8617;</a></span></li><li id='fn-489-4'><a href="http://www.junit.org/junit/javadoc/4.3/org/junit/runner/JUnitCore.html">http://www.junit.org/junit/javadoc/4.3/org/junit/runner/JUnitCore.html</a> <span class='footnotereverse'><a href='#fnref-489-4'>&#8617;</a></span></li><li id='fn-489-5'><a href="http://www.ibm.com/developerworks/library/os-ecl-osgiconsole/index.html">http://www.ibm.com/developerworks/library/os-ecl-osgiconsole/index.html</a> <span class='footnotereverse'><a href='#fnref-489-5'>&#8617;</a></span></li><li id='fn-489-6'>&Acirc;&nbsp;<a href="http://eclipsesource.com/blogs/2008/08/01/tip-custom-osgi-equinox-console-commands/">http://eclipsesource.com/blogs/2008/08/01/tip-custom-osgi-equinox-console-commands/</a> <span class='footnotereverse'><a href='#fnref-489-6'>&#8617;</a></span></li><li id='fn-489-7'>Good Artists Copy, Great Artists Steal &#8211; Pablo Picasso, probably. Also look for <span class="caps">T.S.</span> Eliot quotes in that context. Steve Jobs stole it, too. In the late eighties, if you judge by his hair. <span class='footnotereverse'><a href='#fnref-489-7'>&#8617;</a></span></li><li id='fn-489-8'>Valiantly deciphering <span class="caps">XML </span>(plugin/application context/deployment descriptors) to learn how dependencies are wired at runtime, too. Provided the tooling is good. Otherwise it can be quite frustrating. Luckily the <span class="caps">PDE </span>tooling for navigating plugin.xml &amp; manifest files is excellent. The search also feels a lot faster on Helios. <span class='footnotereverse'><a href='#fnref-489-8'>&#8617;</a></span></li><li id='fn-489-9'>You can do the same in pre-helios builds: In the Plug-ins view, select all plug-ins and choose &#8216;add to Java search&#8217; from the context menu <span class='footnotereverse'><a href='#fnref-489-9'>&#8617;</a></span></li><li id='fn-489-10'>Ctrl-H <span class='footnotereverse'><a href='#fnref-489-10'>&#8617;</a></span></li><li id='fn-489-11'>This is not pre-recorded. I&#8217;m making it up as we go along. Perhaps I left out some dead ends, though. See if you can spot them in the screen shots. <span class='footnotereverse'><a href='#fnref-489-11'>&#8617;</a></span></li><li id='fn-489-12'>Yep, we simplified the getHelp() method. Using constants is good style, &Acirc;&nbsp;but only if you use them more than once. <span class="caps">KISS, </span>for this example. <span class='footnotereverse'><a href='#fnref-489-12'>&#8617;</a></span></li><li id='fn-489-13'>In a weird, nerdy way, at least. <span class='footnotereverse'><a href='#fnref-489-13'>&#8617;</a></span></li><li id='fn-489-14'><a href="http://blog.wolfgang-werner.net/building-on-equinox-and-restlet-1/#Running">http://blog.wolfgang-werner.net/building-on-equinox-and-restlet-1/#Running</a> <span class='footnotereverse'><a href='#fnref-489-14'>&#8617;</a></span></li></ol></div>]]></content:encoded>
			<wfw:commentRss>http://blog.wolfgang-werner.net/osgi-console-in-helios-monkey-see-monkey-do-and-a-strange-loop/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Building web services on Equinox and Restlet #3</title>
		<link>http://blog.wolfgang-werner.net/building-web-services-on-equinox-and-restlet-3/</link>
		<comments>http://blog.wolfgang-werner.net/building-web-services-on-equinox-and-restlet-3/#comments</comments>
		<pubDate>Wed, 05 May 2010 00:09:35 +0000</pubDate>
		<dc:creator>Wolfgang Werner</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[equinox]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[extension point]]></category>
		<category><![CDATA[osgi]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[restlet]]></category>

		<guid isPermaLink="false">http://blog.wolfgang-werner.net/?p=341</guid>
		<description><![CDATA[After having implemented the server application, we want to prepare ourselves for future changes. We want to be able to add new services to our application without impacting existing ones. We also may want to change the structure of the URIs in case they are not cool. 

To do so, we'll create extension points and the supporting code for the Restlet types used and contribute our implementations and see how that this helps us making our application more flexible and maintainable.

We'll learn how to define an extension point, how to contribute to it and how to work with the contributions.]]></description>
			<content:encoded><![CDATA[<div class='toc wptoc'>
<h2>Contents</h2>
<ol class='toc-odd level-1'>
	<li>
		<a href="#Prerequisites">Prerequisites</a>
	</li>
	<li>
		<a href="#Introducing_Extension_Points">Introducing Extension Points</a>
	</li>
	<li>
		<a href="#The_Restlet_Registry_-_Extension_Points">The Restlet Registry - Extension Points</a>
	</li>
	<li>
		<a href="#The_Restlet_Registry_-_Implementation">The Restlet Registry - Implementation</a>
	</li>
	<li>
		<a href="#They_tried_to_make_me_go_to_recap_...">They tried to make me go to recap ...</a>
	</li>
</ol>
</ol>
</div>
<br />
<span id="Prerequisites"><h2>Prerequisites</h2></span>
You should have skimmed over the first two posts of this series. If they do not contain new information for you you to not have to work through them, however since we are going to build upon the results from the last post, you should get the latest code from the post2 branch<sup class='footnote'><a href='#fn-341-1' id='fnref-341-1' onclick='return fdfootnote_show(341)'>1</a></sup>&Acirc;&nbsp;if you do not have it yet.<br />
<span id="Introducing_Extension_Points"><h2>Introducing Extension Points</h2></span>
The extension point mechanism is a powerful feature that allows multiple bundles to communicate with each other. A bundle defining one or many extension points basically says &#8220;I am extensible. You can add your extension code here. &#8220;. An extension contributed to an extension point provides additional functionality to its bundle. The twist is that you can extend the bundle providing the extension point without making any changes to it. This is&Acirc;&nbsp;particularly&Acirc;&nbsp;useful when you want to customize/build upon an application without&Acirc;&nbsp;modifying&Acirc;&nbsp;it<sup class='footnote'><a href='#fn-341-2' id='fnref-341-2' onclick='return fdfootnote_show(341)'>2</a></sup>.

<p>The extension point is a piece of <span class="caps">XML </span>schema that defines how contributions to that point are to be structured, i.e. which attributes they need in which cardinality and of which type. The contribution to the extension point is made in the plugin.xml file of the contributing bundle. In addition to that, there needs to be some code that evaluates all contributions to the extension point and provides them to the core implementation. Note that this also separates the implementation of single components (code) from their use (contribution). In my experience, this leads to a significantly improved overall design as the definition modular structure of an application is clearly separated from its implementation and people tend to think about modularization earlier and more clearly as a part of the development workflow<sup class='footnote'><a href='#fn-341-3' id='fnref-341-3' onclick='return fdfootnote_show(341)'>3</a></sup>.</p>

<p>Eclipse and others&Acirc;&nbsp;uses this paradigm e.g. to provide <span class="caps">IDE</span>s in very different flavors<sup class='footnote'><a href='#fn-341-4' id='fnref-341-4' onclick='return fdfootnote_show(341)'>4</a></sup> <sup class='footnote'><a href='#fn-341-5' id='fnref-341-5' onclick='return fdfootnote_show(341)'>5</a></sup> <sup class='footnote'><a href='#fn-341-6' id='fnref-341-6' onclick='return fdfootnote_show(341)'>6</a></sup>, but they all share a common core. &Acirc;&nbsp;I daresay that this &Acirc;&nbsp;is one of the key factors to the success of Eclipse and its wonderfully vibrant and diverse ecosystem. The implementation of&Acirc;&nbsp;extension mechanism is provided by the Eclipse Equinox project <sup class='footnote'><a href='#fn-341-7' id='fnref-341-7' onclick='return fdfootnote_show(341)'>7</a></sup>. You could argue that a similar result can be achieved by using plain <span class="caps">OSG</span>i (declarative) services, perhaps we&#8217;ll do so in a later post. For a comparison refer to Neil Bartlett&#8217;s excellent posts<sup class='footnote'><a href='#fn-341-8' id='fnref-341-8' onclick='return fdfootnote_show(341)'>8</a></sup> <sup class='footnote'><a href='#fn-341-9' id='fnref-341-9' onclick='return fdfootnote_show(341)'>9</a></sup>.</p>

If you want to dig deeper, there is quite a number of useful articles on that topic out there <sup class='footnote'><a href='#fn-341-10' id='fnref-341-10' onclick='return fdfootnote_show(341)'>10</a></sup> <sup class='footnote'><a href='#fn-341-11' id='fnref-341-11' onclick='return fdfootnote_show(341)'>11</a></sup> <sup class='footnote'><a href='#fn-341-12' id='fnref-341-12' onclick='return fdfootnote_show(341)'>12</a></sup> <sup class='footnote'><a href='#fn-341-13' id='fnref-341-13' onclick='return fdfootnote_show(341)'>13</a></sup>.<br />
<span id="The_Restlet_Registry_8211_Extension_Points"><h2>The Restlet Registry &#8211; Extension Points</h2></span>
The backbone of our new architecture will be the Restlet registry. It will contain the definitions of our extension points &Acirc;&nbsp;and the supporting code that evaluates contributions to these extension points.

Our final registry will consist of multiple extension points:<br />
<ul>
	<li>Resources</li>
	<li>Components</li>
	<li>Applications</li>
	<li>Routers</li>
	<li>Filters</li>
</ul>
For this post, we&#8217;ll only implement the first two, the other two will be in the example code as well<sup class='footnote'><a href='#fn-341-14' id='fnref-341-14' onclick='return fdfootnote_show(341)'>14</a></sup> but I won&#8217;t describe them in detail here<sup class='footnote'><a href='#fn-341-15' id='fnref-341-15' onclick='return fdfootnote_show(341)'>15</a></sup>.&Acirc;&nbsp;To gather the contributed extensions, wire them at runtime&Acirc;&nbsp;and start &Acirc;&nbsp;the application at runtime, we&#8217;ll also need to write some code.

<p>To avoid clutter<sup class='footnote'><a href='#fn-341-16' id='fnref-341-16' onclick='return fdfootnote_show(341)'>16</a></sup>, we&#8217;ll put all registry components into a new bundle. Create a new bundle as described in the first post <sup class='footnote'><a href='#fn-341-17' id='fnref-341-17' onclick='return fdfootnote_show(341)'>17</a></sup> and call it &#8220;net.wolfgangwerner.restlet.registry&#8221;.&Acirc;&nbsp;<div class="wpgalleryimage_shortcode" style="width: 501px; height: 578px; float: none;"><img width="499" height="576" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/create-registry-bundle.png" class="attachment-full" alt="create-registry-bundle" title="create-registry-bundle" /></div></p>

<p>Let&#8217;s start with the first extension point, Resources. A resource consist of an ID and a corresponding implementation class. First, activate the editors for extensions in the new project&#8217;s properties.<br />
<div class="wpgalleryimage_shortcode" style="width: 348px; height: 204px; float: none;"><img width="346" height="202" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/activate-editors-1.png" class="attachment-full" alt="activate-editors-1" title="activate-editors-1" /></div> <div class="wpgalleryimage_shortcode" style="width: 547px; height: 349px; float: none;"><img width="545" height="347" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/activate-editors-2.png" class="attachment-full" alt="activate-editors-2" title="activate-editors-2" /></div></p>

<p>To be able to reference the the ServerResource interface, it needs to be added to the registry bundle&#8217;s dependencies.<br />
&iuml;&raquo;&iquest;<div class="wpgalleryimage_shortcode" style="width: 365px; height: 242px; float: none;"><img width="363" height="240" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/registry-dependencies-1.png" class="attachment-full" alt="registry-dependencies-1" title="registry-dependencies-1" /></div></p>

<p>After that, open the <span class="caps">MANIFEST.MF </span>file from the <span class="caps">META</span>-INF folder an go to the extension point tab. Click &#8220;Add&#8221; to add a new extension point. Enter &#8216;resources&#8217; as ID <sup class='footnote'><a href='#fn-341-18' id='fnref-341-18' onclick='return fdfootnote_show(341)'>18</a></sup> and &#8216;Restlet Resources&#8217; as name.<br />
<div class="wpgalleryimage_shortcode" style="width: 751px; height: 390px; float: none;"><img width="749" height="388" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/new-ext-point-1.png" class="attachment-full" alt="new-ext-point-1" title="new-ext-point-1" /></div>&Acirc;&nbsp;<div class="wpgalleryimage_shortcode" style="width: 428px; height: 216px; float: none;"><img width="426" height="214" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/ext-point-schema-1.1.png" class="attachment-full" alt="ext-point-schema-1.1" title="ext-point-schema-1.1" /></div></p>

<p>Clicking &#8220;Ok&#8221; takes us the extension point schema editor. You should <strong>really</strong> enter useful information on how the extensions contributed will be handled. The function of your extension point may seem clear to you now, but remember you are building an interface for public (more or less) consumption.<br />
<div class="wpgalleryimage_shortcode" style="width: 753px; height: 389px; float: none;"><img width="751" height="387" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/ext-point-schema-1.2.png" class="attachment-full" alt="ext-point-schema-1.2" title="ext-point-schema-1.2" /></div></p>

<p>Now let&#8217;s go to the &#8220;Definition&#8221; tab and add a new element &#8220;<strong>resource</strong>&#8220;. Add the attributes &#8220;<strong>id</strong>&#8220;, &#8220;<strong>name</strong>&#8221; and &#8220;<strong>class</strong>&#8221; to the new element. IDs &Acirc;&nbsp;and names should have the type <strong>String</strong>; Classes the type Java with the restriction that contributed classes must implement Restlet&#8217;s <strong>ServerResource</strong> interface. Then select the &#8216;extension&#8217; element and add a &#8220;New Sequence&#8221; of the cardinality 1:n. Add a reference to the &#8220;resource&#8221; element to the new sequence.</p>

<p><div class="wpgalleryimage_shortcode" style="width: 757px; height: 268px; float: none;"><img width="755" height="266" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/ext-point-schema-1.31.png" class="attachment-full" alt="ext-point-schema-1.3" title="ext-point-schema-1.3" /></div>&Acirc;&nbsp;<div class="wpgalleryimage_shortcode" style="width: 757px; height: 393px; float: none;"><img width="755" height="391" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/ext-point-schema-1.4.png" class="attachment-full" alt="ext-point-schema-1.4" title="ext-point-schema-1.4" /></div></p>

<p>The next extension point to implement &Acirc;&nbsp;is the one for Restlet Components. It should be able to handle a collection of component elements, each with a name for human (developer) readable representation, a list of servers it uses (each with protocol and port) and a list of referenced resources. Note that if you create references to IDs, you can also restrict the element you want to reference. Go ahead and try to create the extension point on your own now. If you get stuck, the screenshots<sup class='footnote'><a href='#fn-341-19' id='fnref-341-19' onclick='return fdfootnote_show(341)'>19</a></sup> below should help.</p>


<a href='http://blog.wolfgang-werner.net/building-web-services-on-equinox-and-restlet-3/ext-point-schema-1-2/' title='ext-point-schema-1'><img width="150" height="150" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/ext-point-schema-11-150x150.png" class="attachment-thumbnail" alt="ext-point-schema-1" title="ext-point-schema-1" /></a>
<a href='http://blog.wolfgang-werner.net/building-web-services-on-equinox-and-restlet-3/ext-point-schema-2/' title='ext-point-schema-2'><img width="150" height="150" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/ext-point-schema-2-150x150.png" class="attachment-thumbnail" alt="ext-point-schema-2" title="ext-point-schema-2" /></a>
<a href='http://blog.wolfgang-werner.net/building-web-services-on-equinox-and-restlet-3/ext-point-schema-3/' title='ext-point-schema-3'><img width="150" height="150" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/ext-point-schema-3-150x150.png" class="attachment-thumbnail" alt="ext-point-schema-3" title="ext-point-schema-3" /></a>
<a href='http://blog.wolfgang-werner.net/building-web-services-on-equinox-and-restlet-3/ext-point-schema-4/' title='ext-point-schema-4'><img width="150" height="150" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/ext-point-schema-4-150x150.png" class="attachment-thumbnail" alt="ext-point-schema-4" title="ext-point-schema-4" /></a>
<a href='http://blog.wolfgang-werner.net/building-web-services-on-equinox-and-restlet-3/ext-point-schema-5/' title='ext-point-schema-5'><img width="150" height="150" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/ext-point-schema-5-150x150.png" class="attachment-thumbnail" alt="ext-point-schema-5" title="ext-point-schema-5" /></a>
<a href='http://blog.wolfgang-werner.net/building-web-services-on-equinox-and-restlet-3/ext-point-schema-6/' title='ext-point-schema-6'><img width="150" height="150" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/ext-point-schema-6-150x150.png" class="attachment-thumbnail" alt="ext-point-schema-6" title="ext-point-schema-6" /></a>
<a href='http://blog.wolfgang-werner.net/building-web-services-on-equinox-and-restlet-3/ext-point-schema-7/' title='ext-point-schema-7'><img width="150" height="150" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/ext-point-schema-7-150x150.png" class="attachment-thumbnail" alt="ext-point-schema-7" title="ext-point-schema-7" /></a>


The following list summarizes all extension point elements created so far:<br />
<ul>
	<li>Resources
<ul>
	<li>resource (1:*)
<ul>
	<li>id (String)</li>
	<li>name (String)</li>
	<li>class (Java, implements org.restlet.resource.ServerResource)</li>
</ul>
</li>
</ul>
</li>
	<li>Components
<ul>
	<li>component (1:*)
<ul>
	<li>name (String)</li>
</ul>
</li>
	<li>a &Acirc;&nbsp;list (1:*)&Acirc;&nbsp;of elements of the types
<ul>
	<li>server
<ul>
	<li>name (String)</li>
	<li>protocol (String)</li>
	<li>port (String)</li>
</ul>
</li>
	<li>resourceReference &Acirc;&nbsp;(1:*)
<ul>
	<li>urlTemplate (String)</li>
	<li>resourceId (Identifier, Restlet Resources/@id)</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<strong>Contributing Restlet Extensions</strong>

<p>Go back to the original net.wolfgangwerner.tutorial1.restlet bundle and open the plugin.xml file and go to the &#8220;Extensions&#8221; tab. Click &#8220;Add&#8221; to add a new extension point to contribute to. In the following dialog, deselect &#8220;Show only extension points from the required plug-ins&#8221;. Select the resources extension point we just created and click &#8220;Finish&#8221;. When prompted, confirm you want to add the registry plug-in to the list of required bundles.<br />
<div class="wpgalleryimage_shortcode" style="width: 501px; height: 350px; float: none;"><img width="499" height="348" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/contributing-resource-1.png" class="attachment-full" alt="contributing-resource-1" title="contributing-resource-1" /></div></p>

<p>Since we specified a lower bound of one element for the resource element, one has already been created for us. <span class="caps">PDE </span>is smart enough to parse our extension point schema and configure the editor correspondingly. <sup class='footnote'><a href='#fn-341-20' id='fnref-341-20' onclick='return fdfootnote_show(341)'>20</a></sup></p>

<p>Enter an <span class="caps">ID, </span>a name and the implementing class. The ID is used later to attach the resource to the Restlet Component. You should use a fully qualified ID (i.e. including the bundle name) here to make it unique within the runtime<sup class='footnote'><a href='#fn-341-21' id='fnref-341-21' onclick='return fdfootnote_show(341)'>21</a></sup>. Though you can choose any ID you like, using a fully qualified one protects you from name clashes with extensions in other bundles. The name will be used as label in the extension editor; use a label that helps you identifying it once you contributed a larger number of resources. To chose the implementing class, you can use the code assist shortcut Ctrl+Space.<br />
<div class="wpgalleryimage_shortcode" style="width: 743px; height: 165px; float: none;"><img width="741" height="163" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/contributing-resource-2.png" class="attachment-full" alt="contributing-resource-2" title="contributing-resource-2" /></div></p>

<p>Repeat the last steps to contribute the Greeting List Resource in the same manner.</p>

<p>Then add a component extension and use its context menu to define two servers (e.g. <span class="caps">HTTP </span>on port 80 and on port 8080). Then add a resource reference element, enter &#8220;/greet&#8221; as <span class="caps">URL </span>template and browse for the corresponding identifier<sup class='footnote'><a href='#fn-341-22' id='fnref-341-22' onclick='return fdfootnote_show(341)'>22</a></sup>.</p>

<p><div class="wpgalleryimage_shortcode" style="width: 539px; height: 98px; float: none;"><img width="537" height="96" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/contributing-component-1.png" class="attachment-full" alt="contributing-component-1" title="contributing-component-1" /></div> <div class="wpgalleryimage_shortcode" style="width: 396px; height: 235px; float: none;"><img width="394" height="233" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/contributing-component-2.png" class="attachment-full" alt="contributing-component-2" title="contributing-component-2" /></div></p>

Now your extension definitions should look like this:<br />
<div class="wpgalleryimage_shortcode" style="width: 626px; height: 242px; float: none;"><img width="624" height="240" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/contributing-done.png" class="attachment-full" alt="contributing-done" title="contributing-done" /></div><br />
<span id="The_Restlet_Registry_8211_Implementation"><h2>The Restlet Registry &#8211; Implementation</h2></span>
Now that we have defined our extension points and contributed our code to them, we need to write the code that actually does something with this information.

<p>As we need to reference the registry bundle to get its extension points, we add an ID to its Activator. The ID is the same as the bundle id of the registry bundle<sup class='footnote'><a href='#fn-341-23' id='fnref-341-23' onclick='return fdfootnote_show(341)'>23</a></sup>.</p>

<script src="http://gist.github.com/391321.js"></script>

<p>To keep the code that evaluates the contributed extensions separate from our bundle life cycle code, we create a new class called RestletRegistry that encapsulates . We can simply ask the Platform to give us all contributions to a specific extension point. &Acirc;&nbsp;We specify the extension point by its namespace <sup class='footnote'><a href='#fn-341-24' id='fnref-341-24' onclick='return fdfootnote_show(341)'>24</a></sup> and the name of the extension point. We iterate over the list of contributions and save the contents in our Restlet Registry. This is straight forward for the &#8216;id&#8217; attribute but requires some thought for the resource class. While there is a shortcut to create an executable extension (read: Object) from a class attribute, this doesn&#8217;t help us: The Restlet <span class="caps">API </span>requires us to attach the resource&#8217;s Class. In a non-OSGi environment, we could simply call Class.forName(String), but this will fail here as each bundle has a separate class loader. Instead, we need to get the contributing bundle and let it load the class for us using its own class loaded<sup class='footnote'><a href='#fn-341-25' id='fnref-341-25' onclick='return fdfootnote_show(341)'>25</a></sup>.</p>

<script src="http://gist.github.com/391371.js"></script>

<p>Getting the data from the component extensions is pretty straightforward. We get the extensions as we did for the resources, create a Restlet component for each extension, then add the corresponding servers to it and attach the resources at the given <span class="caps">URL </span>templates.<br />
To keep our code clean<sup class='footnote'><a href='#fn-341-26' id='fnref-341-26' onclick='return fdfootnote_show(341)'>26</a></sup>, we split the parsing into several small methods<sup class='footnote'><a href='#fn-341-27' id='fnref-341-27' onclick='return fdfootnote_show(341)'>27</a></sup>.</p>

<script src="http://gist.github.com/391475.js"></script>

<p>You&#8217;ll have noted that there are several exceptions that can be thrown when working with extensions. In real life, you&#8217;ll need to safeguard your code against invalid extensions<sup class='footnote'><a href='#fn-341-28' id='fnref-341-28' onclick='return fdfootnote_show(341)'>28</a></sup>. Invalid extensions should be ignored and logged but may not impact other extensions contributed to the same extension point or obstruct the operation of the core.</p>

<p>In the last post, we started our server from the Activator of the first bundle. Since we are going to start all contributed components instead of hardcoding them, we can delete this Activator class and remove it from the Manifest.<br />
</p>

<p>Now all we have to do to make it run is to tie the registered component&#8217;s life cycle to the one of our registry bundle and adjust our launch configuration to include and auto-start the registry bundle.<br />
<script src="http://gist.github.com/391488.js"></script></p>

Launch it and verify that the greeting service is active by pointing your browser to<br />
<span id="They_tried_to_make_me_go_to_recap_8230"><h2>They tried to make me go to recap &#8230;</h2></span>
You just created a small framework to build Restlet based web services and run them on the equinox <span class="caps">OSG</span>i runtime. You started by building an <span class="caps">OSG</span>i bundle that contained a simple Restlet web service. You rolled this bundle into a standalone server application. In the second post, you extended the application and learned a little bit about Restlet. Along the way, you also picked up some <span class="caps">OSG</span>i knowledge.

<p>In this post, we improved our application by allowing the contribution of additional resources from other bundles as well.</p>

<p>The application is not yet finished, I left some work for you<sup class='footnote'><a href='#fn-341-29' id='fnref-341-29' onclick='return fdfootnote_show(341)'>29</a></sup>. The next step is to create more extension points to allow the contribution of other Restlet subclasses (Filters, Applications and Routers) and finish the migration of the version from post #2.</p>

<p>Prior art includes the presentation of Shams and Norris on eclipsecon 2008<sup class='footnote'><a href='#fn-341-30' id='fnref-341-30' onclick='return fdfootnote_show(341)'>30</a></sup>. Originally I wanted to give the code they created a try, but was deterred by a strange license and download process. Others have noticed that as well <sup class='footnote'><a href='#fn-341-31' id='fnref-341-31' onclick='return fdfootnote_show(341)'>31</a></sup>.</p>

<p>I will wrap the final results of the Restlet Registry with all extension points into a project ready to use, stay tuned <sup class='footnote'><a href='#fn-341-32' id='fnref-341-32' onclick='return fdfootnote_show(341)'>32</a></sup> if you are interested. I&#8217;d also be happy to discuss whether the current extension point setup matches real world requirements and how it could be improved. Implementing a Restlet <span class="caps">OSG</span>i service is also very conceivable. Let me know if you want to help or simply go ahead and fork the repository.</p>

<div class='footnotes' id='footnotes-341'><div class='footnotedivider'></div><ol><li id='fn-341-1'>git clone git://github.com/wwerner/EquinoxRestletTutorial.git

<p>cd EquinoxRestletTutorial<br />
git branch -a<br />
git checkout -b post2 origin/post2</p>

or http://github.com/wwerner/EquinoxRestletTutorial/zipball/post3 <span class='footnotereverse'><a href='#fnref-341-1'>&#8617;</a></span></li><li id='fn-341-2'>Standard Software + Customizing / <span class="caps">ISV </span>+ Implementation Partners, anyone? <span class='footnotereverse'><a href='#fnref-341-2'>&#8617;</a></span></li><li id='fn-341-3'>&#8220;Oi, this class shouldn&#8217;t call that class from the other layer&#8221;
&#8220;Which other layer?&#8221;<br />
&#8220;Well, you can see which layer a class belongs to by it&#8217;s name. We have a naming convention for that.&#8221;<br />
&#8220;Oh, Bob never uses it. He finds that the names become unreadable when applying it.&#8221;<br />
&#8220;It also is in the wrong package.&#8221;<br />
&#8220;I did&#8217;t import it! Strg-O did, so I figured I could use it.&#8221;<br />
&#8220;&#8230;&#8221; <span class='footnotereverse'><a href='#fnref-341-3'>&#8617;</a></span></li><li id='fn-341-4'>See <a href="http://www.eclipse.org/downloads/">http://www.eclipse.org/downloads/</a> <span class='footnotereverse'><a href='#fnref-341-4'>&#8617;</a></span></li><li id='fn-341-5'>&Acirc;&nbsp;<a href="http://www.borland.com/de/products/together/index.html">http://www.borland.com/de/products/together/index.html</a> <span class='footnotereverse'><a href='#fnref-341-5'>&#8617;</a></span></li><li id='fn-341-6'>&Acirc;&nbsp;<a href="http://www.sdn.sap.com/irj/sdn/nw-devstudio">http://www.sdn.sap.com/irj/sdn/nw-devstudio</a> <span class='footnotereverse'><a href='#fnref-341-6'>&#8617;</a></span></li><li id='fn-341-7'><a href="http://www.eclipse.org/projects/project_summary.php?projectid=rt.equinox">http://www.eclipse.org/projects/project_summary.php?projectid=rt.equinox</a> <span class='footnotereverse'><a href='#fnref-341-7'>&#8617;</a></span></li><li id='fn-341-8'><a href="http://www.eclipsezone.com/articles/extensions-vs-services/">http://www.eclipsezone.com/articles/extensions-vs-services/</a> <span class='footnotereverse'><a href='#fnref-341-8'>&#8617;</a></span></li><li id='fn-341-9'>Or use his e2s project as glue:&Acirc;&nbsp;<a href="http://github.com/njbartlett/Extensions2Services">http://github.com/njbartlett/Extensions2Services</a> <span class='footnotereverse'><a href='#fnref-341-9'>&#8617;</a></span></li><li id='fn-341-10'>&Acirc;&nbsp;<a href="http://www.eclipsezone.com/eclipse/forums/t93753.html">http://www.eclipsezone.com/eclipse/forums/t93753.html</a> <span class='footnotereverse'><a href='#fnref-341-10'>&#8617;</a></span></li><li id='fn-341-11'><a href="http://www.eclipsezone.com/eclipse/forums/t97608.rhtml">http://www.eclipsezone.com/eclipse/forums/t97608.rhtml</a> <span class='footnotereverse'><a href='#fnref-341-11'>&#8617;</a></span></li><li id='fn-341-12'><a href="http://www.vogella.de/articles/EclipseExtensionPoint/article.html">http://www.vogella.de/articles/EclipseExtensionPoint/article.html</a> <span class='footnotereverse'><a href='#fnref-341-12'>&#8617;</a></span></li><li id='fn-341-13'><a href="http://www.sigs.de/publications/js/2008/01/hennig_seeberger_JS_01_08.pdf">http://www.sigs.de/publications/js/2008/01/hennig_seeberger_JS_01_08.pdf</a> (german) <span class='footnotereverse'><a href='#fnref-341-13'>&#8617;</a></span></li><li id='fn-341-14'>At some point in time <span class='footnotereverse'><a href='#fnref-341-14'>&#8617;</a></span></li><li id='fn-341-15'>Partly because I&#8217;m not sure how they should look like exactly <span class='footnotereverse'><a href='#fnref-341-15'>&#8617;</a></span></li><li id='fn-341-16'>And also to demonstrate the interaction of multiple bundles <span class='footnotereverse'><a href='#fnref-341-16'>&#8617;</a></span></li><li id='fn-341-17'><a href="http://blog.wolfgang-werner.net/building-on-equinox-and-restlet-1/#Creating_the_Application_Bundle">http://blog.wolfgang-werner.net/building-on-equinox-and-restlet-1/#Creating_the_Application_Bundle</a> <span class='footnotereverse'><a href='#fnref-341-17'>&#8617;</a></span></li><li id='fn-341-18'>The fully qualified ID will be the plug-in ID plus the extension point <span class="caps">ID, </span>so you have to worry about uniqueness only in the namespace of your bundle. <span class='footnotereverse'><a href='#fnref-341-18'>&#8617;</a></span></li><li id='fn-341-19'>Or &#8211; of course &#8211; the code <span class='footnotereverse'><a href='#fnref-341-19'>&#8617;</a></span></li><li id='fn-341-20'>Never really noticed it&Acirc;&nbsp;consciously. But&Acirc;&nbsp;actually pretty neat! <span class='footnotereverse'><a href='#fnref-341-20'>&#8617;</a></span></li><li id='fn-341-21'>The extension point schemas got that prefix from the wizard <span class='footnotereverse'><a href='#fnref-341-21'>&#8617;</a></span></li><li id='fn-341-22'>Neat <span class='footnotereverse'><a href='#fnref-341-22'>&#8617;</a></span></li><li id='fn-341-23'>This is a little <span class="caps">WET </span>(Write Everything Twice ||&Acirc;&nbsp;Wicked, Evil and Tedious). Not <a title="DRY" href="http://c2.com/cgi/wiki?DontRepeatYourself"><span class="caps">DRY</span></a>. You could also obtain the current bundle from the BundleContext in the Activator and use its symbolic name <span class='footnotereverse'><a href='#fnref-341-23'>&#8617;</a></span></li><li id='fn-341-24'>i.e. the ID of the bundle as added to the activator <span class='footnotereverse'><a href='#fnref-341-24'>&#8617;</a></span></li><li id='fn-341-25'>Note that making the registry bundle&Acirc;&nbsp;dependent on the contributing bundle is not an option. This would force us to change the generic registry whenever a new bundle contributes a resource. The whole point of having an extension point is to have independent/third party code extend core functionality w/o modifying the core&Acirc;&nbsp; <span class='footnotereverse'><a href='#fnref-341-25'>&#8617;</a></span></li><li id='fn-341-26'><a href="http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882">http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882</a> <span class='footnotereverse'><a href='#fnref-341-26'>&#8617;</a></span></li><li id='fn-341-27'>Alt-Shift-M or Alt-Shift-T, x is your <a title="friend" href="http://www.refactoring.com/catalog/extractMethod.html">friend</a>.  <span class='footnotereverse'><a href='#fnref-341-27'>&#8617;</a></span></li><li id='fn-341-28'>What if a contributed resource is not derived from ServerResource? What if the extension does not provide all required attributes? <span class='footnotereverse'><a href='#fnref-341-28'>&#8617;</a></span></li><li id='fn-341-29'>Also, the post is getting long. And it is getting late. <span class='footnotereverse'><a href='#fnref-341-29'>&#8617;</a></span></li><li id='fn-341-30'><a href="http://www.eclipsecon.org/2008/?page=sub/&amp;id=462">http://www.eclipsecon.org/2008/?page=sub/&amp;id=462</a> <span class='footnotereverse'><a href='#fnref-341-30'>&#8617;</a></span></li><li id='fn-341-31'><a href="http://bryanhunt.wordpress.com/2009/11/23/updating-ensemble-for-restlet-2-0-license-beware/">http://bryanhunt.wordpress.com/2009/11/23/updating-ensemble-for-restlet-2-0-license-beware/</a> <span class='footnotereverse'><a href='#fnref-341-31'>&#8617;</a></span></li><li id='fn-341-32'>&Acirc;&nbsp;<a href="http://twitter.com/0xcafebabe">http://twitter.com/0xcafebabe</a>,&Acirc;&nbsp;<a href="http://github.com/wwerner">http://github.com/wwerner</a> <span class='footnotereverse'><a href='#fnref-341-32'>&#8617;</a></span></li></ol></div>]]></content:encoded>
			<wfw:commentRss>http://blog.wolfgang-werner.net/building-web-services-on-equinox-and-restlet-3/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Building web services on Equinox and Restlet #2</title>
		<link>http://blog.wolfgang-werner.net/building-web-services-on-equinox-and-restlet-2/</link>
		<comments>http://blog.wolfgang-werner.net/building-web-services-on-equinox-and-restlet-2/#comments</comments>
		<pubDate>Sun, 11 Apr 2010 09:12:19 +0000</pubDate>
		<dc:creator>Wolfgang Werner</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[management console]]></category>
		<category><![CDATA[osgi]]></category>
		<category><![CDATA[rant]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[restlet]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://blog.wolfgang-werner.net/?p=340</guid>
		<description><![CDATA[Now that we've implemented our greeting application, it's time to make it more enterprisey. We can't have it just echo any greeting, can we? We'll need to make sure that only greetings from a predefined list can be chosen as current greeting, so we can have committees that can spend time discussing and defining greeting standards. 
As we want to make our lives easier (let's assume the committee will change it's mind every few days or so), we'll implement a greeting manager that can be used to edit the list of approved greetings.

Beside rants at software development for The Enterprise(TM), expect  a short introduction of the Restlet base classes that are typically assembled into a server application. ]]></description>
			<content:encoded><![CDATA[<div>

<div class='toc wptoc'>
<h2>Contents</h2>
<ol class='toc-odd level-1'>
	<li>
		<a href="#Cherry_Picking">Cherry Picking</a>
	</li>
	<li>
		<a href="#Server_Architecture">Server Architecture</a>
	</li>
	<li>
		<a href="#Putting_it_together">Putting it together</a>
	</li>
	<li>
		<a href="#Must._Resist._Urge_...">Must. Resist. Urge ...</a>
	</li>
	<li>
		<a href="#Where_to_go_from_here">Where to go from here</a>
	</li>
</ol>
</ol>
</div>
<br />
<span id="Cherry_Picking"><h2>Cherry Picking</h2></span>
There are books <sup class='footnote'><a href='#fn-340-1' id='fnref-340-1' onclick='return fdfootnote_show(340)'>1</a></sup> <sup class='footnote'><a href='#fn-340-2' id='fnref-340-2' onclick='return fdfootnote_show(340)'>2</a></sup> that cover Restlet, so it would be both futile and pointless to try to give you an complete overview. Refer to the Restlet documentation <sup class='footnote'><a href='#fn-340-3' id='fnref-340-3' onclick='return fdfootnote_show(340)'>3</a></sup> and the books if you are primarily interested in Restlet. This post only illuminates a few concepts of Restlet that need to be understood as a foundation for the next post where we are going to wrap Restlet into Eclipse extension points.

<p>So, what is that Restlet thing anyway? Basically, Restlet is a library that assists you to in building <span class="caps">REST </span>style applications <sup class='footnote'><a href='#fn-340-4' id='fnref-340-4' onclick='return fdfootnote_show(340)'>4</a></sup>. Restlet implements many core concepts from Roy T. Fieldings dissertation <sup class='footnote'><a href='#fn-340-5' id='fnref-340-5' onclick='return fdfootnote_show(340)'>5</a></sup> directly, so if you are familiar with the <span class="caps">REST </span>architectural style, you&#8217;ll recognize many of the base classes of the <span class="caps">API </span>immediately.</p>

<p>The most&Acirc;&nbsp;notable&Acirc;&nbsp;concept for our purpose is the uniform interface. It allows components to to be assembled arbitrarily without revealing their internal structure to clients. Clients don&#8217;t need to realize to which kind of component they are talking to, this allows the server components to change without breaking existing clients. Real life examples are Pipes on *nix systems and &#8211; of course &#8211; the web. Your browser did not notice to which components it was talking to when you were loading this page. Chances are that there are multiple caches, a proxy and a virtual host between you and the physical storage location of this content. The browser doesn&#8217;t know about this; all it cares about is that its <span class="caps">GET </span>request got handled. And all components along the way know how to do that: They have a uniform interface <sup class='footnote'><a href='#fn-340-6' id='fnref-340-6' onclick='return fdfootnote_show(340)'>6</a></sup>.</p>

<p>Restlet allows for building both client and server applications. We&#8217;ll concentrate on the server side in this tutorial. The figures below are actually annotated copies from the Restlet documentation <sup class='footnote'><a href='#fn-340-3' id='fnref-340-3' onclick='return fdfootnote_show(340)'>3</a></sup>, filtered to exclude all elements we won&#8217;t use in this tutorial <sup class='footnote'><a href='#fn-340-8' id='fnref-340-8' onclick='return fdfootnote_show(340)'>8</a></sup></p>

<div class="wpgalleryimage_shortcode" style="width: 767px; height: 502px; float: none;"><img width="765" height="500" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/RestletClasses.png" class="attachment-full" alt="RestletClasses" title="RestletClasses" /></div>

<p>Since all the the classes that implement the UniformInterface know how to handle <span class="caps">HTTP </span>requests, we are able to assemble them very flexibly. Great, this promotes loose coupling in our application. I was tempted to include a matrix of which combinations are valid, but that is beside the point. Better think of it as a variation of the classic composite pattern <sup class='footnote'><a href='#fn-340-9' id='fnref-340-9' onclick='return fdfootnote_show(340)'>9</a></sup>, like this: &#8220;Everything is a Restlet (Component). There are special Restlets that can contain other&Acirc;&nbsp;Restlets (Composites).&#8221;</p>

<p>While the Restlet derived classes are mostly used to define the hierarchical &Acirc;&nbsp;structure and the management of the application, the actual business data is served and updated by Resources. Resources are not part of the Restlet inheritance hierarchy but are derived from a common UniformResource base class. Uniform? Heard that before? Yes, Resources also know how to handle <span class="caps">HTTP </span>requests. They are the targets of a hypertext reference/URI.</p>

<div class="wpgalleryimage_shortcode" style="width: 379px; height: 227px; float: none;"><img width="377" height="225" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/Resources.png" class="attachment-full" alt="Resources" title="Resources" /></div>

<p>To go back to the Composite pattern reference, we now also know what the Leafs of the composite pattern are: &#8220;Everything is a Restlet (Component). There are special Restlets that can contain other&Acirc;&nbsp;Restlets (Composites). ServerResources are Leafs.&#8221;</p>

<p>Given I understood everything correctly, I think the Restlet <span class="caps">API </span>could make these points more intuitive by adding a common base class for composite Restlets and by including UniformResource as leafs into the Component inheritance hierarchy. But I do not claim to have any authority on that topic.</p>

If you are familiar with Restlet, you&#8217;ll note that this section &Acirc;&nbsp;contains analogies that are not 100% accurate and even partly misleading. I am aware of that, but I believe thinking of in Composites is a good way to get started with the assembly of a Restlet based application. Now shoot me, or flame away in the comments. <sup class='footnote'><a href='#fn-340-10' id='fnref-340-10' onclick='return fdfootnote_show(340)'>10</a></sup><br />
<span id="Server_Architecture"><h2>Server Architecture</h2></span>
Back to our application. Let&#8217;s elaborate on the requirements stated in the teaser:<br />
<ul>
	<li> The server must able to issue a greeting</li>
	<li>There needs to be a controllable list of approved greetings</li>
	<li>The current greeting must be selectable by a administrator</li>
	<li>An administrator needs the possibility to add and remove greetings from the list</li>
	<li>The current list contents must by visible to the administrator</li>
	<li>For the sake of the examples, a greeting in the list needs to have a mnemonic by which it can be found. The list of greetings is actually a map of mnemonics and greetings.</li>
</ul>
</div>
This means we&#8217;ll have two resources: A greeting and a list of greetings. The list can be modified by two operations: &#8220;add greeting&#8221; and &#8220;remove greeting&#8221;. I&#8217;ll leave setting the default greeting to you as an&Acirc;&nbsp;exercise.

When we map this to an <span class="caps">URI </span>structure, we get something like this <sup class='footnote'><a href='#fn-340-11' id='fnref-340-11' onclick='return fdfootnote_show(340)'>11</a></sup>:<br />
<ul>
	<li>greet: issues the currently active greeting</li>
	<li>manage: base for the administrative section</li>
	<li>manage/list: show all greetings</li>
	<li>manage/list/{mnemonic}: Display a greeting with <span class="caps">GET, </span>add or modify a greeting with <span class="caps">POST, </span>remove it with <span class="caps">DELETE</span></li>
</ul>
Now what does that {mnemonic} thing in the <span class="caps">URL </span>mean? This is simply a variable that is part of the <span class="caps">URL </span>and will be extracted by Restlet. So the last <span class="caps">URL </span>is not a concrete <span class="caps">URL </span>but rather an <span class="caps">URL </span>template. There is a normative specification of <span class="caps">URL </span>templates &Acirc;&nbsp;<sup class='footnote'><a href='#fn-340-12' id='fnref-340-12' onclick='return fdfootnote_show(340)'>12</a></sup> that is implemented in Restlet. <span class="caps">URL </span>templates are quite powerful, reading the specification is time well spent.

In real life, you may want to think about exposing your domain model directly by annotating it, i.e. make resources from your model classes. This has pros and cons, but is worth considering.<br />
<div>
<span id="Putting_it_together"><h2>Putting it together</h2></span>
Now that we know that we have many degrees of freedom to assemble our components, we can make the initial application from the first post a little simpler: We&#8217;ll attach the greeting Resource directly to the server without having a application a a router in between. Both can safely be removed for now.

<script src="http://gist.github.com/355709.js"></script>

<p>As want to control the allowed greeting values, we&#8217;ll have to remove the @Put method in GreetingResource to disallow rouge write access.</p>

<script src="http://gist.github.com/355693.js"></script>

<p>We modify GreetingStorage to hold a map of valid greetings and allows us to add and remove greetings.</p>

<script src="http://gist.github.com/355702.js"></script>

We also need to implement our new GreetingListResource. This resource fulfills three of our requirements:<br />
<ol>
	<li>It displays the current greeting map content a single greeting (list())</li>
	<li>It adds a given greeting to the list (set(), via <span class="caps">POST </span>&amp; a &#8220;greeting&#8221; parameter )</li>
	<li>It removes a given greeting from the list (remove())</li>
</ol>
<script src="http://gist.github.com/362365.js"></script>

<p>The interesting part is how the application will be assembled: We first create a component and attach an <span class="caps">HTTP </span>server to it. Then we create an application containing our management section. This is &#8211; for the time being &#8211; only a container that creates a new namespace for the administration part . To this application, we attach a Router that is configured to route the requests to GreetingListResource and get the mnemonic part of the <span class="caps">URI</span>s.</p>

<script src="http://gist.github.com/362395.js"></script>

<p>As in the last post, the complete code is available on github <sup class='footnote'><a href='#fn-340-13' id='fnref-340-13' onclick='return fdfootnote_show(340)'>13</a></sup>.</p>

Now use Poster as described in the last post with the following <span class="caps">URL</span>s:<br />
<ul>
	<li><a href="http://localhost:8182/tutorial2/manage/list">http://localhost:8182/tutorial2/manage/list</a></li>
	<li><a href="http://localhost:8182/tutorial2/manage/list/default">http://localhost:8182/tutorial2/manage/list/default</a></li>
	<li><span class="caps">POST </span>a parameter called &#8220;greeting&#8221; to&Acirc;&nbsp;<a href="http://localhost:8182/tutorial2/manage/list/newGreeting">http://localhost:8182/tutorial2/manage/list/newGreeting</a></li>
	<li>Show the list again</li>
</ul>
</div>
<div class="wpgalleryimage_shortcode" style="width: 589px; height: 76px; float: none;"><img width="587" height="74" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/3-NewList.png" class="attachment-full" alt="3-NewList" title="3-NewList" /></div>

<p>Of course you can also <span class="caps">DELETE </span>&Acirc;&nbsp;greetings:</p>

<div class="wpgalleryimage_shortcode" style="width: 554px; height: 245px; float: none;"><img width="552" height="243" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/4-Delete.png" class="attachment-full" alt="4-Delete" title="4-Delete" /></div><br />
<div>
<span id="Must._Resist._Urge_8230"><h2>Must. Resist. Urge &#8230;</h2></span>
&#8230; to throw in mostly unrelated <span class="caps">OSG</span>i stuff,&Acirc;&nbsp;but I can&#8217;t.

<p>You&#8217;ll have noticed that the the component local variable was turned into a member variable and that a second method, stop() was introduced. Analogous to start() the stop() method of the Activator is called whenever the <span class="caps">OSG</span>i runtime stops the bundle.</p>

<p>I did this to be able to hot deploy the bundle within the running <span class="caps">OSG</span>i framework without having to restart the server. Sure, if I start the application from the <span class="caps">IDE</span> I run it in debug mode, but this won&#8217;t help when I make changes to the activator. Remember, the start() method is only called when the bundle is first loaded. As most of the application assembly currently happens there, we&#8217;d still have to restart the whole framework for the changes to be picked up.</p>

<p><span class="caps">OSG</span>i to the rescue! One of <span class="caps">OSG</span>i core features is the ability to manage (start, stop, install, update) individual bundles within a running framework. <span class="caps">OSG</span>i describes management agents that are able to do so. Equinox ships with a management agent as a console.&Acirc;&nbsp;Let&#8217;s see how we can use that in our example.</p>

<p>First, we need to tell our launch config to enable the management console.</p>

<div class="wpgalleryimage_shortcode" style="width: 501px; height: 117px; float: none;"><img width="499" height="115" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/cons-1.png" class="attachment-full" alt="cons-1" title="cons-1" /></div>

<p>Start the application in debug mode. In the console, you&#8217;ll see an &#8220;osgi&gt;&#8221; prompt now. Type &#8220;ss&#8221; (for short status) and hit enter. Hey, there is our bundle.</p>

<div class="wpgalleryimage_shortcode" style="width: 488px; height: 160px; float: none;"><img width="486" height="158" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/cons-2.png" class="attachment-full" alt="cons-2" title="cons-2" /></div>

<p>Remember the ID for subsequent commands. You can also use the full name of the bundle, but we&#8217;re lazy, right<sup class='footnote'><a href='#fn-340-14' id='fnref-340-14' onclick='return fdfootnote_show(340)'>14</a></sup>?</p>

<div class="wpgalleryimage_shortcode" style="width: 590px; height: 363px; float: none;"><img width="588" height="361" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/cons-3.png" class="attachment-full" alt="cons-3" title="cons-3" /></div>

<p>Let&#8217;s suppose you want to change the management <span class="caps">URL </span>from &#8220;manage&#8221; to &#8220;admin&#8221; <sup class='footnote'><a href='#fn-340-15' id='fnref-340-15' onclick='return fdfootnote_show(340)'>15</a></sup> <sup class='footnote'><a href='#fn-340-16' id='fnref-340-16' onclick='return fdfootnote_show(340)'>16</a></sup>.</p>

<script src="http://gist.github.com/362595.js"></script>

<p>So you change that piece of code &Acirc;&nbsp;and check the new <span class="caps">URL.</span> And receive a lovely 404 because no one called the start method in our Activator. So let&#8217;s use the management console to restart the bundle: We remembered the bundle ID and just call &#8220;stop &lt;ID&gt;&#8221; and &#8220;start &lt;ID&gt;&#8221; in sequence. Now you also see the purpose of the stop() method addition above. We basically attached our components life cycle to the one of our bundle.</p>

<div class="wpgalleryimage_shortcode" style="width: 646px; height: 127px; float: none;"><img width="644" height="125" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/cons-4.png" class="attachment-full" alt="cons-4" title="cons-4" /></div>

<p>Hint: Use the &#8220;help&#8221; command in the console to see if there is a shorter way to do the restart.</p>

<p>Now point your browser to the new <span class="caps">URL </span>&iuml;&raquo;&iquest;(http://localhost:8182/tutorial2/admin/list) and be utterly astonished. It works.</p>

<div class="wpgalleryimage_shortcode" style="width: 465px; height: 84px; float: none;"><img width="463" height="82" src="http://blog.wolfgang-werner.net/wp-content/uploads/2010/04/cons-5.png" class="attachment-full" alt="cons-5" title="cons-5" /></div><br />
<span id="Where_to_go_from_here"><h2>Where to go from here</h2></span>
You now know how to assemble Restlet applications by using Components, Applications, Routers and Resources. You have learned how to enable and use the equinox <span class="caps">OSG</span>i management console. From the last post, you already saw how all this can be wrapped up into a standalone application.

<p>If you want to dive in a little deeper into Restlet, check out the great tutorials on the Restlet home page<sup class='footnote'><a href='#fn-340-17' id='fnref-340-17' onclick='return fdfootnote_show(340)'>17</a></sup> <sup class='footnote'><a href='#fn-340-18' id='fnref-340-18' onclick='return fdfootnote_show(340)'>18</a></sup> <sup class='footnote'><a href='#fn-340-19' id='fnref-340-19' onclick='return fdfootnote_show(340)'>19</a></sup>. Some of this will be familiar. A great next step would be to learn what different Representations of a Resource are and how the content types are negotiated.</p>

<p>The next post will introduce equinox&#8217; extension point/extension paradigm and remove the concrete application assembly from the Activator by using it. We&#8217;ll also tackle the hidden requirement that we didn&#8217;t implement. Can you find it?</p>

<p>A last word of warning: Now that you know how to create those nice looking <span class="caps">URL</span>s, don&#8217;t build a product and go around advertising your <span class="caps">REST</span>ful <span class="caps">API, </span>or the holy wrath of Roy T. Fielding will consume your soul <sup class='footnote'><a href='#fn-340-20' id='fnref-340-20' onclick='return fdfootnote_show(340)'>20</a></sup>. You (and I, for that matter) still have a lot to learn. Use the term &#8220;CRUD over <span class="caps">HTTP</span>&#8221; for now, even if it gives you less buzz. Seriously.</p>

<p>I came across the headline &#8220;Tutorials should foster thinking&#8221; in a smashingmag post yesterday. Did this tutorial make you think? Leave a comment.</p>

</div>

<div class='footnotes' id='footnotes-340'><div class='footnotedivider'></div><ol><li id='fn-340-1'><a href="http://www.amazon.com/Restful-Web-Services-Leonard-Richardson/dp/0596529260?ie=UTF8&amp;s=books&amp;qid=1173381194&amp;sr=8-1">http://www.amazon.com/Restful-Web-Services-Leonard-Richardson/dp/0596529260?ie=UTF8&amp;s=books&amp;qid=1173381194&amp;sr=8-1</a> <span class='footnotereverse'><a href='#fnref-340-1'>&#8617;</a></span></li><li id='fn-340-2'><a href="http://www.manning.com/louvel/">http://www.manning.com/louvel/</a> <span class='footnotereverse'><a href='#fnref-340-2'>&#8617;</a></span></li><li id='fn-340-3'>&Acirc;&nbsp;<a href="http://www.restlet.org/documentation/2.0/">http://www.restlet.org/documentation/2.0/</a> <span class='footnotereverse'><a href='#fnref-340-3'>&#8617;</a></span></li><li id='fn-340-4'>You&#8217;ll have figured that out by now <span class='footnotereverse'><a href='#fnref-340-4'>&#8617;</a></span></li><li id='fn-340-5'>&Acirc;&nbsp;<a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm">http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm</a> <span class='footnotereverse'><a href='#fnref-340-5'>&#8617;</a></span></li><li id='fn-340-6'>In this case, they speak <span class="caps">HTTP </span><span class='footnotereverse'><a href='#fnref-340-6'>&#8617;</a></span></li><li id='fn-340-7'>&Acirc;&nbsp;<a href="http://www.restlet.org/documentation/2.0/">http://www.restlet.org/documentation/2.0/</a> <span class='footnotereverse'><a href='#fnref-340-7'>&#8617;</a></span></li><li id='fn-340-8'>Namely, these are: Clients, VirtualHosts, Filters and Redirectors. Refer to the Restlet homepage for details. <span class='footnotereverse'><a href='#fnref-340-8'>&#8617;</a></span></li><li id='fn-340-9'>&Acirc;&nbsp;<a href="http://en.wikipedia.org/wiki/Composite_pattern">http://en.wikipedia.org/wiki/Composite_pattern</a> <span class='footnotereverse'><a href='#fnref-340-9'>&#8617;</a></span></li><li id='fn-340-10'>Oh, and nice comments are welcome as well. <span class='footnotereverse'><a href='#fnref-340-10'>&#8617;</a></span></li><li id='fn-340-11'>Relative to our base <span class="caps">URL, </span>http://localhost:8182/tutorial1/ <span class='footnotereverse'><a href='#fnref-340-11'>&#8617;</a></span></li><li id='fn-340-12'>&Acirc;&nbsp;<a href="http://bitworking.org/projects/URI-Templates/">http://bitworking.org/projects/URI-Templates/</a> <span class='footnotereverse'><a href='#fnref-340-12'>&#8617;</a></span></li><li id='fn-340-13'>&Acirc;&nbsp;<a title="http://github.com/wwerner/EquinoxRestletTutorial/tree/post2" href="http://github.com/wwerner/EquinoxRestletTutorial/tree/post2">http://github.com/wwerner/EquinoxRestletTutorial/tree/post2</a> (There is a branch for each post/part of a post of this series) <span class='footnotereverse'><a href='#fnref-340-13'>&#8617;</a></span></li><li id='fn-340-14'><a href="http://c2.com/cgi/wiki?LazinessImpatienceHubris">http://c2.com/cgi/wiki?LazinessImpatienceHubris</a> <span class='footnotereverse'><a href='#fnref-340-14'>&#8617;</a></span></li><li id='fn-340-15'>You also want to do this on the live system, as it is very urgent.&Acirc;&nbsp;The steering committee found that &#8220;manage&#8221; conflicted with the corporate &#8220;Use-Of-The-Word-Management-In-Software-Application&#8221; policy and filed a change request. The request, however, got stuck in the workflow as one of the required approvers retired two years ago. By the time you got the issue on your desk, it was already delayed and suffered some serious upper management attention. Someone you report to decided to skip the tests in order to make up for the delay&Acirc;&nbsp;in spite&Acirc;&nbsp;of your protesting. <span class='footnotereverse'><a href='#fnref-340-15'>&#8617;</a></span></li><li id='fn-340-16'>No, the live system will not run in debug mode from your <span class="caps">IDE, </span>but give an old men the possibility to let off a little steam, would ya? <span class='footnotereverse'><a href='#fnref-340-16'>&#8617;</a></span></li><li id='fn-340-17'><a href="http://www.restlet.org/documentation/2.0/firstSteps">http://www.restlet.org/documentation/2.0/firstSteps</a> <span class='footnotereverse'><a href='#fnref-340-17'>&#8617;</a></span></li><li id='fn-340-18'><a href="http://www.restlet.org/documentation/2.0/firstResource">http://www.restlet.org/documentation/2.0/firstResource</a> <span class='footnotereverse'><a href='#fnref-340-18'>&#8617;</a></span></li><li id='fn-340-19'><a href="http://www.restlet.org/documentation/2.0/tutorial">http://www.restlet.org/documentation/2.0/tutorial</a>  <span class='footnotereverse'><a href='#fnref-340-19'>&#8617;</a></span></li><li id='fn-340-20'><a href="http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven">http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven</a> <span class='footnotereverse'><a href='#fnref-340-20'>&#8617;</a></span></li></ol></div>]]></content:encoded>
			<wfw:commentRss>http://blog.wolfgang-werner.net/building-web-services-on-equinox-and-restlet-2/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Building web services on Equinox and Restlet #1</title>
		<link>http://blog.wolfgang-werner.net/building-on-equinox-and-restlet-1/</link>
		<comments>http://blog.wolfgang-werner.net/building-on-equinox-and-restlet-1/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 10:46:57 +0000</pubDate>
		<dc:creator>Wolfgang Werner</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[osgi]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[restlet]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://blog.wolfgang-werner.net/?p=120</guid>
		<description><![CDATA[In this series, we'll look at creating modular and flexible server applications based on OSGi/Equinox and Restlet. The first article of this series covers the basics: It will walk you through creating a simple REST style web service using Restlet and show you how to roll it into a standalone application based on the equinox OSGi framework.

The next posts will go into how the different Restlet types can be assembled into bigger applications. We'll then build extension points to lift the actual plumbing to a declarative level instead of hardcoding it. 

We'll see that the uniform interface proposed in the REST architectural style and the extension/extension point mechanisms in Equinox make quite a good couple.

]]></description>
			<content:encoded><![CDATA[<br />
<b>Fatal error</b>:  Allowed memory size of 67108864 bytes exhausted (tried to allocate 7680 bytes) in <b>/var/www/web241/html/blog/wp-content/plugins/textile-2/class/Textile.php(3245) : runtime-created function</b> on line <b>1</b><br />

