<?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>Luminis Software Development &#187; pax</title>
	<atom:link href="http://lsd.luminis.eu/en/tag/pax/feed/" rel="self" type="application/rss+xml" />
	<link>http://lsd.luminis.eu</link>
	<description></description>
	<lastBuildDate>Wed, 28 Dec 2011 20:44:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using GWT to create an OSGi-aware web application</title>
		<link>http://lsd.luminis.eu/en/using-gwt-to-create-an-osgi-aware-web-application/</link>
		<comments>http://lsd.luminis.eu/en/using-gwt-to-create-an-osgi-aware-web-application/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 20:14:29 +0000</pubDate>
		<dc:creator>Angelo van der Sijpt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Apache ACE]]></category>
		<category><![CDATA[Apache Felix]]></category>
		<category><![CDATA[extender]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[gwt]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[OSGi]]></category>
		<category><![CDATA[pax]]></category>
		<category><![CDATA[service]]></category>
		<category><![CDATA[smartservices]]></category>
		<category><![CDATA[toolkit]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://lsd.luminis.net/?p=217</guid>
		<description><![CDATA[<p><a href="http://code.google.com/webtoolkit/">Google Web Toolkit</a> is cool, and so is OSGi. However, creating a web application that can use OSGi services is not that easy. By the end of this tutorial, you will have created a GWT project that delivers a usable jar. If you're impatient, skip to the end for the downloadable Eclipse project.</p>]]></description>
			<content:encoded><![CDATA[<p><strong>Update 2010-02-20</strong> <em>Both Pax Runner 1.3.0 and GWT 2.0 have caused quite some changes to this post. I have tried to stay up to date as well as I could (the zipped project now uses GWT 2.0), but you might find some inconsistencies when following the tutorial.</em></p>
<p><a href="http://code.google.com/webtoolkit/">Google Web Toolkit</a> is cool, and so is OSGi. However, when building a web UI for <a href="http://cwiki.apache.org/confluence/display/ACE/Index">Apache ACE</a>, I found out that creating a web application that can use OSGi services is not that easy. By the end of this tutorial, you will have created a GWT project that delivers a usable jar. If you&#8217;re impatient, skip to the end for the downloadable Eclipse project.</p>
<h3>Step 1: Create a GWT project</h3>
<p>Create a regular GWT project using the regular webAppCreator; this will give you a project that includes an <a href="http://ant.apache.org">Ant</a> buildfile, we will need that later on.</p>

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">angelos:workspace angelos$ ./gwt-mac-1.6.4/webAppCreator -out GwtDemo net.luminis.gwt.gwtdemo
Created directory GwtDemo/src
Created directory GwtDemo/war
Created directory GwtDemo/war/WEB-INF
Created directory GwtDemo/war/WEB-INF/lib
Created directory GwtDemo/src/net/luminis/gwt
Created directory GwtDemo/src/net/luminis/gwt/client
Created directory GwtDemo/src/net/luminis/gwt/server
Created file GwtDemo/src/net/luminis/gwt/gwtdemo.gwt.xml
Created file GwtDemo/war/gwtdemo.html
Created file GwtDemo/war/gwtdemo.css
Created file GwtDemo/war/WEB-INF/web.xml
Created file GwtDemo/src/net/luminis/gwt/client/gwtdemo.java
Created file GwtDemo/src/net/luminis/gwt/client/GreetingService.java
Created file GwtDemo/src/net/luminis/gwt/client/GreetingServiceAsync.java
Created file GwtDemo/src/net/luminis/gwt/server/GreetingServiceImpl.java
Created file GwtDemo/build.xml
Created file GwtDemo/README.txt
Created file GwtDemo/.project
Created file GwtDemo/.classpath
Created file GwtDemo/gwtdemo.launch
Created file GwtDemo/war/WEB-INF/lib/gwt-servlet.jar</pre></div></div>

<p>If you want to, you can import this project directly into your Eclipse. If you check the mark &#8220;use Google Web Toolkit&#8221; in the project properties, you can use all the same goodies that creating the project in Eclipse would have given you. Remember to replace the buildpath entries for gwt-user.jar and gwt-dev-*.jar by a Library import for GWT.</p>
<h3>Step 2: Include the necessary OSGi references</h3>
<p>Create an &#8216;ext&#8217; directory, and add org.osgi.core.jar to that. In Eclipse, add this jar to your build path.</p>
<h3>Step 3: Use OSGi services from your web applicaiton</h3>
<p>We will first add a simple Activator on the server side.</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;">net.luminis.gwt.server</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.framework.BundleActivator</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.framework.BundleContext</span><span style="color: #339933;">;</span>
&nbsp;
<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;">private</span> <span style="color: #000000; font-weight: bold;">static</span> BundleContext m_context<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> BundleContext getContext<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> m_context<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> 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>
        m_context <span style="color: #339933;">=</span> context<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> stop<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: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then, we up the GreetingServiceImpl to actually use this <a href="http://www.osgi.org/javadoc/r4v41/org/osgi/framework/BundleContext.html">BundleContext</a> (note that we use it directly here, but you could use it to get other services, create a <a href="http://www.osgi.org/javadoc/r4v41/org/osgi/util/tracker/ServiceTracker.html">ServiceTracker</a>, etc.)</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: #003399;">String</span> greetServer<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">String</span> serverInfo <span style="color: #339933;">=</span> getServletContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getServerInfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003399;">String</span> userAgent <span style="color: #339933;">=</span> getThreadLocalRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;User-Agent&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Hello, &quot;</span> <span style="color: #339933;">+</span> input <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;!
&nbsp;
I am running &quot;</span> <span style="color: #339933;">+</span> serverInfo
    <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;.
&nbsp;
It looks like you are using:&quot;</span> <span style="color: #339933;">+</span> userAgent <span style="color: #339933;">+</span>
    <span style="color: #0000ff;">&quot;The framework we run from has &quot;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">Activator</span>.<span style="color: #006633;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getBundles</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">length</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; bundles in it.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Step 4: Add OSGi dependencies for the compiler</h3>
<p>Add our OSGi dependencies to the classpath, so the compiler can find all of it.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #808080; font-style: italic;">&lt;!-- Add any additional non-server libs (such as JUnit) --&gt;</span></pre></div></div>

<p>Right, let&#8217;s give it a try!</p>

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">angelos:GwtDemo angelos$ ant war
Buildfile: build.xml
&nbsp;
...some output removed...
&nbsp;
war:
[zip] Building zip: /Users/angelos/workspace/workspace/GwtDemo/gwtdemo.war
&nbsp;
BUILD SUCCESSFUL
Total time: 36 seconds</pre></div></div>

<p>You will find a war in your project directory now. There still is one ingredient we need. We need to make this into a proper bundle. We can use <a href="http://www.aqute.biz/Code/Bnd">bnd</a> to help us with that.</p>
<h3>Step 5: use bnd to create a proper war</h3>
<p>Download bnd, and put into a lib directory, and add it to your buildfile.</p>
<p>We create a new target that transforms our war into a jar.</p>

<div class="wp_syntax"><div class="code"><pre class="ant" style="font-family:monospace;">&lt;target name=&quot;jar&quot;&gt;
    &lt;copy file=&quot;gwtdemo.war&quot; tofile=&quot;gwtdemo.jar&quot;/&gt;
    &lt;echo file=&quot;gwtdemo.bnd&quot;&gt;Import-Package: junit.framework;resolution:=optional, com.google.gwt.*;resolution:=optional, org.w3c.*;resolution:=optional, sun.misc;resolution:=optional, javax.imageio;resolution:=optional, javax.servlet.*;resolution:=optional, *
Bundle-Name: GWT Demo
Bundle-ClassPath: WEB-INF/classes, WEB-INF/lib/gwt-servlet.jar
Bundle-SymbolicName: net.luminis.gwt.gwtdemo
Webapp-Context: gwtdemo
Bundle-Activator: net.luminis.gwt.server.Activator
    &lt;/echo&gt;
    &lt;bndwrap jars=&quot;gwtdemo.jar&quot; output=&quot;gwtdemo.jar&quot;/&gt;
    &lt;jar file=&quot;gwtdemo.jar&quot; update=&quot;true&quot;&gt;
    &lt;manifest&gt;
        &lt;attribute name=&quot;Bundle-ClassPath&quot; value=&quot;WEB-INF/classes, WEB-INF/lib/gwt-servlet.jar, .&quot;/&gt;
     &lt;/manifest&gt;
    &lt;/jar&gt;
    &lt;delete file=&quot;gwtdemo.bnd&quot;/&gt;
&lt;/target&gt;</pre></div></div>

<p>What&#8217;s happening here?</p>
<ul>
<li>we copy our war to the same file, but with a jar extension,</li>
<li>we create a file for bnd to use, stating that we
<ul>
<li>want optional imports for junit and the gwt benchmarks, and non-optional imports for everything else (that what the * is for),</li>
<li>have some classes that we want bnd to scan for finding dependencies,</li>
<li>want to use a given Webapp-Context (this is a <a href="http://paxrunner.ops4j.org/display/paxweb/WAR+Extender">Pax war extender</a> specific entry),</li>
</ul>
</li>
<li>let bnd do its magic,</li>
<li>update our manifest: we put the . back on the classpath. This is important for the web application to find all resources, but if we would tell bnd to do it like this, it would treat . as the root of the classpath.</li>
<li>Finally, we delete that temporary bnd file.</li>
</ul>
<p>What does that give us?</p>

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">angelos:GwtDemo angelos$ ant jar
Buildfile: build.xml
&nbsp;
...some output removed...
&nbsp;
jar:
[copy] Copying 1 file to /Users/angelos/workspace/workspace/GwtDemo
[bndwrap] gwtdemo 41 910305
[bndwrap] Warnings
[bndwrap] Superfluous export-package instructions: [WEB-INF.classes.net, gwtdemo.gwt.standard.images, WEB-INF, gwtdemo, WEB-INF.classes.net.luminis.gwt, gwtdemo.gwt.standard, WEB-INF.classes.net.luminis, WEB-INF.lib, WEB-INF.classes, gwtdemo.gwt.standard.images.ie6, WEB-INF.classes.net.luminis.gwt.client, WEB-INF.classes.net.luminis.gwt.server, gwtdemo.gwt]
[jar] Updating jar: /Users/angelos/workspace/workspace/GwtDemo/gwtdemo.jar
[delete] Deleting: /Users/angelos/workspace/workspace/GwtDemo/gwtdemo.bnd
&nbsp;
BUILD SUCCESSFUL
Total time: 23 seconds</pre></div></div>

<p>That&#8217;s it! You can now deploy this jar into a framework that uses the pax web tools. Right, let&#8217;s give that a try.</p>
<p>Download <a href="http://paxrunner.ops4j.org/space/Pax+Runner">pax-runner</a>, and unzip that somewhere. Copy in your new jar, an try the following command</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">angelos:pax-runner angelos$ <span style="color: #c20cb9; font-weight: bold;">sh</span> bin<span style="color: #000000; font-weight: bold;">/</span>pax-run.sh <span style="color: #660033;">--profiles</span>=war,compendium gwtdemo.jar</pre></div></div>

<p>Now visit <a href="http://localhost:8080/gwtdemo">http://localhost:8080/gwtdemo</a>:</p>
<p><img class="alignnone size-full wp-image-450" title="gwtdemo" src="http://lsd.luminis.nl/wp-content/uploads/2009/07/gwtdemo.png" alt="gwtdemo" width="500" height="432" /></p>
<h3>Summary</h3>
<p>So, what did we need?</p>
<ul>
<li>A fairly regular GWT project, create with an Ant file,</li>
<li>some code that tries to use OSGi services,</li>
<li>some bnd magic to make the war into a jar,</li>
<li>Pax tools to get it all running quickly.</li>
</ul>
<p>If you don&#8217;t want to use pax runner, you can need to deploy <a href="http://paxrunner.ops4j.org/display/paxweb/WAR+Extender">pax-web-extender-war</a>(<a href="http://repository.ops4j.org/mvn-snapshots/org/ops4j/pax/web/pax-web-extender-war/0.7.0-SNAPSHOT/pax-web-extender-war-0.7.0-20090623.160836-13.jar">jar</a>, snapshot 23 June) and an http server, preferably <a href="http://paxrunner.ops4j.org/display/paxweb/Pax+Web">pax-web-service</a>(<a href="http://repository.ops4j.org/maven2/org/ops4j/pax/web/pax-web-service/0.6.0/pax-web-service-0.6.0.jar">jar</a>), into your framework.</p>
<p>You can download the <a href='http://lsd.luminis.nl/wp-content/uploads/2009/07/gwtdemo.zip'>gwtdemo</a> Eclipse project to play around with it. I have not provided the GWT runtime in this download; you should edit line 4 of the build.xml to point to your installation of GWT.</p>
]]></content:encoded>
			<wfw:commentRss>http://lsd.luminis.eu/en/using-gwt-to-create-an-osgi-aware-web-application/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

