blob: f7e97d9dd8e7ae962b98dd4c3e45d760722ddf6e [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head>
<title>Apache Felix - iPOJO in 10 minutes</title>
<link rel="stylesheet" href="ipojo-in-10-minutes_files/site.css" type="text/css" media="all">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head><body>
<div class="title"><div class="logo"><a href="http://felix.apache.org/site/index.html"><img alt="Apache Felix" src="ipojo-in-10-minutes_files/logo.png" border="0"></a></div><div class="header"><a href="http://www.apache.org/"><img alt="Apache" src="ipojo-in-10-minutes_files/apache.png" border="0"></a></div></div>
<div class="menu">
<ul>
<li><a href="http://felix.apache.org/site/news.html" title="news">news</a></li>
<li><a href="http://felix.apache.org/site/license.html" title="license">license</a></li>
<li><span class="nobr"><a href="http://felix.apache.org/site/downloads.cgi" title="Visit page outside Confluence" rel="nofollow">downloads<sup><img class="rendericon" src="ipojo-in-10-minutes_files/linkext7.gif" alt="" align="absmiddle" border="0" width="7" height="7"></sup></a></span></li>
<li><a href="http://felix.apache.org/site/documentation.html" title="documentation">documentation</a></li>
<li><a href="http://felix.apache.org/site/mailinglists.html" title="mailinglists">mailing lists</a></li>
<li><a href="http://felix.apache.org/site/contributing.html" title="Contributing">contributing</a></li>
<li><span class="nobr"><a href="http://www.apache.org/" title="Visit page outside Confluence" rel="nofollow">asf<sup><img class="rendericon" src="ipojo-in-10-minutes_files/linkext7.gif" alt="" align="absmiddle" border="0" width="7" height="7"></sup></a></span></li>
<li><span class="nobr"><a href="http://www.apache.org/foundation/sponsorship.html" title="Visit page outside Confluence" rel="nofollow">sponsorship<sup><img class="rendericon" src="ipojo-in-10-minutes_files/linkext7.gif" alt="" align="absmiddle" border="0" width="7" height="7"></sup></a></span></li>
<li><span class="nobr"><a href="http://www.apache.org/foundation/thanks.html" title="Visit page outside Confluence" rel="nofollow">sponsors<sup><img class="rendericon" src="ipojo-in-10-minutes_files/linkext7.gif" alt="" align="absmiddle" border="0" width="7" height="7"></sup></a></span>
<!-- ApacheCon Ad -->
<iframe src="ipojo-in-10-minutes_files/button.html" style="border-width: 0pt; float: left;" scrolling="no" width="135" frameborder="0" height="135"></iframe>
<p style="height: 100px;">
<!-- ApacheCon Ad -->
</p></li></ul> </div>
<div class="main">
<table class="sectionMacro" border="0" cellpadding="5" cellspacing="0" width="100%"><tbody><tr>
<td class="confluenceTd" valign="top" width="80%">
<h1><a name="iPOJOin10minutes-iPOJOin10minutes"></a>iPOJO in 10 minutes</h1>
<p><em>This page presents how to use the</em> <em>iPOJO</em> <em>runtime
and its associated service component model. The concepts of the service
component model are introduced, followed by a simple example that
demonstrates the features of iPOJO.</em></p>
<h2><a name="iPOJOin10minutes-Introduction"></a>Introduction</h2>
<p>iPOJO aims to simplify service-oriented programming on OSGi frameworks; the name iPOJO is an abbreviation for <em>injected POJO</em>.
iPOJO provides a new way to develop OSGi service components with the
main goal being to simplify service component implementation by
transparently managing the dynamics of the environment as well as other
non-functional requirements. The iPOJO framework allows developers to
more clearly separate functional code (i.e., the POJO) from the
non-functional code (i.e., dependency management, service provision,
configuration, etc.). iPOJO combines the functional and non-functional
aspects at run time. To achieve this, iPOJO provides a simple and
extensible service component model based on POJOs.</p>
<h2><a name="iPOJOin10minutes-ThePOJOconcept"></a>The POJO concept</h2>
<p>POJO is an acronym for Plain Old Java Object, but it embodies a
concept that the simpler and less intrusive the design of a given
framework, the better. The name is used to emphasize that a given
object is not somehow special, but is an ordinary Java Object. Martin
Fowler, Rebecca Parsons and Josh Mackenzie coined the term POJO in
September 2000: "We wondered why people were so against using regular
objects in their systems and concluded that it was because simple
objects lacked a fancy name. So we gave them one, and it's caught on
very nicely." From the developer's perspective, the iPOJO framework
strives to only require POJOs in as much as it is possible.</p>
<h2><a name="iPOJOin10minutes-iPOJOservicecomponentoverview"></a>iPOJO service component overview</h2>
<p>A service component is able to provide and/or require services,
where a service is an object that implements a given service interface
embodied as a Java interface. In addition, iPOJO introduces a callback
concept to notify a component about various state changes.</p>
<p>The component is the central concept in iPOJO. In the core iPOJO
model, a component describes service dependencies, provided services,
and callbacks; this information is recorded in the component's
metadata. Then, the second important concept in iPOJO is component
instances. A component instances is a special <em>version</em> of the
component. By merging component metadata and instance configuration,
the iPOJO runtime is able to manage the component, i.e., manage its
life cycle, inject required services, publish provided services,
discover needed services.</p>
<h2><a name="iPOJOin10minutes-Asimpleexample"></a>A simple example</h2>
<p>In this tutorial we will present how to:</p>
<ul>
<li>Publish an OSGi service</li>
<li>Require an OSGi service</li>
<li>Use lifecycle callbacks to activate and deactivate components</li>
</ul>
<h3><a name="iPOJOin10minutes-PresentationoftheSpellapplication"></a>Presentation of the <em>Spell</em> application</h3>
<p>To illustrate iPOJO features, we will implement a very simple application. This application is composed by three components:</p>
<ul>
<li>A component implementing a dictionary service</li>
<li>A component requiring the dictionary service and providing a spellchecker service</li>
<li>A component requiring the spellchecker and providing an user interface</li>
</ul>
<p><img src="ipojo-in-10-minutes_files/spell.png" align="absmiddle" border="0"></p>
<h3><a name="iPOJOin10minutes-Preparingthetutorial"></a>Preparing the tutorial</h3>
<p>This tutorial is based on Ant. So, you need to have the Ant program accessible in your path (see <span class="nobr"><a href="http://ant.apache.org/" title="Visit page outside Confluence" rel="nofollow">here<sup><img class="rendericon" src="ipojo-in-10-minutes_files/linkext7.gif" alt="" align="absmiddle" border="0" width="7" height="7"></sup></a></span> to download and install Ant). Download the tutorial archive available <span class="nobr"><a href="http://people.apache.org/%7Eclement/ipojo/tutorials/10min/tutorial.zip" title="Visit page outside Confluence" rel="nofollow">here<sup><img class="rendericon" src="ipojo-in-10-minutes_files/linkext7.gif" alt="" align="absmiddle" border="0" width="7" height="7"></sup></a></span> and then unzip it. The archive is composed by seven directories:</p>
<ul>
<li>spell.services contains service interfaces used by the applications</li>
<li>spell.english contains an implementation of the Dictionary service (containing English words)</li>
<li>spell.checker
contains an implementation of a Spell Checker. The spell checker
requires a dictionary service and check if an input passage is correct
(according to the words contained in the dictionary).</li>
<li>spell.gui
contains a very simple user interface. This component uses a spell
checker service. Then the user can interact with the spell checker with
this user interface.</li>
<li>The task directory contains Ant tasks used to build the project</li>
<li>The solution directory contains an already developed version of the application.</li>
<li>Finally, the felix folder contains a configured version of the Felix runtime</li>
</ul>
<h3><a name="iPOJOin10minutes-Thespell.servicesproject"></a>The spell.services project</h3>
<p>The spell.services project contains only service interfaces. It is not an iPOJO powered bundle.</p>
<p>Go inside the Spell.services directory and create the file "src/spell/services/DictionaryService.java" for the following <em>Dictionary</em> service interface:</p>
<div class="code"><div class="codeContent">
<pre class="code-java"><span class="code-keyword">package</span> spell.services;
/**
* A simple service <span class="code-keyword">interface</span> that defines a dictionary service.
* A dictionary service simply verifies the existence of a word.
**/
<span class="code-keyword">public</span> <span class="code-keyword">interface</span> DictionaryService {
/**
* Check <span class="code-keyword">for</span> the existence of a word.
* @param word the word to be checked.
* @<span class="code-keyword">return</span> <span class="code-keyword">true</span> <span class="code-keyword">if</span> the word is in the dictionary,
* <span class="code-keyword">false</span> otherwise.
**/
<span class="code-keyword">public</span> <span class="code-object">boolean</span> checkWord(<span class="code-object">String</span> word);
}</pre>
</div></div>
<p>Then, create the file "src/spell/services/SpellChecker.java" for the following <em>Spell Checker</em> service interface:</p>
<div class="code"><div class="codeContent">
<pre class="code-java"><span class="code-keyword">package</span> spell.services;
/**
* A simple service <span class="code-keyword">interface</span> that defines a spell checker service.
* A spell checker service checks the spelling of all words in a
* given passage. A passage is any number of words separated by
* a space character and the following punctuation marks: comma,
* period, exclamation mark, question mark, semi-colon, and colon.
**/
<span class="code-keyword">public</span> <span class="code-keyword">interface</span> SpellChecker {
/**
* Checks a given passage <span class="code-keyword">for</span> spelling errors. A passage is any
* number of words separated by a space and any of the following
* punctuation marks: comma (,), period (.), exclamation mark (!),
* question mark (?), semi-colon (;), and colon(:).
* @param passage the passage to spell check.
* @<span class="code-keyword">return</span> An array of misspelled words or <span class="code-keyword">null</span> <span class="code-keyword">if</span> no
* words are misspelled.
**/
<span class="code-keyword">public</span> <span class="code-object">String</span>[] check(<span class="code-object">String</span> passage);
}</pre>
</div></div>
<p>Once created, you can build the project by launching Ant from the project directory.</p>
<div class="code"><div class="codeContent">
<pre class="code-java">$ ant
Buildfile: build.xml
clean:
compile:
[mkdir] Created dir: d:\clement\workspaces\sandbox\ipojo\examples\tutorial-ant\
spell.services\output
[mkdir] Created dir: d:\clement\workspaces\sandbox\ipojo\examples\tutorial-ant\
spell.services\output\classes
[javac] Compiling 2 source files to d:\clement\workspaces\sandbox\ipojo\examples\
tutorial-ant\spell.services\output\classes
<span class="code-keyword">package</span>:
[bnd] spell.services 2
BUILD SUCCESSFUL
Total time: 0 seconds</pre>
</div></div>
<p>The created bundle is inside the output directory (spell.services.jar). The build process use <span class="nobr"><a href="http://www.aqute.biz/Code/Bnd" title="Visit page outside Confluence" rel="nofollow">BND<sup><img class="rendericon" src="ipojo-in-10-minutes_files/linkext7.gif" alt="" align="absmiddle" border="0" width="7" height="7"></sup></a></span>. The bundle manifest is described in the spell.services.bnd file.<br>
Once this project is done, we are able to implement a Dictionary service.</p>
<h3><a name="iPOJOin10minutes-Thespell.englishproject:ProvidinganOSGiservice"></a>The spell.english project: Providing an OSGi service</h3>
<p>The spell.english project is a simple dictionary implementation of
the Dictionary service. It contains few English words. This
implementation is an iPOJO component.<br>
The first step is to implement the service. Create the "src/spell/english/EnglishDictionary.java" file for the following <em>Dictionary service</em> implementation:</p>
<div class="code"><div class="codeContent">
<pre class="code-java"><span class="code-keyword">package</span> spell.english;
<span class="code-keyword">import</span> spell.services.DictionaryService;
/**
* An implementation of the Dictionary service containing English words
* see DictionaryService <span class="code-keyword">for</span> details of the service.
**/
<span class="code-keyword">public</span> class EnglishDictionary <span class="code-keyword">implements</span> DictionaryService {
<span class="code-comment">// The set of words contained in the dictionary.
</span> <span class="code-object">String</span>[] m_dictionary = { <span class="code-quote">"welcome"</span>, <span class="code-quote">"to"</span>, <span class="code-quote">"the"</span>, <span class="code-quote">"ipojo"</span>, <span class="code-quote">"tutorial"</span> };
/**
* Implements DictionaryService.checkWord(). Determines
* <span class="code-keyword">if</span> the passed in word is contained in the dictionary.
* @param word the word to be checked.
* @<span class="code-keyword">return</span> <span class="code-keyword">true</span> <span class="code-keyword">if</span> the word is in the dictionary,
* <span class="code-keyword">false</span> otherwise.
**/
<span class="code-keyword">public</span> <span class="code-object">boolean</span> checkWord(<span class="code-object">String</span> word) {
word = word.toLowerCase();
<span class="code-comment">// This is very inefficient
</span> <span class="code-keyword">for</span> (<span class="code-object">int</span> i = 0; i &lt; m_dictionary.length; i++) {
<span class="code-keyword">if</span> (m_dictionary[i].equals(word)) {
<span class="code-keyword">return</span> <span class="code-keyword">true</span>;
}
}
<span class="code-keyword">return</span> <span class="code-keyword">false</span>;
}
}</pre>
</div></div>
<p>Notice that this class does not contains neither OSGi nor iPOJO
specific code. It is just an implementation of the Dictionary Service
interface.<br>
Once created, we need to describe this component to ask iPOJO to manage
it. To achieve this, create the "metadata.xml" file in the
spell.english directory:</p>
<div class="code"><div class="codeContent">
<pre class="code-xml"><span class="code-tag">&lt;ipojo&gt;</span>
<span class="code-tag">&lt;component classname=<span class="code-quote">"spell.english.EnglishDictionary"</span>&gt;</span>
<span class="code-tag">&lt;provides/&gt;</span>
<span class="code-tag">&lt;/component&gt;</span>
<span class="code-tag">&lt;instance component=<span class="code-quote">"spell.english.EnglishDictionary"</span>/&gt;</span>
<span class="code-tag">&lt;/ipojo&gt;</span></pre>
</div></div>
<p>This file describes the Dictionary service implementation. First it creates a <em>component</em>. This component has a <em>classname</em> attribute containing the implementation class name. The <em>'provides'</em>
element indicates that the component provide a service. Provided
service interfaces are computed by iPOJO, so it is not necessary to
indicate them. The <em>instance</em> element asks iPOJO to create an
instance of the described component when the bundle starts. The
relation between components and instances is the same than between
classes and objects in the object oriented programming. The <em>component</em>
attribute indicates which component needs to be instantiated. By
default, component type name is the implementation class (i.e. the
classname attribute).</p>
<p>Then, we are able to create the bundle. In the spell.english directory launch the ant command:</p>
<div class="code"><div class="codeContent">
<pre class="code-java">$ ant
Buildfile: build.xml
clean:
[delete] Deleting directory d:\clement\workspaces\sandbox\ipojo\examples\
tutorial-ant\spell.english\output\classes
[delete] Deleting directory d:\clement\workspaces\sandbox\ipojo\examples\
tutorial-ant\spell.english\output
buildclasspath:
compile:
[mkdir] Created dir: d:\clement\workspaces\sandbox\ipojo\examples\
tutorial-ant\spell.english\output
[mkdir] Created dir: d:\clement\workspaces\sandbox\ipojo\examples\
tutorial-ant\spell.english\output\classes
[javac] Compiling 1 source file to d:\clement\workspaces\sandbox\ipojo\examples\
tutorial-ant\spell.english\output\classes
<span class="code-keyword">package</span>:
[bnd] spell.english 1
[ipojo] Input Bundle File : d:\clement\workspaces\sandbox\ipojo\examples\
tutorial-ant\spell.english\output\spell.english.jar
[ipojo] Metadata File : d:\clement\workspaces\sandbox\ipojo\examples\
tutorial-ant\spell.english\metadata.xml
[ipojo] Start bundle manipulation
[ipojo] Bundle manipulation - SUCCESS
[ipojo] Output File : d:\clement\workspaces\sandbox\ipojo\examples\
tutorial-ant\spell.english\output\spell.english.jar
BUILD SUCCESSFUL
Total time: 1 second</pre>
</div></div>
<p>The created bundle is inside the output directory
(spell.english.jar). The build process is based on BND and on the iPOJO
Ant task. The manifest of the bundle is described in the
spell.english.bnd file.</p>
<h3><a name="iPOJOin10minutes-Thespell.checkerproject:RequiringanOSGiservice"></a>The spell.checker project: Requiring an OSGi service</h3>
<p>The spell.checker project aims to provide a <em>spell checker</em> service. However, to serve this service, this implementation requires a <em>dictionary</em>
service. During this step, we will create an iPOJO component requiring
a Dictionary service and providing the Spell Checker service.</p>
<p>First create the file "src/spell/checker/SpellCheck.java" in the spell.checker directory for the following <em>Check Speller service</em> implementation:</p>
<div class="code"><div class="codeContent">
<pre class="code-java"><span class="code-keyword">package</span> spell.checker;
<span class="code-keyword">import</span> java.util.ArrayList;
<span class="code-keyword">import</span> java.util.List;
<span class="code-keyword">import</span> java.util.StringTokenizer;
<span class="code-keyword">import</span> spell.services.DictionaryService;
<span class="code-keyword">import</span> spell.services.SpellChecker;
<span class="code-keyword">public</span> class SpellCheck <span class="code-keyword">implements</span> SpellChecker {
<span class="code-keyword">private</span> DictionaryService m_dictionary;
/**
* Implements SpellChecker.check(). Checks the given passage <span class="code-keyword">for</span> misspelled words.
* @param passage the passage to spell check.
* @<span class="code-keyword">return</span> An array of misspelled words or <span class="code-keyword">null</span> <span class="code-keyword">if</span> no words are misspelled.
*/
<span class="code-keyword">public</span> <span class="code-object">String</span>[] check(<span class="code-object">String</span> passage) {
<span class="code-comment">// No misspelled words <span class="code-keyword">for</span> an empty string.
</span> <span class="code-keyword">if</span> ((passage == <span class="code-keyword">null</span>) || (passage.length() == 0)) { <span class="code-keyword">return</span> <span class="code-keyword">null</span>; }
List errorList = <span class="code-keyword">new</span> ArrayList();
<span class="code-comment">// Tokenize the passage using spaces and punctuation.
</span> StringTokenizer st = <span class="code-keyword">new</span> StringTokenizer(passage, <span class="code-quote">" ,.!?;:"</span>);
<span class="code-comment">// Loop through each word in the passage.
</span> <span class="code-keyword">while</span> (st.hasMoreTokens()) {
<span class="code-object">String</span> word = st.nextToken();
<span class="code-comment">// Check the current word.
</span> <span class="code-keyword">if</span> (! m_dictionary.checkWord(word)) {
<span class="code-comment">// If the word is not correct, then add it
</span> <span class="code-comment">// to the incorrect word list.
</span> errorList.add(word);
}
}
<span class="code-comment">// Return <span class="code-keyword">null</span> <span class="code-keyword">if</span> no words are incorrect.
</span> <span class="code-keyword">if</span> (errorList.size() == 0) { <span class="code-keyword">return</span> <span class="code-keyword">null</span>; }
<span class="code-comment">// Return the array of incorrect words.
</span> <span class="code-keyword">return</span> (<span class="code-object">String</span>[]) errorList.toArray(<span class="code-keyword">new</span> <span class="code-object">String</span>[errorList.size()]);
}
}</pre>
</div></div>
<p>This class implements the SpellChecker service interface as it will provide it. Moreover, it has a <em>special</em>
field "_m_dictionary_". This field represents the required service.
iPOJO will inject a Dictionary service when needed. So, the class can
use it directly. Notice that this class as no OSGi specific code, both
the service providing and the requiring are managed by iPOJO. If the
used dictionary service leaves, iPOJO will try to find another
provider. If no more providers are available, the instance is
invalidated, and the provided service is withdrawn from the service
registry.</p>
<p>Once implemented, we need to describe this component. Create the
"metadata.xml" file in the spell.checker directory for the following
component descriptor:</p>
<div class="code"><div class="codeContent">
<pre class="code-xml"><span class="code-tag">&lt;ipojo&gt;</span>
<span class="code-tag">&lt;component classname=<span class="code-quote">"spell.checker.SpellCheck"</span>&gt;</span>
<span class="code-tag">&lt;requires field=<span class="code-quote">"m_dictionary"</span>/&gt;</span>
<span class="code-tag">&lt;provides/&gt;</span>
<span class="code-tag">&lt;/component&gt;</span>
<span class="code-tag">&lt;instance component=<span class="code-quote">"spell.checker.SpellCheck"</span>/&gt;</span>
<span class="code-tag">&lt;/ipojo&gt;</span></pre>
</div></div>
<p>This description contains a '<em>requires'</em> element. This element indicates to iPOJO to manage the service dependency. The <em>field</em>
attributes describe in which member of the class the service need to be
injected. It is not necessary to write the required service as it is
computed by iPOJO. Notice that iPOJO can inject required service by
invoking methods too.</p>
<p>Finally, we are able to build the bundle. As for previous projects, launch Ant from the project directory.</p>
<h3><a name="iPOJOin10minutes-Thespell.checker.guiproject"></a>The spell.checker.gui project</h3>
<p>The spell.check.gui project contains a very simple user interface (in Swing) allowing a user to interact with a <em>spell checker</em> service.</p>
<p>Create the "src\spell\gui\SpellCheckerGui.java" for the following implementation:</p>
<div class="code"><div class="codeContent">
<pre class="code-java"><span class="code-keyword">package</span> spell.gui;
<span class="code-keyword">import</span> javax.swing.JButton;
<span class="code-keyword">import</span> javax.swing.JFrame;
<span class="code-keyword">import</span> javax.swing.JLabel;
<span class="code-keyword">import</span> javax.swing.JTextField;
<span class="code-keyword">import</span> spell.services.SpellChecker;
/**
* A very simple Gui interacting with the CheckSpeller service
*/
<span class="code-keyword">public</span> class SpellCheckerGui <span class="code-keyword">extends</span> JFrame {
/**
* Swing component where the user write the passage to check.
*/
<span class="code-keyword">private</span> JTextField m_passage = <span class="code-keyword">null</span>;
/**
* Check button
*/
<span class="code-keyword">private</span> JButton m_checkButton = <span class="code-keyword">null</span>;
/**
* Area where the result is displayed.
*/
<span class="code-keyword">private</span> JLabel m_result = <span class="code-keyword">null</span>;
/**
* Service dependency on the SpellChecker.
*/
<span class="code-keyword">private</span> SpellChecker m_checker;
/**
* Constructor.
* Initialize the GUI.
*/
<span class="code-keyword">public</span> SpellCheckerGui() {
<span class="code-keyword">super</span>();
initComponents();
<span class="code-keyword">this</span>.setTitle(<span class="code-quote">"Spellchecker Gui"</span>);
}
/**
* Initialize the Swing Gui.
*/
<span class="code-keyword">private</span> void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
m_checkButton = <span class="code-keyword">new</span> javax.swing.JButton();
m_result = <span class="code-keyword">new</span> javax.swing.JLabel();
m_passage = <span class="code-keyword">new</span> javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); <span class="code-comment">//Stop Felix
</span> getContentPane().setLayout(<span class="code-keyword">new</span> java.awt.GridBagLayout());
m_checkButton.setText(<span class="code-quote">"Check"</span>);
m_checkButton.addActionListener(<span class="code-keyword">new</span> java.awt.event.ActionListener() {
<span class="code-keyword">public</span> void actionPerformed(java.awt.event.ActionEvent e) {
check();
}
});
gridBagConstraints = <span class="code-keyword">new</span> java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.insets = <span class="code-keyword">new</span> java.awt.Insets(2, 2, 2, 2);
getContentPane().add(m_checkButton, gridBagConstraints);
m_result.setPreferredSize(<span class="code-keyword">new</span> java.awt.Dimension(175, 20));
gridBagConstraints = <span class="code-keyword">new</span> java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = <span class="code-keyword">new</span> java.awt.Insets(2, 2, 2, 2);
getContentPane().add(m_result, gridBagConstraints);
m_passage.setPreferredSize(<span class="code-keyword">new</span> java.awt.Dimension(175, 20));
gridBagConstraints = <span class="code-keyword">new</span> java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = <span class="code-keyword">new</span> java.awt.Insets(2, 2, 2, 2);
getContentPane().add(m_passage, gridBagConstraints);
pack();
}
/**
* Check Button action.
* Collects the user input and checks it.
*/
<span class="code-keyword">private</span> void check() {
<span class="code-object">String</span>[] result = m_checker.check(m_passage.getText());
<span class="code-keyword">if</span> (result != <span class="code-keyword">null</span>) {
m_result.setText(result.length + <span class="code-quote">" word(s) are mispelled"</span>);
} <span class="code-keyword">else</span> {
m_result.setText(<span class="code-quote">"All words are correct"</span>);
}
}
/**
* Start callback.
* This method will be called when the instance becomes valid.
* It set the Gui visibility to <span class="code-keyword">true</span>.
*/
<span class="code-keyword">public</span> void start() {
<span class="code-keyword">this</span>.setVisible(<span class="code-keyword">true</span>);
}
/**
* Stop callback.
* This method will be called when the instance becomes invalid or stops.
* It deletes the Gui.
*/
<span class="code-keyword">public</span> void stop() {
<span class="code-keyword">this</span>.dispose();
}
}</pre>
</div></div>
<p>Look at the three last methods. The <em>check</em> methods collect the user input and use a <em>Check speller</em> service to check this input. This method is called when the button is pressed by the user. The <em>start</em> and <em>stop</em>
methods are lifecycle callbacks. As we need to display the user
interface when the instance is created and to delete it when the
instance stops, we need a way to be notified when we need to execute
these actions. iPOJO provide an easy way to do this. The component
provides two callback methods for its activation and deactivation,
start and stop, respectively. Callbacks are used when the component
needs to be informed about a component state change. In iPOJO, the
component state is either <b>INVALID</b> (i.e., not all of the component's constraints are satisfied) or <b>VALID</b>
(i.e., all of the component's constraints are satisfied). In this
example, the start callback method set the GUI visibility to true; the
stop callback method deletes the GUI. The component metadata will
instruct iPOJO to invoke these methods when the component's state
changes to <b>VALID</b> or <b>INVALID</b> respectively.</p>
<p>Create the <em>metadata.xml</em> file in the spell.chercker.gui directory with the following content:</p>
<div class="code"><div class="codeContent">
<pre class="code-xml"><span class="code-tag">&lt;ipojo&gt;</span>
<span class="code-tag">&lt;component classname=<span class="code-quote">"spell.gui.SpellCheckerGui"</span>&gt;</span>
<span class="code-tag">&lt;requires field=<span class="code-quote">"m_checker"</span>/&gt;</span>
<span class="code-tag">&lt;callback transition=<span class="code-quote">"validate"</span> method=<span class="code-quote">"start"</span>/&gt;</span>
<span class="code-tag">&lt;callback transition=<span class="code-quote">"invalidate"</span> method=<span class="code-quote">"stop"</span>/&gt;</span>
<span class="code-tag">&lt;/component&gt;</span>
<span class="code-tag">&lt;instance component=<span class="code-quote">"spell.gui.SpellCheckerGui"</span>/&gt;</span>
<span class="code-tag">&lt;/ipojo&gt;</span></pre>
</div></div>
<p>The component element again has the '<em>classname'</em> attribute that refers to the component implementation class. The '<em>requires</em>' element describes the <em>Check Speller</em> service dependency by simply specifying its associated component field. The '<em>callback'</em> elements describe which method to invoke when the component's state changes. Then the <em>'instance'</em> element asks iPOJO to create an instance of the component.</p>
<p>Once this file is created, you can compile the project by launching <em>ant</em> in the spell.checker.gui directory.</p>
<h2><a name="iPOJOin10minutes-Runningtheapplication"></a>Running the application</h2>
<p>To run the example, start Felix. A distribution of Felix is provided
in the felix directory. This version is configured to launch iPOJO
automatically. From the Felix directory, launch the following command
to start the framework. Then enter a profile name.</p>
<div class="code"><div class="codeContent">
<pre class="code-java">java -jar bin/felix.jar</pre>
</div></div>
<p>You can check installed bundles by using the '<em>ps</em>' command:</p>
<div class="code"><div class="codeContent">
<pre class="code-java">-&gt; ps
START LEVEL 1
ID State Level Name
[ 0] [Active ] [ 0] <span class="code-object">System</span> Bundle (1.0.3)
[ 1] [Active ] [ 1] Apache Felix Shell Service (1.0.0)
[ 2] [Active ] [ 1] Apache Felix Shell TUI (1.0.0)
[ 3] [Active ] [ 1] Apache Felix Bundle Repository (1.0.2)
[ 4] [Active ] [ 1] iPOJO (1.0.0)
-&gt;</pre>
</div></div>
<p>iPOJO runtime is the bundle 4. Once started, install the four created bundles as above:</p>
<div class="code"><div class="codeContent">
<pre class="code-java">start file:../spell.services/output/spell.services.jar
start file:../spell.english/output/spell.english.jar
start file:../spell.checker/output/spell.checker.jar
start file:../spell.checker.gui/output/spell.checker.gui.jar</pre>
</div></div>
<p>When starting the GUI bundle, the user interface appears. Indeed, the <em>Check Speller</em> service is provided. You can interact with this service by entering a passage and clicking on the check button:<br>
<img src="ipojo-in-10-minutes_files/ss.png" align="absmiddle" border="0"><br>
Then, stop the <em>Dictionary</em> service provider (with the <em>stop 6</em>) command. The GUI disappears. Indeed, Spell Checker service cannot be provided as it depends on the Dictionary service.<br>
<img src="ipojo-in-10-minutes_files/spell2.png" align="absmiddle" border="0"><br>
Then, restart the Dictionary service provider with the <em>start 6</em> command. The GUI reappears immediately. You can try to stop the <em>check speller</em> service provider without stopping the <em>dictionary</em> service provider with the <em>stop 7</em> command. As for the last manipulation, the GUI disappears.<br>
<img src="ipojo-in-10-minutes_files/spell3.png" align="absmiddle" border="0"><br>
To go further in the exploration, modify the spell.cheker implementation. For example, add a simple trace, printing wrong words:</p>
<div class="code"><div class="codeContent">
<pre class="code-java"><span class="code-comment">// ...
</span> <span class="code-comment">// Return <span class="code-keyword">null</span> <span class="code-keyword">if</span> no words are incorrect.
</span> <span class="code-keyword">if</span> (errorList.size() == 0) { <span class="code-keyword">return</span> <span class="code-keyword">null</span>; }
<span class="code-comment">// Return the array of incorrect words.
</span> <span class="code-object">System</span>.out.println(<span class="code-quote">"Wrong words:"</span> + errorList); <span class="code-comment">// &lt;-- Add <span class="code-keyword">this</span> line
</span> <span class="code-keyword">return</span> (<span class="code-object">String</span>[]) errorList.toArray(<span class="code-keyword">new</span> <span class="code-object">String</span>[errorList.size()]);</pre>
</div></div>
<p>Then rebuild the spell.checker bundle and update it (with the
'update 7' command). You will see the gui "blinking". In fact, the gui
was stopped as the required spell checker service was no more
available. As soon as it was back, the gui restarts. The gui didn't
lose its state. The other services weren't stopped during the update.<br>
You can uninstall the check service bundle and then re-install a new one (or the same one), you will see the same behavior. </p>
<h2><a name="iPOJOin10minutes-Conclusion"></a>Conclusion</h2>
<p>We saw how to use easily iPOJO to build service-oriented component. In this tutorial, we have demonstrated how to:</p>
<ul>
<li>Publish OSGi services</li>
<li>Require OSGi services</li>
<li>Use lifecycle callbacks to activate and deactivate components</li>
</ul>
<p>iPOJO provides a lot of others features that you can try in the
others available tutorials. Subscribe to the Felix users mailing list
by sending a message to <span class="nobr"><a href="mailto:users-subscribe@felix.apache.org" title="Send mail to users-subscribe@felix.apache.org" rel="nofollow">users-subscribe@felix.apache.org<sup><img class="rendericon" src="ipojo-in-10-minutes_files/mail_small.gif" alt="" align="absmiddle" border="0" width="13" height="12"></sup></a></span>; after subscribing, email questions or feedback to <span class="nobr"><a href="mailto:users@felix.apache.org" title="Send mail to users@felix.apache.org" rel="nofollow">users@felix.apache.org<sup><img class="rendericon" src="ipojo-in-10-minutes_files/mail_small.gif" alt="" align="absmiddle" border="0" width="13" height="12"></sup></a></span>.</p>
</td>
<td class="confluenceTd" valign="top" width="20%">
<h6><a name="iPOJOin10minutes-Overview"></a><b>Overview</b></h6>
<ul>
<li><a href="http://felix.apache.org/site/apache-felix-ipojo.html" title="Apache Felix iPOJO">Home Page</a></li>
<li><a href="http://felix.apache.org/site/apache-felix-ipojo-feature-overview.html" title="Apache Felix iPOJO Feature Overview">iPOJO Feature Overview</a></li>
<li><a href="http://felix.apache.org/site/download.html" title="Download">Download &amp; Install </a></li>
</ul>
<h6><a name="iPOJOin10minutes-GettingStarted"></a><b>Getting Started</b></h6>
<ul>
<li><a href="" title="iPOJO in 10 minutes">iPOJO in 10 minutes</a></li>
<li><a href="http://felix.apache.org/site/how-to-use-ipojo-annotations.html" title="How to use iPOJO Annotations">How to use iPOJO Annotations</a></li>
<li><a href="http://felix.apache.org/site/ipojo-hello-word-maven-based-tutorial.html" title="iPOJO Hello Word (Maven-Based) tutorial">iPOJO Hello Word (Maven-Based) tutorial</a></li>
<li><a href="http://felix.apache.org/site/ipojo-advanced-tutorial.html" title="iPOJO Advanced Tutorial">iPOJO Advanced Tutorial</a></li>
<li><a href="http://felix.apache.org/site/ipojo-composition-tutorial.html" title="iPOJO Composition Tutorial">iPOJO Composition Tutorial</a></li>
</ul>
<h6><a name="iPOJOin10minutes-UserGuide"></a><b>User Guide</b></h6>
<ul>
<li><a href="http://felix.apache.org/site/describing-components.html" title="Describing components">Describing components (handler list) </a></li>
<li><a href="http://felix.apache.org/site/using-xml-schemas.html" title="Using XML Schemas">Using XML Schemas</a></li>
<li><a href="http://felix.apache.org/site/apache-felix-ipojo-testing-components.html" title="apache-felix-ipojo-testing-components">Testing components</a></li>
<li><a href="http://felix.apache.org/site/ipojo-advanced-topics.html" title="iPOJO Advanced Topics">Advanced Topics</a></li>
<li><a href="http://felix.apache.org/site/ipojo-faq.html" title="iPOJO FAQ">FAQ</a></li>
</ul>
<h6><a name="iPOJOin10minutes-Tools"></a><b>Tools</b></h6>
<ul>
<li><a href="http://felix.apache.org/site/ipojo-eclipse-plug-in.html" title="iPOJO Eclipse Plug-in">iPOJO Eclipse Plug-in</a></li>
<li><a href="http://felix.apache.org/site/ipojo-ant-task.html" title="iPOJO Ant Task">iPOJO Ant Task</a></li>
<li><a href="http://felix.apache.org/site/ipojo-maven-plug-in.html" title="iPOJO Maven Plug-in">iPOJO Maven Plug-in</a></li>
<li><a href="http://felix.apache.org/site/apache-felix-ipojo-junit4osgi.html" title="apache-felix-ipojo-junit4osgi">Junit4OSGi</a></li>
<li><a href="http://felix.apache.org/site/ipojo-concepts-overview.html" title="iPOJO Concepts Overview">iPOJO concepts overview</a></li>
</ul>
<h6><a name="iPOJOin10minutes-DeveloperGuide"></a><b>Developer Guide</b></h6>
<ul>
<li>API: <span class="nobr"><a href="http://people.apache.org/%7Eclement/ipojo/api/1.0/" title="Visit page outside Confluence" rel="nofollow">1.0<sup><img class="rendericon" src="ipojo-in-10-minutes_files/linkext7.gif" alt="" align="absmiddle" border="0" width="7" height="7"></sup></a></span></li>
<li><a href="http://felix.apache.org/site/how-to-write-your-own-handler.html" title="How to write your own handler">How to write your own handler</a></li>
<li><a href="http://felix.apache.org/site/how-to-use-ipojo-manipulation-metadata.html" title="How to use iPOJO Manipulation Metadata">How to use iPOJO Manipulation Metadata</a></li>
<li><a href="http://felix.apache.org/site/dive-into-the-ipojo-manipulation-depths.html" title="Dive into the iPOJO Manipulation depths">Dive into the iPOJO Manipulation depths</a></li>
</ul>
<h6><a name="iPOJOin10minutes-Misc&amp;Contact"></a><b>Misc &amp; Contact</b></h6>
<ul>
<li><a href="http://felix.apache.org/site/apache-felix-ipojo-issuestracker.html" title="apache-felix-ipojo-issuestracker">Issues Tracker</a></li>
<li><a href="http://felix.apache.org/site/apache-felix-ipojo-supportedvms.html" title="apache-felix-ipojo-supportedVMs">Supported JVMs</a></li>
<li><a href="http://felix.apache.org/site/apache-felix-ipojo-supportedosgi.html" title="apache-felix-ipojo-supportedOSGi">Supported OSGi Implementations</a></li>
<li><span class="nobr"><a href="http://ipojo-dark-side.blogspot.com/" title="Visit page outside Confluence" rel="nofollow">iPOJO's Dark Side Blog<sup><img class="rendericon" src="ipojo-in-10-minutes_files/linkext7.gif" alt="" align="absmiddle" border="0" width="7" height="7"></sup></a></span></li>
<li><a href="http://felix.apache.org/site/future-ideas.html" title="Future Ideas">Future Ideas</a></li>
<li><a href="http://felix.apache.org/site/contact.html" title="Contact">Contact</a></li>
<li><a href="http://felix.apache.org/site/related-works.html" title="Related Works">Related Works</a></li>
<li><a href="http://felix.apache.org/site/article-presentations.html" title="Article &amp; Presentations">Article &amp; Presentations</a></li>
</ul>
<hr>
<div class="" align="center">
<p><span class="nobr"><a href="http://cwiki.apache.org/confluence/createrssfeed.action?types=blogpost&amp;statuses=created&amp;statuses=modified&amp;spaces=FELIX&amp;labelString=iPOJO&amp;rssType=atom&amp;maxResults=10&amp;timeSpan=5&amp;publicFeed=true&amp;title=iPOJO%20Atom%20Feed" title="Stay tuned!" rel="nofollow"><img src="ipojo-in-10-minutes_files/feed-icon-32x32.png" align="absmiddle" border="0"><sup><img class="rendericon" src="ipojo-in-10-minutes_files/linkext7.gif" alt="" align="absmiddle" border="0" width="7" height="7"></sup></a></span></p></div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script><script src="ipojo-in-10-minutes_files/ga.js" type="text/javascript"></script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-1518442-4");
pageTracker._trackPageview();
</script>
</td></tr></tbody></table>
</div>
</body></html>