<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Mike's Very Occasional Blog</title>
	<atom:link href="http://mikesveryoccasionalblog.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://mikesveryoccasionalblog.wordpress.com</link>
	<description>When inspiration strikes</description>
	<lastBuildDate>Fri, 23 Jan 2009 15:18:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='mikesveryoccasionalblog.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Mike's Very Occasional Blog</title>
		<link>http://mikesveryoccasionalblog.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://mikesveryoccasionalblog.wordpress.com/osd.xml" title="Mike&#039;s Very Occasional Blog" />
	<atom:link rel='hub' href='http://mikesveryoccasionalblog.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Using OSGi Services in an existing RCP Application</title>
		<link>http://mikesveryoccasionalblog.wordpress.com/2009/01/23/using-osgi-services-in-an-existing-rcp-application/</link>
		<comments>http://mikesveryoccasionalblog.wordpress.com/2009/01/23/using-osgi-services-in-an-existing-rcp-application/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 15:18:50 +0000</pubDate>
		<dc:creator>mikesveryoccasionalblog</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://mikesveryoccasionalblog.wordpress.com/?p=20</guid>
		<description><![CDATA[How to add and reference declarative OSGi services in your RCP application.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mikesveryoccasionalblog.wordpress.com&amp;blog=6279328&amp;post=20&amp;subd=mikesveryoccasionalblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Summary</strong><br />
How to integrate declared OSGi services into an existing RCP application.</p>
<p><strong>Background</strong><br />
Traditionally you <a href="http://eclipsezone.com/eclipse/forums/t90688.html">register</a> and <a href="http://www.eclipsezone.com/eclipse/forums/t90796.html">consume</a> OSGi services using Java code.  However in a truly dynamic system with bundles coming and going, your code must explicitly <a href="http://www.eclipsezone.com/eclipse/forums/t91059.rhtml">track the services</a>.  This can get very complicated with multiple services and multiple dependencies between them. As of OSGi 4.1 (and Equinox in Eclipse 3.3) we can use <a href="http://www.eclipsezone.com/eclipse/forums/t96740.html">declarative services</a> to <a href="http://www.eclipsezone.com/eclipse/forums/t97690.rhtml">deal with dependencies</a>. There are great links to deal with all of this, in particular those supplied by <a href="http://neilbartlett.name/blog/osgi-articles/">Neil Bartlett</a>.  However it is not at all clear how to use declaratively declared services in an existing GUI application.  That&#8217;s the purpose of this post.</p>
<p><strong>Caveat</strong><br />
Would you even want to do this?  As of 3.4, GUI RCP apps are fundamentally not dynamic as the extension point based UI is not itself dynamic.  Hence any service-based dynamism should be hidden behind some static facade that the UI accesses.  However, this is good thought experiment to better understand declarative services in OSGI.</p>
<p><strong>Solution Theory</strong><br />
The declarative services are all defined and intantiated via entries in xml files within their bundles. However how does an RCP application that is defined and instantiated via extension points (in particular <code>org.eclipse.core.runtime.applications</code>) then access these?<br />
The answer is the <code>reference</code> element in declarative services.  This allows OSGI components to define dependencies and, crucially, have methods that are passed explicit Java references to these dependencies.  These methods are defined in the <code>bind</code> and <code>unbind</code> attributes, and can be used as callbacks to provide your Java code with references to the declared services.</p>
<p>Let us say use have declared a new OSGi service based in an interface <code>IMyService</code>.  This is now running in your VM but you have no Java reference to it. So:</p>
<ol>
<li>In your Java code create another interface <code>IMyServiceConsumer</code> with a <code>setService(IMyService)</code> method.</li>
<li>Create an implementation of <code>IMyServiceConsumer</code> and programmatically register this as an OSGi service.  This is now running in OSGi but of course its <code>setService</code> has not yet been called.</li>
<li>Now create an OSGi component &#8211; <code>MyServiceMediator</code> which has an <code>addService(IMyService) </code>and an  <code>addConsumer(IMyServiceConsumer)</code> methods.  The <code>addService</code> will simply store the <code>IMyService</code> reference whilst the <code>addConsumer</code> will use this in the callback <code>IMyServiceConsumer.setService()</code></li>
<li>The final step is to declare this component adding <code>reference</code> elements for both <code>IMyService</code> and <code>IMyServiceConsumer</code>.  Each reference element has <code>bind</code> and <code>unbind</code> attributes that refer to methods that will be supplied with a reference to the service.  Each method is called whenever an instance of the service is registered or unregistered.  This reference can then be passed on to your client code.</li>
</ol>
<p><strong>Solution Practice</strong><br />
These are steps to go through in practice:</p>
<ol>
<li>Create a bundle containing the IMyService interface and an implementation;</li>
<li>Define this as a declarative service with an xml file in the OSGI-INF directory: <img class="aligncenter size-full wp-image-26" title="screenhunter_06-jan-23-1450" src="http://mikesveryoccasionalblog.files.wordpress.com/2009/01/screenhunter_06-jan-23-1450.jpg?w=500" alt="screenhunter_06-jan-23-1450"   /></li>
<li>Ensure the xml file is picked up by OSGi declarative services by adding a line to the bundle&#8217;s MANIFEST.MF file: <img class="aligncenter size-full wp-image-23" title="screenhunter_03-jan-23-1432" src="http://mikesveryoccasionalblog.files.wordpress.com/2009/01/screenhunter_03-jan-23-1432.jpg?w=500" alt="screenhunter_03-jan-23-1432"   /></li>
<li>Create the <code>IMyServiceConsumer</code> interface and an implementation;</li>
<li>Whenever the implementation class requires a reference to <code>IMyService</code> it should programmatically register itself as an OSGi service, hence obtaining the <code>IMyService</code> via the callback, and then deregister itself as soon as possible: <img class="aligncenter size-full wp-image-24" title="screenhunter_04-jan-23-1440" src="http://mikesveryoccasionalblog.files.wordpress.com/2009/01/screenhunter_04-jan-23-1440.jpg?w=500&#038;h=422" alt="screenhunter_04-jan-23-1440" width="500" height="422" /></li>
<li>Create the <code>MyServiceMediator</code> implementation:</li>
<p><img class="aligncenter size-full wp-image-25" title="screenhunter_05-jan-23-1446" src="http://mikesveryoccasionalblog.files.wordpress.com/2009/01/screenhunter_05-jan-23-1446.jpg?w=500&#038;h=218" alt="screenhunter_05-jan-23-1446" width="500" height="218" /></p>
<li>Declare this as an OSGi component, adding the essential <code>reference</code> elements to ensure all the bind methods are called: <img src="http://mikesveryoccasionalblog.files.wordpress.com/2009/01/screenhunter_09-jan-23-1514.jpg?w=500" alt="screenhunter_09-jan-23-1514" title="screenhunter_09-jan-23-1514"   class="alignleft size-full wp-image-28" />
<li>Finally split this up into seperate bundles as suits your purposes &#8211; it is likely you will want the actual <code>IMyService</code> implementation in a seperate, swapable bundle.</li>
</ol>
<p><strong>Notes and Further Caveats</strong></p>
<ul>
<li>Why use a mediator at all?  Can the core service be set up with <code>reference</code> elements direct?  Absolutely!  However the mediator allows you to keep the main service interface completely &#8216;clean&#8217; &#8211; with no knowlede of consumers etc.</li>
<li>You will still need to do some coding to account for dynamic behaviour as <code>IMyService</code> might be unregistered at any time.  Ensure either the mediator and/or the consumer can deal with this situation &#8211; most probably using a method defined by the unbind attribute on the service declaration.</li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mikesveryoccasionalblog.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mikesveryoccasionalblog.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mikesveryoccasionalblog.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mikesveryoccasionalblog.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mikesveryoccasionalblog.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mikesveryoccasionalblog.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mikesveryoccasionalblog.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mikesveryoccasionalblog.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mikesveryoccasionalblog.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mikesveryoccasionalblog.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mikesveryoccasionalblog.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mikesveryoccasionalblog.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mikesveryoccasionalblog.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mikesveryoccasionalblog.wordpress.com/20/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mikesveryoccasionalblog.wordpress.com&amp;blog=6279328&amp;post=20&amp;subd=mikesveryoccasionalblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mikesveryoccasionalblog.wordpress.com/2009/01/23/using-osgi-services-in-an-existing-rcp-application/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4f283b6be9f8d71e39512c7fc6819e99?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mikesveryoccasionalblog</media:title>
		</media:content>

		<media:content url="http://mikesveryoccasionalblog.files.wordpress.com/2009/01/screenhunter_06-jan-23-1450.jpg" medium="image">
			<media:title type="html">screenhunter_06-jan-23-1450</media:title>
		</media:content>

		<media:content url="http://mikesveryoccasionalblog.files.wordpress.com/2009/01/screenhunter_03-jan-23-1432.jpg" medium="image">
			<media:title type="html">screenhunter_03-jan-23-1432</media:title>
		</media:content>

		<media:content url="http://mikesveryoccasionalblog.files.wordpress.com/2009/01/screenhunter_04-jan-23-1440.jpg" medium="image">
			<media:title type="html">screenhunter_04-jan-23-1440</media:title>
		</media:content>

		<media:content url="http://mikesveryoccasionalblog.files.wordpress.com/2009/01/screenhunter_05-jan-23-1446.jpg" medium="image">
			<media:title type="html">screenhunter_05-jan-23-1446</media:title>
		</media:content>

		<media:content url="http://mikesveryoccasionalblog.files.wordpress.com/2009/01/screenhunter_09-jan-23-1514.jpg" medium="image">
			<media:title type="html">screenhunter_09-jan-23-1514</media:title>
		</media:content>
	</item>
		<item>
		<title>Hullo</title>
		<link>http://mikesveryoccasionalblog.wordpress.com/2009/01/22/hullo/</link>
		<comments>http://mikesveryoccasionalblog.wordpress.com/2009/01/22/hullo/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 18:23:37 +0000</pubDate>
		<dc:creator>mikesveryoccasionalblog</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Here I am going to scratch some of the personal itches I have with Eclipse related technologies. I hope I scratch some of yours too.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mikesveryoccasionalblog.wordpress.com&amp;blog=6279328&amp;post=1&amp;subd=mikesveryoccasionalblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here I am going to scratch some of the personal itches I have with <a href="http://www.eclipse.org">Eclipse</a> related technologies. I hope I scratch some of yours too.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mikesveryoccasionalblog.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mikesveryoccasionalblog.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mikesveryoccasionalblog.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mikesveryoccasionalblog.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mikesveryoccasionalblog.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mikesveryoccasionalblog.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mikesveryoccasionalblog.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mikesveryoccasionalblog.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mikesveryoccasionalblog.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mikesveryoccasionalblog.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mikesveryoccasionalblog.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mikesveryoccasionalblog.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mikesveryoccasionalblog.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mikesveryoccasionalblog.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mikesveryoccasionalblog.wordpress.com&amp;blog=6279328&amp;post=1&amp;subd=mikesveryoccasionalblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mikesveryoccasionalblog.wordpress.com/2009/01/22/hullo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4f283b6be9f8d71e39512c7fc6819e99?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mikesveryoccasionalblog</media:title>
		</media:content>
	</item>
	</channel>
</rss>
