blob: 850312fa96dafbc585c3a9244cf9108121c6b4e8 [file] [log] [blame]
Clement Escoffier50254022008-05-16 20:33:54 +00001
2<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3<HTML>
4
5
6<HEAD>
7 <TITLE>Apache Felix - iPOJO in 10 minutes</TITLE>
8 <LINK rel="stylesheet" href="http://felix.apache.org/site/media.data/site.css" type="text/css" media="all">
9 <META http-equiv="Content-Type" content="text/html;charset=UTF-8">
10 </HEAD>
11 <BODY>
12 <DIV class="title"><DIV class="logo"><A href="http://felix.apache.org/site/index.html"><IMG border="0" alt="Apache Felix" src="http://felix.apache.org/site/media.data/logo.png"></A></DIV><DIV class="header"><A href="http://www.apache.org/"><IMG border="0" alt="Apache" src="http://felix.apache.org/site/media.data/apache.png"></A></DIV></DIV>
13 <DIV class="menu">
14 <UL>
15 <LI><A href="http://felix.apache.org/site/news.html" title="news">news</A></LI>
16 <LI><A href="http://felix.apache.org/site/license.html" title="license">license</A></LI>
17 <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="http://cwiki.apache.org/confluence/images/icons/linkext7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN></LI>
18 <LI><A href="http://felix.apache.org/site/documentation.html" title="documentation">documentation</A></LI>
19 <LI><A href="http://felix.apache.org/site/mailinglists.html" title="mailinglists">mailing lists</A></LI>
20 <LI><A href="http://felix.apache.org/site/contributing.html" title="Contributing">contributing</A></LI>
21 <LI><SPAN class="nobr"><A href="http://www.apache.org/" title="Visit page outside Confluence" rel="nofollow">asf<SUP><IMG class="rendericon" src="http://cwiki.apache.org/confluence/images/icons/linkext7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN></LI>
22 <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="http://cwiki.apache.org/confluence/images/icons/linkext7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN></LI>
23 <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="http://cwiki.apache.org/confluence/images/icons/linkext7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN>
24<!-- ApacheCon Ad -->
25<IFRAME src="http://www.apache.org/ads/button.html" style="border-width:0; float: left" frameborder="0" scrolling="no" width="135" height="135"></IFRAME>
26<P style="height: 100px">
27<!-- ApacheCon Ad --></LI>
28</UL>
29 </DIV>
30 <DIV class="main">
31<TABLE class="sectionMacro" border="0" cellpadding="5" cellspacing="0" width="100%"><TBODY><TR>
32<TD class="confluenceTd" valign="top" width="80%">
33<H1><A name="iPOJOin10minutes-iPOJOin10minutes"></A>iPOJO in 10 minutes</H1>
34
35<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>
36
37<H2><A name="iPOJOin10minutes-Introduction"></A>Introduction</H2>
38
39<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>
40
41<H2><A name="iPOJOin10minutes-ThePOJOconcept"></A>The POJO concept</H2>
42
43<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: &quot;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.&quot; From the developer's perspective, the iPOJO framework strives to only require POJOs in as much as it is possible.</P>
44
45<H2><A name="iPOJOin10minutes-iPOJOservicecomponentoverview"></A>iPOJO service component overview</H2>
46
47<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>
48
49<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>
50
51<H2><A name="iPOJOin10minutes-Asimpleexample"></A>A simple example</H2>
52
53<P>In this tutorial we will present how to:</P>
54<UL>
55 <LI>Publish an OSGi service</LI>
56 <LI>Require an OSGi service</LI>
57 <LI>Use lifecycle callbacks to activate and deactivate components</LI>
58</UL>
59
60
61<H3><A name="iPOJOin10minutes-PresentationoftheSpellapplication"></A>Presentation of the <EM>Spell</EM> application</H3>
62
63<P>To illustrate iPOJO features, we will implement a very simple application. This application is composed by three components:</P>
64<UL>
65 <LI>A component implementing a dictionary service</LI>
66 <LI>A component requiring the dictionary service and providing a spellchecker service</LI>
67 <LI>A component requiring the spellchecker and providing an user interface</LI>
68</UL>
69
70
71<P><IMG src="http://felix.apache.org/site/ipojo-in-10-minutes.data/spell.png" align="absmiddle" border="0"></P>
72
73<H3><A name="iPOJOin10minutes-Preparingthetutorial"></A>Preparing the tutorial</H3>
74
75<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="http://cwiki.apache.org/confluence/images/icons/linkext7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN> to download and install Ant). Download the tutorial archive available <SPAN class="nobr"><A href="http://felix.apache.org/site/ipojo-in-10-minutes.data/tutorial.zip" title="tutorial.zip attached to iPOJO in 10 minutes">here<SUP><IMG class="rendericon" src="http://cwiki.apache.org/confluence/images/icons/link_attachment_7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN> and then unzip it. The archive is composed by seven directories:</P>
76<UL>
77 <LI>spell.services contains service interfaces used by the applications</LI>
78 <LI>spell.english contains an implementation of the Dictionary service (containing English words)</LI>
79 <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>
80 <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>
81 <LI>The task directory contains Ant tasks used to build the project</LI>
82 <LI>The solution directory contains an already developed version of the application.</LI>
83 <LI>Finally, the felix folder contains a configured version of the Felix runtime</LI>
84</UL>
85
86
87<H3><A name="iPOJOin10minutes-Thespell.servicesproject"></A>The spell.services project</H3>
88
89<P>The spell.services project contains only service interfaces. It is not an iPOJO powered bundle.</P>
90
91<P>Go inside the Spell.services directory and create the file &quot;src/spell/services/DictionaryService.java&quot; for the following <EM>Dictionary</EM> service interface:</P>
92<DIV class="code"><DIV class="codeContent">
93<PRE class="code-java"><SPAN class="code-keyword">package</SPAN> spell.services;
94/**
95 * A simple service <SPAN class="code-keyword">interface</SPAN> that defines a dictionary service.
96 * A dictionary service simply verifies the existence of a word.
97**/
98<SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">interface</SPAN> DictionaryService {
99 /**
100 * Check <SPAN class="code-keyword">for</SPAN> the existence of a word.
101 * @param word the word to be checked.
102 * @<SPAN class="code-keyword">return</SPAN> <SPAN class="code-keyword">true</SPAN> <SPAN class="code-keyword">if</SPAN> the word is in the dictionary,
103 * <SPAN class="code-keyword">false</SPAN> otherwise.
104 **/
105 <SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">boolean</SPAN> checkWord(<SPAN class="code-object">String</SPAN> word);
106}</PRE>
107</DIV></DIV>
108<P>Then, create the file &quot;src/spell/services/SpellChecker.java&quot; for the following <EM>Spell Checker</EM> service interface:</P>
109<DIV class="code"><DIV class="codeContent">
110<PRE class="code-java"><SPAN class="code-keyword">package</SPAN> spell.services;
111/**
112 * A simple service <SPAN class="code-keyword">interface</SPAN> that defines a spell checker service.
113 * A spell checker service checks the spelling of all words in a
114 * given passage. A passage is any number of words separated by
115 * a space character and the following punctuation marks: comma,
116 * period, exclamation mark, question mark, semi-colon, and colon.
117**/
118<SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">interface</SPAN> SpellChecker {
119 /**
120 * Checks a given passage <SPAN class="code-keyword">for</SPAN> spelling errors. A passage is any
121 * number of words separated by a space and any of the following
122 * punctuation marks: comma (,), period (.), exclamation mark (!),
123 * question mark (?), semi-colon (;), and colon(:).
124 * @param passage the passage to spell check.
125 * @<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
126 * words are misspelled.
127 **/
128 <SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">String</SPAN>[] check(<SPAN class="code-object">String</SPAN> passage);
129}</PRE>
130</DIV></DIV>
131<P>Once created, you can build the project by launching Ant from the project directory.</P>
132<DIV class="code"><DIV class="codeContent">
133<PRE class="code-java">$ ant
134Buildfile: build.xml
135clean:
136compile:
137 [mkdir] Created dir: d:\clement\workspaces\sandbox\ipojo\examples\tutorial-ant\spell.services\output
138 [mkdir] Created dir: d:\clement\workspaces\sandbox\ipojo\examples\tutorial-ant\spell.services\output\classes
139 [javac] Compiling 2 source files to d:\clement\workspaces\sandbox\ipojo\examples\tutorial-ant\spell.services\
140 output\classes
141<SPAN class="code-keyword">package</SPAN>:
142 [bnd] spell.services 2
143BUILD SUCCESSFUL
144Total time: 0 seconds</PRE>
145</DIV></DIV>
146<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="http://cwiki.apache.org/confluence/images/icons/linkext7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN>. The bundle manifest is described in the spell.services.bnd file.<BR>
147Once this project is done, we are able to implement a Dictionary service.</P>
148
149<H3><A name="iPOJOin10minutes-Thespell.englishproject%3AProvidinganOSGiservice"></A>The spell.english project: Providing an OSGi service</H3>
150
151<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>
152The first step is to implement the service. Create the &quot;src/spell.english/EnglishDictionary.java&quot; file for the following <EM>Dictionary service</EM> implementation:</P>
153<DIV class="code"><DIV class="codeContent">
154<PRE class="code-java"><SPAN class="code-keyword">package</SPAN> spell.english;
155<SPAN class="code-keyword">import</SPAN> spell.services.DictionaryService;
156/**
157 * An implementation of the Dictionary service containing English words
158 * see DictionaryService <SPAN class="code-keyword">for</SPAN> details of the service.
159 **/
160<SPAN class="code-keyword">public</SPAN> class EnglishDictionary <SPAN class="code-keyword">implements</SPAN> DictionaryService {
161 <SPAN class="code-comment">// The set of words contained in the dictionary.
162</SPAN> <SPAN class="code-object">String</SPAN>[] m_dictionary = { <SPAN class="code-quote">&quot;welcome&quot;</SPAN>, <SPAN class="code-quote">&quot;to&quot;</SPAN>, <SPAN class="code-quote">&quot;the&quot;</SPAN>, <SPAN class="code-quote">&quot;ipojo&quot;</SPAN>, <SPAN class="code-quote">&quot;tutorial&quot;</SPAN> };
163 /**
164 * Implements DictionaryService.checkWord(). Determines
165 * <SPAN class="code-keyword">if</SPAN> the passed in word is contained in the dictionary.
166 * @param word the word to be checked.
167 * @<SPAN class="code-keyword">return</SPAN> <SPAN class="code-keyword">true</SPAN> <SPAN class="code-keyword">if</SPAN> the word is in the dictionary,
168 * <SPAN class="code-keyword">false</SPAN> otherwise.
169 **/
170 <SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">boolean</SPAN> checkWord(<SPAN class="code-object">String</SPAN> word) {
171 word = word.toLowerCase();
172 <SPAN class="code-comment">// This is very inefficient
173</SPAN> <SPAN class="code-keyword">for</SPAN> (<SPAN class="code-object">int</SPAN> i = 0; i &lt; m_dictionary.length; i++) {
174 <SPAN class="code-keyword">if</SPAN> (m_dictionary[i].equals(word)) {
175 <SPAN class="code-keyword">return</SPAN> <SPAN class="code-keyword">true</SPAN>;
176 }
177 }
178 <SPAN class="code-keyword">return</SPAN> <SPAN class="code-keyword">false</SPAN>;
179 }
180}</PRE>
181</DIV></DIV>
182<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>
183Once created, we need to describe this component to ask iPOJO to manage it. To achieve this, create the &quot;metadata.xml&quot; file in the spell.english directory:</P>
184<DIV class="code"><DIV class="codeContent">
185<PRE class="code-xml"><SPAN class="code-tag">&lt;ipojo&gt;</SPAN>
186<SPAN class="code-tag">&lt;component classname=<SPAN class="code-quote">&quot;spell.english.EnglishDictionary&quot;</SPAN>&gt;</SPAN>
187 <SPAN class="code-tag">&lt;provides/&gt;</SPAN>
188<SPAN class="code-tag">&lt;/component&gt;</SPAN>
189<SPAN class="code-tag">&lt;instance component=<SPAN class="code-quote">&quot;spell.english.EnglishDictionary&quot;</SPAN>/&gt;</SPAN>
190<SPAN class="code-tag">&lt;/ipojo&gt;</SPAN></PRE>
191</DIV></DIV>
192<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>
193
194<P>Then, we are able to create the bundle. In the spell.english directory launch the ant command:</P>
195<DIV class="code"><DIV class="codeContent">
196<PRE class="code-java">$ ant
197Buildfile: build.xml
198clean:
199 [delete] Deleting directory d:\clement\workspaces\sandbox\ipojo\examples\tutorial-ant\spell.english\output\classes
200 [delete] Deleting directory d:\clement\workspaces\sandbox\ipojo\examples\tutorial-ant\spell.english\output
201buildclasspath:
202compile:
203 [mkdir] Created dir: d:\clement\workspaces\sandbox\ipojo\examples\tutorial-ant\spell.english\output
204 [mkdir] Created dir: d:\clement\workspaces\sandbox\ipojo\examples\tutorial-ant\spell.english\output\classes
205 [javac] Compiling 1 source file to d:\clement\workspaces\sandbox\ipojo\examples\tutorial-ant\spell.english\output
206 \classes
207<SPAN class="code-keyword">package</SPAN>:
208 [bnd] spell.english 1
209 [ipojo] Input Bundle File : d:\clement\workspaces\sandbox\ipojo\examples\tutorial-ant\spell.english\output
210 \spell.english.jar
211 [ipojo] Metadata File : d:\clement\workspaces\sandbox\ipojo\examples\tutorial-ant\spell.english\metadata.xml
212 [ipojo] Start bundle manipulation
213 [ipojo] Bundle manipulation - SUCCESS
214 [ipojo] Output File : d:\clement\workspaces\sandbox\ipojo\examples\tutorial-ant\spell.english\output
215 \spell.english.jar
216BUILD SUCCESSFUL
217Total time: 1 second</PRE>
218</DIV></DIV>
219<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>
220
221<H3><A name="iPOJOin10minutes-Thespell.checkerproject%3ARequiringanOSGiservice"></A>The spell.checker project: Requiring an OSGi service</H3>
222
223<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>
224
225<P>First create the file &quot;src/spell/checker/SpellCheck.java&quot; in the spell.checker directory for the following <EM>Check Speller service</EM>implementation:</P>
226<DIV class="code"><DIV class="codeContent">
227<PRE class="code-java"><SPAN class="code-keyword">package</SPAN> spell.check;
228<SPAN class="code-keyword">import</SPAN> java.util.ArrayList;
229<SPAN class="code-keyword">import</SPAN> java.util.List;
230<SPAN class="code-keyword">import</SPAN> java.util.StringTokenizer;
231<SPAN class="code-keyword">import</SPAN> spell.services.DictionaryService;
232<SPAN class="code-keyword">import</SPAN> spell.services.SpellChecker;
233<SPAN class="code-keyword">public</SPAN> class SpellCheck <SPAN class="code-keyword">implements</SPAN> SpellChecker {
234 <SPAN class="code-keyword">private</SPAN> DictionaryService m_dictionary;
235 /**
236 * Implements SpellChecker.check(). Checks the given passage <SPAN class="code-keyword">for</SPAN> misspelled words.
237 * @param passage the passage to spell check.
238 * @<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.
239 */
240 <SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">String</SPAN>[] check(<SPAN class="code-object">String</SPAN> passage) {
241 <SPAN class="code-comment">// No misspelled words <SPAN class="code-keyword">for</SPAN> an empty string.
242</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>; }
243 List errorList = <SPAN class="code-keyword">new</SPAN> ArrayList();
244 <SPAN class="code-comment">// Tokenize the passage using spaces and punctuation.
245</SPAN> StringTokenizer st = <SPAN class="code-keyword">new</SPAN> StringTokenizer(passage, <SPAN class="code-quote">&quot; ,.!?;:&quot;</SPAN>);
246 <SPAN class="code-comment">// Loop through each word in the passage.
247</SPAN> <SPAN class="code-keyword">while</SPAN> (st.hasMoreTokens()) {
248 <SPAN class="code-object">String</SPAN> word = st.nextToken();
249 <SPAN class="code-comment">// Check the current word.
250</SPAN> <SPAN class="code-keyword">if</SPAN> (! m_dictionary.checkWord(word)) {
251 <SPAN class="code-comment">// If the word is not correct, then add it
252</SPAN> <SPAN class="code-comment">// to the incorrect word list.
253</SPAN> errorList.add(word);
254 }
255 }
256 <SPAN class="code-comment">// Return <SPAN class="code-keyword">null</SPAN> <SPAN class="code-keyword">if</SPAN> no words are incorrect.
257</SPAN> <SPAN class="code-keyword">if</SPAN> (errorList.size() == 0) { <SPAN class="code-keyword">return</SPAN> <SPAN class="code-keyword">null</SPAN>; }
258 <SPAN class="code-comment">// Return the array of incorrect words.
259</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()]);
260 }
261}</PRE>
262</DIV></DIV>
263<P>This class implements the SpellChecker service interface as it will provide it. Moreover, it has a <EM>special</EM> field &quot;_m_dictionary_&quot;. 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>
264
265<P>Once implemented, we need to describe this component. Create the &quot;metadata.xml&quot; file in the spell.checker directory for the following component descriptor:</P>
266<DIV class="code"><DIV class="codeContent">
267<PRE class="code-xml"><SPAN class="code-tag">&lt;ipojo&gt;</SPAN>
268<SPAN class="code-tag">&lt;component classname=<SPAN class="code-quote">&quot;spell.check.SpellCheck&quot;</SPAN>&gt;</SPAN>
269 <SPAN class="code-tag">&lt;requires field=<SPAN class="code-quote">&quot;m_dictionary&quot;</SPAN>/&gt;</SPAN>
270 <SPAN class="code-tag">&lt;provides/&gt;</SPAN>
271<SPAN class="code-tag">&lt;/component&gt;</SPAN>
272<SPAN class="code-tag">&lt;instance component=<SPAN class="code-quote">&quot;spell.check.SpellCheck&quot;</SPAN>/&gt;</SPAN>
273<SPAN class="code-tag">&lt;/ipojo&gt;</SPAN></PRE>
274</DIV></DIV>
275<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>
276
277<P>Finally, we are able to build the bundle. As for previous projects, launch Ant from the project directory.</P>
278
279<H3><A name="iPOJOin10minutes-Thespell.check.guiproject"></A>The spell.check.gui project</H3>
280
281<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>
282
283<P>Create the &quot;src\spell\gui\SpellCheckerGui.java&quot; for the following implementation:</P>
284<DIV class="code"><DIV class="codeContent">
285<PRE class="code-java"><SPAN class="code-keyword">package</SPAN> spell.gui;
286<SPAN class="code-keyword">import</SPAN> javax.swing.JButton;
287<SPAN class="code-keyword">import</SPAN> javax.swing.JFrame;
288<SPAN class="code-keyword">import</SPAN> javax.swing.JLabel;
289<SPAN class="code-keyword">import</SPAN> javax.swing.JTextField;
290<SPAN class="code-keyword">import</SPAN> spell.services.SpellChecker;
291/**
292 * A very simple Gui interacting with the CheckSpeller service
293 */
294<SPAN class="code-keyword">public</SPAN> class SpellCheckerGui <SPAN class="code-keyword">extends</SPAN> JFrame {
295 /**
296 * Swing component where the user write the passage to check.
297 */
298 <SPAN class="code-keyword">private</SPAN> JTextField m_passage = <SPAN class="code-keyword">null</SPAN>;
299
300 /**
301 * Check button
302 */
303 <SPAN class="code-keyword">private</SPAN> JButton m_checkButton = <SPAN class="code-keyword">null</SPAN>;
304
305 /**
306 * Area where the result is displayed.
307 */
308 <SPAN class="code-keyword">private</SPAN> JLabel m_result = <SPAN class="code-keyword">null</SPAN>;
309 /**
310 * Service dependency on the SpellChecker.
311 */
312 <SPAN class="code-keyword">private</SPAN> SpellChecker m_checker;
313
314 /**
315 * Constructor.
316 * Initialize the GUI.
317 */
318 <SPAN class="code-keyword">public</SPAN> SpellCheckerGui() {
319 <SPAN class="code-keyword">super</SPAN>();
320 initComponents();
321 <SPAN class="code-keyword">this</SPAN>.setTitle(<SPAN class="code-quote">&quot;Spellchecker Gui&quot;</SPAN>);
322 }
323 /**
324 * Initialize the Swing Gui.
325 */
326 <SPAN class="code-keyword">private</SPAN> void initComponents() {
327 java.awt.GridBagConstraints gridBagConstraints;
328 m_checkButton = <SPAN class="code-keyword">new</SPAN> javax.swing.JButton();
329 m_result = <SPAN class="code-keyword">new</SPAN> javax.swing.JLabel();
330 m_passage = <SPAN class="code-keyword">new</SPAN> javax.swing.JTextField();
331 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
332 getContentPane().setLayout(<SPAN class="code-keyword">new</SPAN> java.awt.GridBagLayout());
333 m_checkButton.setText(<SPAN class="code-quote">&quot;Check&quot;</SPAN>);
334 m_checkButton.addActionListener(<SPAN class="code-keyword">new</SPAN> java.awt.event.ActionListener() {
335 <SPAN class="code-keyword">public</SPAN> void actionPerformed(java.awt.event.ActionEvent e) {
336 check();
337 }
338 });
339 gridBagConstraints = <SPAN class="code-keyword">new</SPAN> java.awt.GridBagConstraints();
340 gridBagConstraints.gridx = 0;
341 gridBagConstraints.gridy = 1;
342 gridBagConstraints.insets = <SPAN class="code-keyword">new</SPAN> java.awt.Insets(2, 2, 2, 2);
343 getContentPane().add(m_checkButton, gridBagConstraints);
344 m_result.setPreferredSize(<SPAN class="code-keyword">new</SPAN> java.awt.Dimension(175, 20));
345 gridBagConstraints = <SPAN class="code-keyword">new</SPAN> java.awt.GridBagConstraints();
346 gridBagConstraints.gridx = 0;
347 gridBagConstraints.gridy = 2;
348 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
349 gridBagConstraints.insets = <SPAN class="code-keyword">new</SPAN> java.awt.Insets(2, 2, 2, 2);
350 getContentPane().add(m_result, gridBagConstraints);
351 m_passage.setPreferredSize(<SPAN class="code-keyword">new</SPAN> java.awt.Dimension(175, 20));
352 gridBagConstraints = <SPAN class="code-keyword">new</SPAN> java.awt.GridBagConstraints();
353 gridBagConstraints.gridx = 0;
354 gridBagConstraints.gridy = 0;
355 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
356 gridBagConstraints.insets = <SPAN class="code-keyword">new</SPAN> java.awt.Insets(2, 2, 2, 2);
357 getContentPane().add(m_passage, gridBagConstraints);
358 pack();
359 }
360 /**
361 * Check Button action.
362 * Collects the user input and checks it.
363 */
364 <SPAN class="code-keyword">private</SPAN> void check() {
365 <SPAN class="code-object">String</SPAN>[] result = m_checker.check(m_passage.getText());
366 <SPAN class="code-keyword">if</SPAN> (result != <SPAN class="code-keyword">null</SPAN>) {
367 m_result.setText(result.length + <SPAN class="code-quote">&quot; word(s) are mispelled&quot;</SPAN>);
368 } <SPAN class="code-keyword">else</SPAN> {
369 m_result.setText(<SPAN class="code-quote">&quot;All words are correct&quot;</SPAN>);
370 }
371 }
372 /**
373 * Start callback.
374 * This method will be called when the instance becomes valid.
375 * It set the Gui visibility to <SPAN class="code-keyword">true</SPAN>.
376 */
377 <SPAN class="code-keyword">public</SPAN> void start() {
378 <SPAN class="code-keyword">this</SPAN>.setVisible(<SPAN class="code-keyword">true</SPAN>);
379 }
380 /**
381 * Stop callback.
382 * This method will be called when the instance becomes invalid or stops.
383 * It deletes the Gui.
384 */
385 <SPAN class="code-keyword">public</SPAN> void stop() {
386 <SPAN class="code-keyword">this</SPAN>.dispose();
387 }
388}</PRE>
389</DIV></DIV>
390<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>
391
392<P>Create the <EM>metadata.xml</EM> file in the spell.chercker.gui directory with the following content:</P>
393<DIV class="code"><DIV class="codeContent">
394<PRE class="code-xml"><SPAN class="code-tag">&lt;ipojo&gt;</SPAN>
395<SPAN class="code-tag">&lt;component classname=<SPAN class="code-quote">&quot;spell.gui.SpellCheckerGui&quot;</SPAN>&gt;</SPAN>
396 <SPAN class="code-tag">&lt;requires field=<SPAN class="code-quote">&quot;m_checker&quot;</SPAN>/&gt;</SPAN>
397 <SPAN class="code-tag">&lt;callback transition=<SPAN class="code-quote">&quot;validate&quot;</SPAN> method=<SPAN class="code-quote">&quot;start&quot;</SPAN>/&gt;</SPAN>
398 <SPAN class="code-tag">&lt;callback transition=<SPAN class="code-quote">&quot;invalidate&quot;</SPAN> method=<SPAN class="code-quote">&quot;stop&quot;</SPAN>/&gt;</SPAN>
399<SPAN class="code-tag">&lt;/component&gt;</SPAN>
400<SPAN class="code-tag">&lt;instance component=<SPAN class="code-quote">&quot;spell.gui.SpellCheckerGui&quot;</SPAN>/&gt;</SPAN>
401<SPAN class="code-tag">&lt;/ipojo&gt;</SPAN></PRE>
402</DIV></DIV>
403<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>
404
405<P>Once this file is created, you can compile the project by launching <EM>ant</EM> in the spell.checker.gui directory.</P>
406
407<H2><A name="iPOJOin10minutes-Runningtheapplication"></A>Running the application</H2>
408
409<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>
410<DIV class="code"><DIV class="codeContent">
411<PRE class="code-java">java -jar bin/felix.jar</PRE>
412</DIV></DIV>
413<P>You can check installed bundles by using the '_ps'_command:</P>
414<DIV class="code"><DIV class="codeContent">
415<PRE class="code-java">-&gt; ps
416START LEVEL 1
417 ID State Level Name
418[ 0] [Active ] [ 0] <SPAN class="code-object">System</SPAN> Bundle (1.0.3)
419[ 1] [Active ] [ 1] Apache Felix Shell Service (1.0.0)
420[ 2] [Active ] [ 1] Apache Felix Shell TUI (1.0.0)
421[ 3] [Active ] [ 1] Apache Felix Bundle Repository (1.0.2)
422[ 4] [Active ] [ 1] iPOJO (0.7.6.SNAPSHOT)
423-&gt;</PRE>
424</DIV></DIV>
425<P>iPOJO runtime is the bundle 4. Once started, install the four created bundles as above:</P>
426<DIV class="code"><DIV class="codeContent">
427<PRE class="code-java">start file:../spell.services/output/spell.services.jar
428start file:../spell.english/output/spell.english.jar
429start file:../spell.checker/output/spell.checker.jar
430start file:../spell.checker.gui/output/spell.checker.gui.jar</PRE>
431</DIV></DIV>
432<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>
433<IMG src="http://felix.apache.org/site/ipojo-in-10-minutes.data/ss.png" align="absmiddle" border="0"><BR>
434Then, 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>
435<IMG src="http://felix.apache.org/site/ipojo-in-10-minutes.data/spell2.png" align="absmiddle" border="0"><BR>
436Then, 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>
437<IMG src="http://felix.apache.org/site/ipojo-in-10-minutes.data/spell3.png" align="absmiddle" border="0"></P>
438
439<H2><A name="iPOJOin10minutes-Conclusion"></A>Conclusion</H2>
440
441<P>We saw how to use easily iPOJO to build service-oriented component. In this tutorial, we have demonstrated how to:</P>
442<UL>
443 <LI>Publish OSGi services</LI>
444 <LI>Require OSGi services</LI>
445 <LI>Use lifecycle callbacks to activate and deactivate components</LI>
446</UL>
447
448
449<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 users-subscribe@felix.apache.org; after subscribing, email questions or feedback to users@felix.apache.org.</P>
450</TD>
451<TD class="confluenceTd" valign="top" width="20%">
452<H6><A name="iPOJOin10minutes-Overview"></A><B>Overview</B></H6>
453<UL>
454 <LI><A href="http://felix.apache.org/site/apache-felix-ipojo.html" title="Apache Felix iPOJO">Home Page</A></LI>
455 <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>
456 <LI><A href="http://felix.apache.org/site/download.html" title="Download">Download &amp; Install </A></LI>
457</UL>
458
459
460<H6><A name="iPOJOin10minutes-GettingStarted"></A><B>Getting Started</B></H6>
461<UL>
462 <LI><A href="#" title="iPOJO in 10 minutes">iPOJO in 10 minutes</A></LI>
463 <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 &#40;Maven&#45;Based&#41; tutorial</A></LI>
464 <LI><A href="http://felix.apache.org/site/ipojo-advanced-tutorial.html" title="iPOJO Advanced Tutorial">iPOJO Advanced Tutorial</A></LI>
465</UL>
466
467
468<H6><A name="iPOJOin10minutes-UserGuide"></A><B>User Guide</B></H6>
469<UL>
470 <LI><A href="http://felix.apache.org/site/describing-components.html" title="Describing components">Describing components</A></LI>
471 <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>
472 <LI><A href="http://felix.apache.org/site/ipojo-advanced-topics.html" title="iPOJO Advanced Topics">Advanced Topics</A></LI>
473 <LI><A href="http://felix.apache.org/site/ipojo-faq.html" title="iPOJO FAQ">FAQ</A></LI>
474</UL>
475
476
477<H6><A name="iPOJOin10minutes-Tools"></A><B>Tools</B></H6>
478<UL>
479 <LI><A href="http://felix.apache.org/site/ipojo-eclipse-plug-in.html" title="iPOJO Eclipse Plug-in">iPOJO Eclipse Plug&#45;in</A></LI>
480 <LI><A href="http://felix.apache.org/site/ipojo-ant-task.html" title="iPOJO Ant Task">iPOJO Ant Task</A></LI>
481 <LI><A href="http://felix.apache.org/site/ipojo-maven-plug-in.html" title="iPOJO Maven Plug-in">iPOJO Maven Plug&#45;in</A></LI>
482 <LI><A href="http://felix.apache.org/site/ipojo-concepts-overview.html" title="iPOJO Concepts Overview">iPOJO concepts overview</A></LI>
483</UL>
484
485
486<H6><A name="iPOJOin10minutes-DeveloperGuide"></A><B>Developer Guide</B></H6>
487<UL>
488 <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>
489 <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>
490</UL>
491
492
493<H6><A name="iPOJOin10minutes-Misc%26Contact"></A><B>Misc &amp; Contact</B></H6>
494<UL>
495 <LI><A href="http://felix.apache.org/site/apache-felix-ipojo-issuestracker.html" title="apache-felix-ipojo-issuestracker">Issues Tracker</A></LI>
496 <LI><A href="http://felix.apache.org/site/apache-felix-ipojo-supportedvms.html" title="apache-felix-ipojo-supportedVMs">Supported JVMs</A></LI>
497 <LI><A href="http://felix.apache.org/site/apache-felix-ipojo-supportedosgi.html" title="apache-felix-ipojo-supportedOSGi">Supported OSGi Implementations</A></LI>
498 <LI><A href="http://felix.apache.org/site/future-ideas.html" title="Future Ideas">Future Ideas</A></LI>
499 <LI><A href="http://felix.apache.org/site/contact.html" title="Contact">Contact</A></LI>
500 <LI><A href="http://felix.apache.org/site/related-works.html" title="Related Works">Related Works</A></LI>
501 <LI><A href="http://felix.apache.org/site/article-presentations.html" title="Article & Presentations">Article &amp; Presentations</A></LI>
502</UL>
503
504
505<HR>
506<DIV class="" align="center">
507<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="http://cwiki.apache.org/confluence/images/icons/feed-icon-32x32.png" align="absmiddle" border="0"><SUP><IMG class="rendericon" src="http://cwiki.apache.org/confluence/images/icons/linkext7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN></P></DIV></TD></TR></TBODY></TABLE>
508 </DIV>
509 </BODY>
510
511
512</HTML>