<?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>code.pst</title>
	<atom:link href="http://www.irahul.com/codepst/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.irahul.com/codepst</link>
	<description>tech musings and code posts</description>
	<lastBuildDate>Thu, 01 Oct 2009 06:49:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Spring DM OSGi demo app</title>
		<link>http://www.irahul.com/codepst/?p=35</link>
		<comments>http://www.irahul.com/codepst/?p=35#comments</comments>
		<pubDate>Tue, 29 Sep 2009 06:47:45 +0000</pubDate>
		<dc:creator>rahul</dc:creator>
				<category><![CDATA[OSGi]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[spring DM]]></category>

		<guid isPermaLink="false">http://www.irahul.com/codepst/?p=35</guid>
		<description><![CDATA[As you will see Spring DM makes life much simpler! It manages all &#8220;OSGi aware&#8221; pieces of the application for you and you can rid of the Activators.
The bundles we need now are similar as before. I will prefix &#8220;spring&#8221; to the artifactId just so its easier. 
The shared service: We don&#8217;t need to change [...]]]></description>
			<content:encoded><![CDATA[<p>As you will see <a href="http://www.springsource.org/osgi">Spring DM</a> makes life much simpler! It manages all &#8220;OSGi aware&#8221; pieces of the application for you and you can rid of the Activators.</p>
<p>The bundles we need now are similar as before. I will prefix &#8220;spring&#8221; to the artifactId just so its easier. </p>
<p><strong>The shared service:</strong> We don&#8217;t need to change anything in shared so just use the older one as-is.</p>
<p><strong>Creating a service implementation:</strong> The <code>ComplexService</code> we defined now needs to be implemented and made available as an OSGi service. Lets create a simple ComplexServiceImpl:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ComplexServiceImpl <span style="color: #000000; font-weight: bold;">implements</span> ComplexService <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Hello from Version 1!&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To register the service we now use the OSGi extender provided by Spring DM. All we need to do is create some XML configs.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> ..... <span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
  <span style="color: #808080; font-style: italic;">&lt;!-- Create bean --&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;complexService&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.irahul.spring.complexapp.ComplexServiceImpl&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
  <span style="color: #808080; font-style: italic;">&lt;!-- register service --&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;osgi:service</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;complexService&quot;</span> <span style="color: #000066;">interface</span>=<span style="color: #ff0000;">&quot;com.irahul.shared.ComplexService&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><strong>Creating the servlet:</strong> In Spring world we just create the handler and then wire it up using XML.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyHandler <span style="color: #000000; font-weight: bold;">implements</span> Servlet <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> HttpService httpService<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>ComplexService<span style="color: #339933;">&gt;</span> complexServices<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setHttpService<span style="color: #009900;">&#40;</span>HttpService httpService<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">httpService</span> <span style="color: #339933;">=</span> httpService<span style="color: #339933;">;</span>		
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setComplexServices<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>ComplexService<span style="color: #339933;">&gt;</span> complexServices<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">complexServices</span> <span style="color: #339933;">=</span> complexServices<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//ideally this should be externalized. Have a manager listen for servlets but keeping it simple</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span><span style="color: #009900;">&#123;</span>
		httpService.<span style="color: #006633;">registerServlet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span>, <span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> service<span style="color: #009900;">&#40;</span>ServletRequest request, ServletResponse response<span style="color: #009900;">&#41;</span>
		<span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//we may have discovered more than 1 service</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>complexServices<span style="color: #339933;">==</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">||</span> complexServices.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			response.<span style="color: #006633;">getOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No Service found!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//there are 1...n services, pick one</span>
			<span style="color: #666666; font-style: italic;">//using a random pick</span>
			response.<span style="color: #006633;">getOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>complexServices.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>
					<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span>complexServices.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">doSomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	....
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Configuring it:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> ..... <span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
  <span style="color: #808080; font-style: italic;">&lt;!-- Reference list of services in OSGi --&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;osgi:list</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;complexServiceList&quot;</span> <span style="color: #000066;">interface</span>=<span style="color: #ff0000;">&quot;com.irahul.shared.ComplexService&quot;</span> <span style="color: #000066;">cardinality</span>=<span style="color: #ff0000;">&quot;0..N&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
  <span style="color: #808080; font-style: italic;">&lt;!-- Reference to Http Service --&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;osgi:reference</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;httpService&quot;</span> <span style="color: #000066;">interface</span>=<span style="color: #ff0000;">&quot;org.osgi.service.http.HttpService&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
  <span style="color: #808080; font-style: italic;">&lt;!-- Create handler --&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;myHandler&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.irahul.spring.http.handler.MyHandler&quot;</span> <span style="color: #000066;">init-method</span>=<span style="color: #ff0000;">&quot;init&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;complexServices&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;complexServiceList&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;httpService&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;httpService&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><a href='http://www.irahul.com/codepst/wp-content/uploads/2009/09/spring_osgidemo.rar'>Download full source</a>.</p>
<p><strong>Running in Eclipse Equinox</strong>: Use the following config.ini</p>
<p><code><br />
osgi.bundles=log4j.osgi-1.2.15-SNAPSHOT.jar@start, com.springsource.org.aopalliance-1.0.0.jar@start, com.springsource.slf4j.log4j-1.5.0.jar@start, com.springsource.slf4j.api-1.5.0.jar@start, com.springsource.slf4j.org.apache.commons.logging-1.5.0.jar@start, com.springsource.javax.servlet-2.4.0.jar@start, services-3.1.2.jar@start, org.eclipse.equinox.http.jar@start, org.springframework.aop-2.5.6.A.jar@start, org.springframework.beans-2.5.6.A.jar@start, org.springframework.context-2.5.6.A.jar@start, org.springframework.core-2.5.6.A.jar@start, spring-osgi-core-1.2.0.jar@start, spring-osgi-io-1.2.0.jar@start, spring-osgi-extender-1.2.0.jar@start, spring-osgi-annotation-1.2.0.jar@start, shared-1.0-SNAPSHOT.jar@start, spring.complexapp-1.0-SNAPSHOT.jar@start, spring.complexapp-2.0-SNAPSHOT.jar@start, spring.http.handler-1.0-SNAPSHOT.jar@start<br />
eclipse.ignoreApp=true<br />
</code></p>
<p>That is it, see you at code-camp!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.irahul.com/codepst/?feed=rss2&amp;p=35</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Running demo in Eclipse Equinox</title>
		<link>http://www.irahul.com/codepst/?p=67</link>
		<comments>http://www.irahul.com/codepst/?p=67#comments</comments>
		<pubDate>Fri, 25 Sep 2009 04:36:14 +0000</pubDate>
		<dc:creator>rahul</dc:creator>
				<category><![CDATA[OSGi]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[equinox]]></category>

		<guid isPermaLink="false">http://www.irahul.com/codepst/?p=67</guid>
		<description><![CDATA[Running in Eclipse Equinox: Look up basic instructions on equinox quickstart.
Add the following to your config.ini file (note two versions of the same complexapp):

osgi.bundles=javax.servlet_2.4.0.jar@start, services-3.1.2.jar@start, org.eclipse.equinox.http.jar@start, shared-1.0-SNAPSHOT.jar@start, http.handler-1.0-SNAPSHOT.jar@start, complexapp-1.0-SNAPSHOT.jar@start, complexapp-2.0-SNAPSHOT.jar@start

Testing it out: Start Equinox with -console so you have the osgi prompt. Here is a log of commands and interesting portions are highlighted (running on [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Running in Eclipse Equinox:</strong> Look up basic instructions on <a href="http://www.eclipse.org/equinox/documents/quickstart.php">equinox quickstart</a>.</p>
<p>Add the following to your config.ini file (note two versions of the same complexapp):</p>
<p><code><br />
osgi.bundles=javax.servlet_2.4.0.jar@start, services-3.1.2.jar@start, org.eclipse.equinox.http.jar@start, shared-1.0-SNAPSHOT.jar@start, http.handler-1.0-SNAPSHOT.jar@start, complexapp-1.0-SNAPSHOT.jar@start, complexapp-2.0-SNAPSHOT.jar@start<br />
</code></p>
<p><strong>Testing it out:</strong> Start Equinox with <code>-console</code> so you have the osgi prompt. Here is a log of commands and interesting portions are highlighted (running on Windows).</p>
<p>rahul@lappy /cygdrive/c/osgidemo/runtime<br />
<strong>$ java -jar org.eclipse.osgi_3.5.0.jar -console</strong> <-- Start with console</p>
<p>osgi> http.handler started! <-- osgi prompt<br />
New Service added<br />
New Service added</p>
<p>osgi> ss</p>
<p>Framework is launched.</p>
<p>id      State       Bundle<br />
0       ACTIVE      org.eclipse.osgi_3.5.0.v20090520<br />
1       ACTIVE      javax.servlet_2.4.0.v200806031604<br />
2       ACTIVE      org.eclipse.osgi.services_3.1.200.v20070605<br />
3       ACTIVE      org.eclipse.equinox.http_1.0.300.v20090520-1800<br />
4       ACTIVE      com.irahul.shared_1.0.0.SNAPSHOT<br />
5       ACTIVE      com.irahul.http.handler_1.0.0.SNAPSHOT<br />
6       ACTIVE      com.irahul.complexapp_1.0.0.SNAPSHOT<br />
7       ACTIVE      com.irahul.complexapp_2.0.0.SNAPSHOT</p>
<p>osgi> bundle 5<br />
com.irahul.http.handler_1.0.0.SNAPSHOT [5]<br />
  Id=5, Status=ACTIVE      Data Root=C:\osgidemo\runtime\configuration\org.eclip<br />
se.osgi\bundles\5\data<br />
  No registered services.<br />
  <strong>Services in use:</strong> <-- Services used, noticed multiple ComplexService<br />
    {com.irahul.shared.<strong>ComplexService</strong>}={<strong>service.id=30</strong>}<br />
    {org.osgi.service.http.<strong>HttpService</strong>}={http.port=80, service.pid=org.eclipse.e<br />
quinox.http.HttpService-http, http.address=ALL, service.vendor=IBM, service.desc<br />
ription=OSGi Http Service &#8211; IBM Implementation, http.scheme=http, http.timeout=3<br />
0, service.id=26}<br />
    {com.irahul.shared.<strong>ComplexService</strong>}={<strong>service.id=29</strong>}<br />
  No exported packages<br />
 <strong> Imported packages</strong> <-- Pacakages being imported<br />
    <strong>com.irahul.shared</strong>; version=&#8221;0.0.0&#8243;<com.irahul.shared_1.0.0.SNAPSHOT [4]><br />
    javax.servlet; version=&#8221;2.4.0&#8243;<javax.servlet_2.4.0.v200806031604 [1]><br />
    org.eclipse.equinox.http; version=&#8221;0.0.0&#8243;<org.eclipse.equinox.http_1.0.300.v<br />
20090520-1800 [3]><br />
    org.osgi.framework; version=&#8221;1.5.0&#8243;<org.eclipse.osgi_3.5.0.v20090520 [0]><br />
    org.osgi.service.http; version=&#8221;1.2.0&#8243;<org.eclipse.osgi.services_3.1.200.v20<br />
070605 [2]><br />
  No fragment bundles<br />
  Named class space<br />
    com.irahul.http.handler; bundle-version=&#8221;1.0.0.SNAPSHOT&#8221;[provided]<br />
  No required bundles</p>
<p>osgi> bundle 6<br />
com.irahul.complexapp_1.0.0.SNAPSHOT [6]<br />
  Id=6, Status=ACTIVE      Data Root=C:\osgidemo\runtime\configuration\org.eclip<br />
se.osgi\bundles\6\data<br />
  <strong>Registered Services</strong><br />
    {com.irahul.shared.ComplexService}={service.id=29}<br />
  No services in use.<br />
  No exported packages<br />
 <strong> Imported packages<br />
    com.irahul.shared</strong>; version=&#8221;0.0.0&#8243;<com.irahul.shared_1.0.0.SNAPSHOT [4]><br />
    org.osgi.framework; version=&#8221;1.5.0&#8243;<org.eclipse.osgi_3.5.0.v20090520 [0]><br />
  No fragment bundles<br />
  Named class space<br />
    com.irahul.complexapp; bundle-version=&#8221;1.0.0.SNAPSHOT&#8221;[provided]<br />
  No required bundles</p>
<p>osgi> bundle 7<br />
com.irahul.complexapp_2.0.0.SNAPSHOT [7]<br />
  Id=7, Status=ACTIVE      Data Root=C:\osgidemo\runtime\configuration\org.eclip<br />
se.osgi\bundles\7\data<br />
  <strong>Registered Services</strong><br />
    {com.irahul.shared.ComplexService}={service.id=30}<br />
  No services in use.<br />
  No exported packages<br />
  <strong>Imported packages<br />
    com.irahul.shared;</strong> version=&#8221;0.0.0&#8243;<com.irahul.shared_1.0.0.SNAPSHOT [4]><br />
    org.osgi.framework; version=&#8221;1.5.0&#8243;<org.eclipse.osgi_3.5.0.v20090520 [0]><br />
  No fragment bundles<br />
  Named class space<br />
    com.irahul.complexapp; bundle-version=&#8221;2.0.0.SNAPSHOT&#8221;[provided]<br />
  No required bundles</p>
<p><strong>osgi> stop 6</strong><br />
Service stopped</p>
<p><strong>osgi> stop 7</strong><br />
Service stopped</p>
<p>osgi> ss</p>
<p>Framework is launched.</p>
<p>id      State       Bundle<br />
0       ACTIVE      org.eclipse.osgi_3.5.0.v20090520<br />
1       ACTIVE      javax.servlet_2.4.0.v200806031604<br />
2       ACTIVE      org.eclipse.osgi.services_3.1.200.v20070605<br />
3       ACTIVE      org.eclipse.equinox.http_1.0.300.v20090520-1800<br />
4       ACTIVE      com.irahul.shared_1.0.0.SNAPSHOT<br />
5       ACTIVE      com.irahul.http.handler_1.0.0.SNAPSHOT<br />
<strong>6       RESOLVED    com.irahul.complexapp_1.0.0.SNAPSHOT<br />
7       RESOLVED    com.irahul.complexapp_2.0.0.SNAPSHOT</strong></p>
<p>osgi> bundle 5<br />
com.irahul.http.handler_1.0.0.SNAPSHOT [5]<br />
  Id=5, Status=ACTIVE      Data Root=C:\osgidemo\runtime\configuration\org.eclip<br />
se.osgi\bundles\5\data<br />
  No registered services.<br />
<strong>  Services in use:</strong> <-- ComplexService disappear<br />
    {org.osgi.service.http.HttpService}={http.port=80, service.pid=org.eclipse.e<br />
quinox.http.HttpService-http, http.address=ALL, service.vendor=IBM, service.desc<br />
ription=OSGi Http Service - IBM Implementation, http.scheme=http, http.timeout=3<br />
0, service.id=26}<br />
  No exported packages<br />
  Imported packages<br />
    com.irahul.shared; version="0.0.0"<com.irahul.shared_1.0.0.SNAPSHOT [4]><br />
    javax.servlet; version=&#8221;2.4.0&#8243;<javax.servlet_2.4.0.v200806031604 [1]><br />
    org.eclipse.equinox.http; version=&#8221;0.0.0&#8243;<org.eclipse.equinox.http_1.0.300.v<br />
20090520-1800 [3]><br />
    org.osgi.framework; version=&#8221;1.5.0&#8243;<org.eclipse.osgi_3.5.0.v20090520 [0]><br />
    org.osgi.service.http; version=&#8221;1.2.0&#8243;<org.eclipse.osgi.services_3.1.200.v20<br />
070605 [2]><br />
  No fragment bundles<br />
  Named class space<br />
    com.irahul.http.handler; bundle-version=&#8221;1.0.0.SNAPSHOT&#8221;[provided]<br />
  No required bundles</p>
<p>osgi></p>
<p>You can access the handler registered at http://localhost. Open it in a browser and hit refresh a few times, you should see a different message as a random service is picked! Then as you stop/start the complexapp bundles you can expect to see different messages.</p>
<p>Next: Spring Dynamic Modules (SpringDM) implementation for this same demo app</p>
]]></content:encoded>
			<wfw:commentRss>http://www.irahul.com/codepst/?feed=rss2&amp;p=67</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pure Java OSGi demo implementation</title>
		<link>http://www.irahul.com/codepst/?p=33</link>
		<comments>http://www.irahul.com/codepst/?p=33#comments</comments>
		<pubDate>Thu, 24 Sep 2009 16:56:40 +0000</pubDate>
		<dc:creator>rahul</dc:creator>
				<category><![CDATA[OSGi]]></category>
		<category><![CDATA[BundleContext]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[equinox]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.irahul.com/codepst/?p=33</guid>
		<description><![CDATA[To interface with OSGi we need a handle to the BundleContext. OSGi provides for an &#8216;activator&#8217; that is basically the main() method for when a bundle gets started. To create an activator we will implement the BundleActivator interface. The interface has start and stop methods which are invoked at times that their names suggest and [...]]]></description>
			<content:encoded><![CDATA[<p>To interface with OSGi we need a handle to the <code>BundleContext</code>. OSGi provides for an &#8216;activator&#8217; that is basically the main() method for when a bundle gets started. To create an activator we will implement the <code>BundleActivator</code> interface. The interface has start and stop methods which are invoked at times that their names suggest and you can control the behavior in your implementing class. Then in the Maven plugin (in the POM) add this instruction:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">....
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Bundle-Activator<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.irahul.http.handler.Activator<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Bundle-Activator<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
....</pre></div></div>

<p><strong>The shared service:</strong> The interface for the shared service is one simple method</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">....
<span style="color: #003399;">String</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
....</pre></div></div>

<p><strong>Creating a service implementation:</strong> The <code>ComplexService</code> we defined now needs to be implemented and made available as an OSGi service. Here is the Activator:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #003399;">Activator</span> <span style="color: #000000; font-weight: bold;">implements</span> BundleActivator<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> start<span style="color: #009900;">&#40;</span>BundleContext context<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>		
		<span style="color: #666666; font-style: italic;">//register the complex service impl		</span>
		context.<span style="color: #006633;">registerService</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.irahul.shared.ComplexService&quot;</span>,				
				<span style="color: #000000; font-weight: bold;">new</span> ComplexService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
						<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Hello from Version 1!&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>			
				<span style="color: #009900;">&#125;</span>, 
				<span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	.....
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The service is implemented and <em>registered with the bundle context</em>.</p>
<p><strong>Creating the servlet:</strong> This is done by implementing the <code>Servlet</code> interface. The ComplexService <em>does something</em> and the response is sent back to the client. The service to be used is chosen at random in this simple example.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyHandler <span style="color: #000000; font-weight: bold;">implements</span> Servlet <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>ComplexService<span style="color: #339933;">&gt;</span> complexServices <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>ComplexService<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> addComplexService<span style="color: #009900;">&#40;</span>ComplexService service<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>service<span style="color: #339933;">==</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
		complexServices.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>service<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> removeComplexService<span style="color: #009900;">&#40;</span>ComplexService svc<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>svc<span style="color: #339933;">==</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
		complexServices.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>svc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> service<span style="color: #009900;">&#40;</span>ServletRequest request, ServletResponse response<span style="color: #009900;">&#41;</span>
		<span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//we may have discovered more than 1 service</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>complexServices<span style="color: #339933;">==</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">||</span> complexServices.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			response.<span style="color: #006633;">getOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No Service found!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//there are 1...n services, pick one</span>
			<span style="color: #666666; font-style: italic;">//using a random pick</span>
			response.<span style="color: #006633;">getOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>complexServices.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>
					<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span>complexServices.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">doSomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	....
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Creating the http handler:</strong> The Activator for the HTTP Handler must now get a reference to this service and in turn register itself with the HTTP Server. </p>
<p>In addition to the <code>BundleActivator</code> I am also going to implement the <code>ServiceListener</code> interface. This allows us to get event notifications as services start/stop. We want to dynamically support multiple versions of the <code>ComplexService</code>. The Activator looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #003399;">Activator</span> <span style="color: #000000; font-weight: bold;">implements</span> BundleActivator, ServiceListener<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> MyHandler myHandler<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> BundleContext bundleContext<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> start<span style="color: #009900;">&#40;</span>BundleContext context<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//we need this later</span>
		bundleContext <span style="color: #339933;">=</span> context<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//we are interested in service updates</span>
		context.<span style="color: #006633;">addServiceListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//load referenced services		</span>
		<span style="color: #666666; font-style: italic;">//http service to register handler</span>
		ServiceReference svcRef <span style="color: #339933;">=</span> context.<span style="color: #006633;">getServiceReference</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;org.osgi.service.http.HttpService&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		HttpService httpService <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>HttpService<span style="color: #009900;">&#41;</span>context.<span style="color: #006633;">getService</span><span style="color: #009900;">&#40;</span>svcRef<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//create and register http handler (catch all)</span>
		myHandler <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
		httpService.<span style="color: #006633;">registerServlet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span>, myHandler, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//get a list of ComplexService (there may be more than 1)</span>
		ServiceReference<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> complexSvcRefs <span style="color: #339933;">=</span> context.<span style="color: #006633;">getServiceReferences</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.irahul.shared.ComplexService&quot;</span>,<span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>complexSvcRefs<span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>ServiceReference ref<span style="color: #339933;">:</span>complexSvcRefs<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				myHandler.<span style="color: #006633;">addComplexService</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ComplexService<span style="color: #009900;">&#41;</span>context.<span style="color: #006633;">getService</span><span style="color: #009900;">&#40;</span>ref<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http.handler started!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	....
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> serviceChanged<span style="color: #009900;">&#40;</span>ServiceEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//some service has been registred/removed</span>
		<span style="color: #003399;">Object</span> svc<span style="color: #339933;">=</span>bundleContext.<span style="color: #006633;">getService</span><span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">getServiceReference</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>svc <span style="color: #000000; font-weight: bold;">instanceof</span> ComplexService<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">getType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span>ServiceEvent.<span style="color: #006633;">REGISTERED</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;New Service added&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				myHandler.<span style="color: #006633;">addComplexService</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ComplexService<span style="color: #009900;">&#41;</span>svc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Service stopped&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				myHandler.<span style="color: #006633;">removeComplexService</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ComplexService<span style="color: #009900;">&#41;</span>svc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><a href='http://www.irahul.com/codepst/wp-content/uploads/2009/09/java_osgidemo.rar'>Download full source</a>.</p>
<p>Next: Running this in Eclipse Equinox</p>
]]></content:encoded>
			<wfw:commentRss>http://www.irahul.com/codepst/?feed=rss2&amp;p=33</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OSGi Demo app design</title>
		<link>http://www.irahul.com/codepst/?p=29</link>
		<comments>http://www.irahul.com/codepst/?p=29#comments</comments>
		<pubDate>Tue, 22 Sep 2009 18:21:17 +0000</pubDate>
		<dc:creator>rahul</dc:creator>
				<category><![CDATA[OSGi]]></category>
		<category><![CDATA[felix]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[maven-bundle-plugin]]></category>
		<category><![CDATA[pom]]></category>

		<guid isPermaLink="false">http://www.irahul.com/codepst/?p=29</guid>
		<description><![CDATA[The application will consist of the following bundles (what are bundles?):
shared: This bundle exports the service interface for the proposed ComplexService.
http.handler: This bundle creates a HTTP handler that takes in an incoming request, finds the appropriate ComplexService, invokes it and returns the response. The bundle depends on the the service interface exported in the shared [...]]]></description>
			<content:encoded><![CDATA[<p>The application will consist of the following bundles (<a href="http://en.wikipedia.org/wiki/OSGi#Bundles">what are bundles?</a>):</p>
<p><strong>shared</strong>: This bundle exports the service interface for the proposed <code>ComplexService</code>.<br />
<strong>http.handler</strong>: This bundle creates a HTTP handler that takes in an incoming request, finds the appropriate <code>ComplexService</code>, invokes it and returns the response. The bundle depends on the the service interface exported in the <code>shared</code> bundle.<br />
<strong>complexapp</strong>: This bundle registers an implementation of the <code>ComplexService </code>with OSGi.</p>
<p>You also need external bundles that may be downloaded: (<a href="http://www.eclipse.org/equinox/server/http_in_equinox.php">Look at OSGi HTTP Server</a>)<br />
<strong>javax:servlet:2.4</strong> &#8211; Download from Eclipse Orbit &#8211; This provides the Java Servlets necessary to handle HTTP<br />
<strong>org.eclipse.osgi:services:3.1.x</strong> &#8211; Download from Eclipse Equinox<br />
<strong>org.eclipse.osgi:http:1.0.x</strong> &#8211; Download from Eclipse Equinox &#8211; These two provide the HTTP Server services</p>
<p>I will be using the Maven plugin (<a href="http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html">Felix plugin based on BND</a>) to create the bundles I am writing. For example the POM file for shared is defined as (look at source for all):</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;project</span> ....<span style="color: #000000; font-weight: bold;">&gt;</span></span>
  ....
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.irahul<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>shared<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>bundle<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0-SNAPSHOT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>shared<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.felix<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-bundle-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.4.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;extensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/extensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;instructions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Export-Package<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    					com.irahul.shared*,
    					!*
    				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Export-Package<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Private-Package<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    					com.irahul.shared*
    				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Private-Package<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>    				
      			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/instructions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	  		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.maven.plugins<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	  		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-compiler-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	  		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	  		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      ....
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This results in a nicely created bundle with the MANIFEST.MF file like this:<br />
<code><br />
Manifest-Version: 1.0<br />
Export-Package: com.irahul.shared<br />
Private-Package: com.irahul.shared*<br />
Built-By: rahul<br />
Tool: Bnd-0.0.238<br />
Bundle-Name: shared<br />
Created-By: Apache Maven Bundle Plugin<br />
Bundle-Version: 1.0.0.SNAPSHOT<br />
Build-Jdk: 1.6.0_16<br />
Bnd-LastModified: 1253219746453<br />
Bundle-ManifestVersion: 2<br />
Import-Package: com.irahul.shared<br />
Bundle-SymbolicName: com.irahul.shared<br />
</code></p>
<p>Correctly exported and imported packages are a key to resolving your bundles correctly in the OSGi container.</p>
<p>Next: Pure Java implementation</p>
]]></content:encoded>
			<wfw:commentRss>http://www.irahul.com/codepst/?feed=rss2&amp;p=29</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting started with OSGi</title>
		<link>http://www.irahul.com/codepst/?p=14</link>
		<comments>http://www.irahul.com/codepst/?p=14#comments</comments>
		<pubDate>Thu, 17 Sep 2009 22:49:48 +0000</pubDate>
		<dc:creator>rahul</dc:creator>
				<category><![CDATA[OSGi]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.irahul.com/codepst/?p=14</guid>
		<description><![CDATA[This is the first part in a series for a presentation at the Silicon Valley Code Camp at Foothill college. 
For a concise description read through the official OSGi Architecure page.
I&#8217;ll present a simple application that shows how to dynamically provide an application upgrade with zero downtime and no JVM restart. It will load a [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first part in a series for a presentation at the <a href="http://www.siliconvalley-codecamp.com">Silicon Valley Code Camp</a> at Foothill college. </p>
<p>For a concise description read through the official <a href="http://www.osgi.org/About/WhatIsOSGi ">OSGi Architecure</a> page.</p>
<p>I&#8217;ll present a simple application that shows how to dynamically provide an application upgrade with zero downtime and no JVM restart. It will load a <a href="http://www.eclipse.org/equinox/server/http_in_equinox.php">HTTP Server</a> and then register a handler with it. This handler then processes the request using a <code>ComplexService</code>. Multiple bundles of this ComplexService implementation are possible (suppose V1 and V2). We can start the OSGi container with 0 or more of these and let the handler decide which one to use. If we start with suppose V1 then we can load V2 and stop V1, creating an update without a JVM restart. </p>
<p>The following sequence diagram shows the events.</p>
<div id="attachment_24" class="wp-caption alignnone" style="width: 644px"><img src="http://www.irahul.com/codepst/wp-content/uploads/2009/09/seq-osgidemo1.jpg" alt="Sequence diagram for OSGi example" title="Sequence diagram for OSGi example" width="634" height="266" class="size-full wp-image-24" /><p class="wp-caption-text">Sequence diagram for OSGi example</p></div>
<p>With this simple set of bundles (less than 200 lines of code in all!) I can show the following:</p>
<p><strong>Run multiple versions of your app simultaneously</strong>: If your application is designed with this mind, you can continue to run singletons of the http server and your data access layer, however you have multiple versions of your business logic which may be used dynamically.</p>
<p><strong>Dynamic routing</strong>: Expanding on the point above you can build a smart routing mechanism to invoke the different versions of the services that are available.</p>
<p><strong>Install new bundles to upgrade features</strong>: You may create new services and register them with the http server to provide new features all without a JVM restart.</p>
<p><strong>Upgrade application version</strong>: You may install a new version of any service and shutdown and remove a previous version and have existing http services use this dynamically. Again no JVM restart.</p>
<p>Note on versioning: In OSGi it is possible to version packages exported/imported by bundles. For the purpose of this exercise I&#8217;ll keep things simple and not use it.</p>
<p><a href='http://www.irahul.com/codepst/wp-content/uploads/2009/09/Getting-started-with-OSGi.ppt'>Download presentation: Getting started with OSGi</a></p>
<p>Next: Application design for this demo</p>
]]></content:encoded>
			<wfw:commentRss>http://www.irahul.com/codepst/?feed=rss2&amp;p=14</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Injecting beans into Quartz job</title>
		<link>http://www.irahul.com/codepst/?p=12</link>
		<comments>http://www.irahul.com/codepst/?p=12#comments</comments>
		<pubDate>Fri, 29 May 2009 02:52:47 +0000</pubDate>
		<dc:creator>rahul</dc:creator>
				<category><![CDATA[OSGi]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[quartz]]></category>

		<guid isPermaLink="false">http://www.irahul.com/codepst/?p=12</guid>
		<description><![CDATA[Finally as promised the revised and more efficient way to inject beans directly into your quartz jobs. Simply use the jobDataAsMap property.

&#60;bean name=&#34;jobBean&#34; class=&#34;org.springframework.scheduling.quartz.JobDetailBean&#34;&#62;
		&#60;property name=&#34;jobClass&#34; value=&#34;com.irahul.example.SpringQuartzJob&#34; /&#62;	  
	  	&#60;property name=&#34;name&#34; value=&#34;jobName&#34; /&#62;
	  	&#60;property name=&#34;jobDataAsMap&#34;&#62;
			&#60;map&#62;
				&#60;entry key=&#34;setterName&#34; value-ref=&#34;beanRef&#34; /&#62;				
                [...]]]></description>
			<content:encoded><![CDATA[<p>Finally as promised the revised and more efficient way to inject beans directly into your quartz jobs. Simply use the jobDataAsMap property.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jobBean&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.scheduling.quartz.JobDetailBean&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jobClass&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;com.irahul.example.SpringQuartzJob&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>	  
	  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;name&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;jobName&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jobDataAsMap&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;setterName&quot;</span> <span style="color: #000066;">value-ref</span>=<span style="color: #ff0000;">&quot;beanRef&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>				
                                .....
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.irahul.com/codepst/?feed=rss2&amp;p=12</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Accessing Spring beans from Quartz jobs</title>
		<link>http://www.irahul.com/codepst/?p=10</link>
		<comments>http://www.irahul.com/codepst/?p=10#comments</comments>
		<pubDate>Sun, 04 Nov 2007 03:52:00 +0000</pubDate>
		<dc:creator>rahul</dc:creator>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[quartz]]></category>

		<guid isPermaLink="false">http://www.irahul.com/codepst/?p=10</guid>
		<description><![CDATA[I had referred to Mark&#8217;s blog in my earlier post and actually now have a improvement over his solution. Define your context as usual (courtesy to Mark for his example)

&#60;beans&#62;
  &#60;!-- Define the Job Bean that will be executed. 
  Our bean is named in the jobClass property. --&#62;
  &#60;bean name=&#34;myJob&#34; class=&#34;org.springframework.scheduling.quartz.JobDetailBean&#34;&#62;
 [...]]]></description>
			<content:encoded><![CDATA[<p>I had referred to Mark&#8217;s blog in my earlier post and actually now have a improvement over his solution. Define your context as usual (courtesy to Mark for his example)</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #808080; font-style: italic;">&lt;!-- Define the Job Bean that will be executed. </span>
<span style="color: #808080; font-style: italic;">  Our bean is named in the jobClass property. --&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;myJob&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.scheduling.quartz.JobDetailBean&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jobClass&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;com.gsoftware.common.util.MyJob&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #808080; font-style: italic;">&lt;!-- Associate the Job Bean with a Trigger. </span>
<span style="color: #808080; font-style: italic;">  Triggers define when a job is executed. --&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;simpleTrigger&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.scheduling.quartz.SimpleTriggerBean&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- see the example of method invoking job above --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jobDetail&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;myJob&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;startDelay&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;2000&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;repeatInterval&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;10000&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #808080; font-style: italic;">&lt;!-- A list of Triggers to be scheduled and executed by Quartz --&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.scheduling.quartz.SchedulerFactoryBean&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;triggers&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">&quot;simpleTrigger&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;applicationContextSchedulerContextKey&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;applicationContext&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Note that in the factory I have added the property for the application context.<br />
Now all you need to do in your job is <code>extend</code> the spring base class <code>QuartzJobBean</code> for a job and create a setter as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyJob <span style="color: #000000; font-weight: bold;">extends</span> QuartzJobBean <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> ApplicationContext applicationContext<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setApplicationContext<span style="color: #009900;">&#40;</span>ApplicationContext appContext<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    applicationContext <span style="color: #339933;">=</span> appContext<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> executeInternal<span style="color: #009900;">&#40;</span>JobExecutionContext executionContext<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//your code...</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now you have the application context available in your job. Nothing else required!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.irahul.com/codepst/?feed=rss2&amp;p=10</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>OSGi &#8211; Spring Quartz job</title>
		<link>http://www.irahul.com/codepst/?p=9</link>
		<comments>http://www.irahul.com/codepst/?p=9#comments</comments>
		<pubDate>Wed, 31 Oct 2007 03:48:07 +0000</pubDate>
		<dc:creator>rahul</dc:creator>
				<category><![CDATA[OSGi]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[application context]]></category>
		<category><![CDATA[bundle context]]></category>
		<category><![CDATA[quartz]]></category>

		<guid isPermaLink="false">http://www.irahul.com/codepst/?p=9</guid>
		<description><![CDATA[Mark&#8217;s blog provides a good example of how to provide the Spring application context to a Job. Here I will show how to &#8217;inject&#8217; OSGi services into your Spring OSGi application. This will become much cleaner once Spring Dynamic Modules executor bundle is released but at the moment using Spring 2.0.7 I need to provide some OSGi services [...]]]></description>
			<content:encoded><![CDATA[<p><a target="_blank" href="http://cse-mjmcl.cse.bris.ac.uk/blog/2007/06/20/1182370280435.html">Mark&#8217;s blog</a> provides a good example of how to provide the Spring application context to a Job. Here I will show how to &#8217;inject&#8217; OSGi services into your Spring OSGi application. This will become much cleaner once Spring Dynamic Modules executor bundle is released but at the moment using Spring 2.0.7 I need to provide some OSGi services to my Quartz job.</p>
<p>I have say a bundle &#8216;MyServiceBundle&#8217; that registers a service with the following interface:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.irahul.example.myservicebundle</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> TransactionService<span style="color: #009900;">&#123;</span> 
    <span style="color: #000066; font-weight: bold;">void</span> updateTransaction<span style="color: #009900;">&#40;</span><span style="color: #003399;">Long</span> id, <span style="color: #003399;">String</span> status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The activator looks something like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.irahul.example.myservicebundle</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #003399;">Activator</span> <span style="color: #000000; font-weight: bold;">implements</span> BundleActivator <span style="color: #009900;">&#123;</span> 
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> start<span style="color: #009900;">&#40;</span>BundleContext bundleContext<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
       TransactionService txnSvc <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ....<span style="color: #666666; font-style: italic;">//an implementation of it </span>
       Map<span style="color: #339933;">&lt;</span>String,String<span style="color: #339933;">&gt;</span> metaProperties <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span>... 
       <span style="color: #666666; font-style: italic;">//define whatever meta data you want to register for service discovery </span>
       bundleContext.<span style="color: #006633;">registerService</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.irahul.example.TransactionService&quot;</span>, 
          txnSvc,metaProperties<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now over in the bundle &#8216;MyQuartzJob&#8217; define a JobContext which can be injected with all the beans it requires to perform the Job.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.irahul.example.myquartzjob</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> JobContext<span style="color: #009900;">&#123;</span> 
    <span style="color: #000066; font-weight: bold;">void</span> setTransactionService<span style="color: #009900;">&#40;</span>TransactionService txnSvc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    TransactionService getTransactionService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Implement the QuartzJobBean to get the application context like in Mark&#8217;s blog. Now in your application context create an empty bean with your JobContext implementation.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- Job Context --&gt;</span> 
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;jobContext&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.irahul.example.myquartzjob.JobContextImpl&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>After that all that remains is the activator, which looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.irahul.example.myquartzjob</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #003399;">Activator</span> <span style="color: #000000; font-weight: bold;">implements</span> BundleActivator <span style="color: #009900;">&#123;</span> 
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> start<span style="color: #009900;">&#40;</span>BundleContext bundleContext<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
    <span style="color: #666666; font-style: italic;">// Initialize the Spring application context </span>
    ApplicationContext applicationContext <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ClassPathXmlApplicationContext<span style="color: #009900;">&#40;</span> 
         <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;context.xml&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #666666; font-style: italic;">//get the service </span>
    ServiceReference txnRef <span style="color: #339933;">=</span> bundleContext. 
               <span style="color: #006633;">getServiceReference</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.irahul.example.TransactionService&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
               <span style="color: #666666; font-style: italic;">//you can search on additonal meta data filter as well </span>
    TransactionService txnService <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TransactionService<span style="color: #009900;">&#41;</span>bundleContext.<span style="color: #006633;">getService</span><span style="color: #009900;">&#40;</span>txnRef<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #666666; font-style: italic;">//now get the spring bean as setup </span>
    JobContext jobContext <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>JobContext<span style="color: #009900;">&#41;</span>applicationContext.<span style="color: #006633;">getBean</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jobContext&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    jobContext.<span style="color: #006633;">setTransactionService</span><span style="color: #009900;">&#40;</span>txnService<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Your Job now has the application context all setup and ready to go!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.irahul.com/codepst/?feed=rss2&amp;p=9</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Facebook demo app &#8211; America Votes</title>
		<link>http://www.irahul.com/codepst/?p=5</link>
		<comments>http://www.irahul.com/codepst/?p=5#comments</comments>
		<pubDate>Mon, 29 Oct 2007 05:12:30 +0000</pubDate>
		<dc:creator>rahul</dc:creator>
				<category><![CDATA[Facebook]]></category>
		<category><![CDATA[facebook platform example]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.irahul.com/codepst/?p=5</guid>
		<description><![CDATA[So here is the complete source for my version of a hello world facebook app. It starts off from their footprints example that comes with the facebook client. This is a simple app where you vote for your pick of the 2008 president from the huge list of candidates at the moment! It then shows [...]]]></description>
			<content:encoded><![CDATA[<p>So here is the complete source for my version of a hello world facebook app. It starts off from their footprints example that comes with the facebook client. This is a simple app where you vote for your pick of the 2008 president from the huge list of candidates at the moment! It then shows a tally of the top candidates and allows you to invite your friends.</p>
<p>You can see it in action at <a href="http://apps.facebook.com/americavotes">http://apps.facebook.com/americavot</a><a href="http://apps.facebook.com/americavotes">es</a></p>
<p><a rel="attachment wp-att-7" href="http://www.irahul.com/codepst/?attachment_id=7" title="America Votes Screenshot"><img src="http://www.irahul.com/codepst/wp-content/uploads/2007/10/sshot.jpg" alt="America Votes Screenshot" /></a></p>
<p>If you look at the source you will see it is pretty straight forward and I&#8217;ll try and post some snippets with details if necessary. A few pieces of code are copied from some public internet sources.</p>
<p>Download: <a href="http://www.irahul.com/codepst/wp-content/uploads/2007/10/america.zip" title="America Votes Source">America Votes Source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.irahul.com/codepst/?feed=rss2&amp;p=5</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Welcome</title>
		<link>http://www.irahul.com/codepst/?p=4</link>
		<comments>http://www.irahul.com/codepst/?p=4#comments</comments>
		<pubDate>Fri, 26 Oct 2007 02:55:25 +0000</pubDate>
		<dc:creator>rahul</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.irahul.com/wordpress/?p=4</guid>
		<description><![CDATA[Finally I&#8217;ve installed a blog! I&#8217;m no blogger so hopefully a couple posts per year should be good  
]]></description>
			<content:encoded><![CDATA[<p>Finally I&#8217;ve installed a blog! I&#8217;m no blogger so hopefully a couple posts per year should be good <img src='http://www.irahul.com/codepst/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.irahul.com/codepst/?feed=rss2&amp;p=4</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
