blob: a6615288d309c50e331c48fdba873a42e057b62a [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE- 2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<head>
<title>Apache Felix - iPOJO in 10 minutes</title>
<link rel="icon" href="/res/favicon.ico">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="The most powerful component model for OSGi">
<link href="/ipojo/web/bootstrap/css/bootstrap-cerulean.css" rel="stylesheet">
<link href="/ipojo/web/bootstrap/css/bootstrap-responsive.css" rel="stylesheet">
<link href="/ipojo/web/bootstrap/css/font-awesome.min.css" rel="stylesheet">
<link href="/ipojo/web/style.css" rel="stylesheet">
<!-- Overide alert's colors -->
<link href="/ipojo/web/bootstrap/css/alert.css" rel="stylesheet">
<link rel="stylesheet" href="/ipojo/web/github.css" type="text/css" media="all">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="/ipojo/web/bootstrap/js/bootstrap.min.js"></script>
</head>
<body data-spy="scroll" data-target=".subnav">
<div class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="/documentation/subprojects/apache-felix-ipojo.html">Apache Felix iPOJO</a>
<div class="nav-collapse" id="main-menu">
<ul class="nav" id="main-menu-left">
<li><a href="/documentation/subprojects/apache-felix-ipojo/ipojo-news.html">News</a></li>
<li><a href="http://felix.apache.org/downloads.cgi">Downloads</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Tutorials <b class="caret"></b></a>
<ul class="dropdown-menu" id="tutorials-menu">
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-why-choose-ipojo.html">Why choose iPOJO</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-successstories.html">Success stories</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-feature-overview.html">Features</a></li>
<li class="divider"></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-gettingstarted/ipojo-in-10-minutes.html">iPOJO in 10 minutes</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-gettingstarted/how-to-use-ipojo-annotations.html">Using Annotations</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-gettingstarted/ipojo-hello-word-maven-based-tutorial.html">Maven tutorial</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-gettingstarted/ipojo-advanced-tutorial.html">Advanced tutorial</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-gettingstarted/apache-felix-ipojo-dosgi.html">Using Distributed OSGi</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-gettingstarted/ipojo-composition-tutorial.html">Application Composition</a></li>
</ul>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Documentation <b class="caret"></b></a>
<ul class="dropdown-menu" id="user-guide-menu">
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/service-requirement-handler.html">Requiring a service</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/providing-osgi-services.html">Providing a service</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/lifecycle-callback-handler.html">Lifecycle management</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/configuration-handler.html">Configuration</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/architecture-handler.html">Introspection</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/controller-lifecycle-handler.html">Impacting the lifecycle</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/injecting-bundle-context.html">Accessing the Bundle Context</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/apache-felix-ipojo-instances.html">Creating instances</a></li>
<li class="divider"></li>
<li class="dropdown-submenu">
<a tabindex="-1" href="#">External <em>handlers</em></a>
<ul class="dropdown-menu">
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/event-admin-handlers.html">Asynchronous communication</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/ipojo-jmx-handler.html">JMX management</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/extender-pattern-handler.html">Extender pattern</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/white-board-pattern-handler.html">Whiteboard pattern</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/temporal-service-dependency.html">Temporal dependencies</a></li>
</ul>
</li>
<li class="dropdown-submenu">
<a tabindex="-1" href="#">Configuration Admin &amp; Factories</a>
<ul class="dropdown-menu">
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/combining-ipojo-and-configuration-admin.html">iPOJO and config admin</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/ipojo-factory-service.html">Using the iPOJO Factory service</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/how-to-use-ipojo-factories.html">Factories and Instances</a></li>
</ul>
</li>
<li class="divider"></li>
<li class="dropdown-submenu">
<a tabindex="-1" href="#">Advanced topics</a>
<ul class="dropdown-menu">
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/instance-vs-service-controller.html">Instance vs. Service Controllers</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/service-binding-interceptors.html">Service Binding Interceptors</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/using-xml-schemas.html">XML Schemas</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/apache-felix-ipojo-api.html">Using the iPOJO API</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/constructing-pojo-objects-with-factory-methods.html">Constructing service objects with factory methods</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/using-ipojo-introspection-api.html">Using the introspection API</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-testing-components.html">Testing components</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/using-stereotypes.html">Using @Stereotypes</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-eclipse-integration.html">Eclipse Integration</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/ipojo-extender-configuration.html">Configuring iPOJO's Extender</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-faq.html">FAQ</a></li>
<li class="divider"></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-devguide/how-to-write-your-own-handler.html">Handler development</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-devguide/how-to-use-ipojo-manipulation-metadata.html">Manipulation Metadata </a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-devguide/dive-into-the-ipojo-manipulation-depths.html">Dive into the iPOJO Manipulation depths</a></li>
<li><a href="http://felix.apache.org/ipojo/api/1.12.1">Javadoc</a></li>
</ul>
</li>
</ul>
</li>
<li class="dropdown" id="tools-menu">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Tools <b class="caret"></b></a>
<ul class="dropdown-menu" id="swatch-menu">
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-tools/ipojo-ant-task.html">Ant Task</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-tools/ipojo-maven-plug-in.html">Maven Plugin</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-tools/ipojo-arch-command.html">Architecture commands</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-tools/apache-felix-ipojo-online-manipulator.html">Online Manipulator</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-tools/ipojo-webconsole-plugin.html">Webconsole plugin</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-tools/ipojo-karaf-feature.html">Apache Karaf Features</a></li>
</ul>
</li>
<li class="dropdown" id="community-menu">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Community <b class="caret"></b></a>
<ul class="dropdown-menu" id="swatch-menu">
<li><a href="/documentation/subprojects/apache-felix-ipojo/ipojo-support.html">Support</a></li>
<li><a href="http://www.apache.org/">ASF</a></li>
<li><a href="http://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li>
<li><a href="http://www.apache.org/foundation/thanks.html">Sponsors</a></li>
</ul>
</li>
<li class="dropdown" id="misc-menu">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Misc <b class="caret"></b></a>
<ul class="dropdown-menu" id="swatch-menu">
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-supportedvms.html">Supported JVMs</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-supportedosgi.html">Supported OSGi Implementations</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/articles-and-presentations.html">Article & Presentations</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/developing-camel-mediators-with-ipojo.html">Developping Camel mediators with iPOJO</a></li>
</ul>
</li>
</ul>
<ul class="nav pull-right" id="main-menu-right">
<li><a rel="tooltip" target="_blank" href="http://felix.apache.org">Apache Felix <i class="icon-share-alt"></i></a></li>
</ul>
</div>
</div>
</div>
</div>
<div class="container">
<div class="content">
<style type="text/css">
/* The following code is added by mdx_elementid.py
It was originally lifted from http://subversion.apache.org/style/site.css */
/*
* Hide class="elementid-permalink", except when an enclosing heading
* has the :hover property.
*/
.headerlink, .elementid-permalink {
visibility: hidden;
}
h2:hover > .headerlink, h3:hover > .headerlink, h1:hover > .headerlink, h6:hover > .headerlink, h4:hover > .headerlink, h5:hover > .headerlink, dt:hover > .elementid-permalink { visibility: visible }</style>
<h1 id="ipojo-in-10-minutes">iPOJO in 10 minutes<a class="headerlink" href="#ipojo-in-10-minutes" title="Permanent link">&para;</a></h1>
<p>This page presents how to use the iPOJO 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. This tutorial uses annotations to describe components. However, you can also use XML or a programmatic API to create iPOJO applications.</p>
<h2 id="introduction">Introduction<a class="headerlink" href="#introduction" title="Permanent link">&para;</a></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, simplifying 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., POJOs) from the non-functional code (i.e., dependency management, service provision, configuration, etc.). At run time, iPOJO combines the functional and non-functional aspects. To achieve this, iPOJO provides a simple and extensible service component model based on POJOs.</p>
<h2 id="the-pojo-concept">The POJO concept<a class="headerlink" href="#the-pojo-concept" title="Permanent link">&para;</a></h2>
<p>"POJO" is just 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 a developer's perspective, the iPOJO framework strives as much as possible to only require POJOs.</p>
<h2 id="ipojo-service-component-overview">iPOJO service component overview<a class="headerlink" href="#ipojo-service-component-overview" title="Permanent link">&para;</a></h2>
<p>A service component is able to provide and/or require services, where a service is an object that implements a given Java interface. In addition, iPOJO introduces a callback concept to notify a component about various state changes.</p>
<p>The component is a 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. After components, the next most important concept in iPOJO is the component instance. A component instance is a special <em>version</em> of a component. By merging component metadata and instance configuration, the iPOJO runtime is able to discover and inject required services, publish provided services, and manage the component's life cycle.</p>
<h2 id="a-simple-example">A simple example<a class="headerlink" href="#a-simple-example" title="Permanent link">&para;</a></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 id="presentation-of-the-spell-application">Presentation of the <em>Spell</em> application<a class="headerlink" href="#presentation-of-the-spell-application" title="Permanent link">&para;</a></h3>
<p>To illustrate iPOJO features, we will implement a very simple application. This application is composed by three components:
<em> A component implementing a dictionary service
</em> A component requiring the dictionary service and providing a spellchecker service
* A component requiring the spellchecker and providing an user interface</p>
<p><img src="spell.png"></p>
<h3 id="preparing-the-tutorial">Preparing the tutorial<a class="headerlink" href="#preparing-the-tutorial" title="Permanent link">&para;</a></h3>
<p>This tutorial is based on Ant. So, you need to have the Ant program accessible in your path (see <a href="http://ant.apache.org/">here</a> to download and install Ant). Download the tutorial archive available <a href="http://repo1.maven.org/maven2/org/apache/felix/org.apache.felix.ipojo.distribution.10mintutorial/1.12.1/org.apache.felix.ipojo.distribution.10mintutorial-1.12.1.zip">here</a> and then unzip it. The archive contains 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 id="the-spellservices-project">The spell.services project<a class="headerlink" href="#the-spellservices-project" title="Permanent link">&para;</a></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 open the file "src/spell/services/DictionaryService.java". It's a very simple service interface with one method:</p>
<div class="codehilite"><pre><span class="kn">package</span> <span class="n">spell</span><span class="o">.</span><span class="na">services</span><span class="o">;</span>
<span class="cm">/**</span>
<span class="cm"> * A simple service interface that defines a dictionary service.</span>
<span class="cm"> * A dictionary service simply verifies the existence of a word.</span>
<span class="cm">**/</span>
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">DictionaryService</span> <span class="o">{</span>
<span class="cm">/**</span>
<span class="cm"> * Check for the existence of a word.</span>
<span class="cm"> * @param word the word to be checked.</span>
<span class="cm"> * @return true if the word is in the dictionary,</span>
<span class="cm"> * false otherwise.</span>
<span class="cm"> **/</span>
<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">checkWord</span><span class="o">(</span><span class="n">String</span> <span class="n">word</span><span class="o">);</span>
<span class="o">}</span>
</pre></div>
<p>Then, open the file <code>src/spell/services/SpellChecker.java</code>, and replace the <code>TODO</code> comment with for the following <code>check</code> method:</p>
<div class="codehilite"><pre><span class="kn">package</span> <span class="n">spell</span><span class="o">.</span><span class="na">services</span><span class="o">;</span>
<span class="cm">/**</span>
<span class="cm"> * A simple service interface that defines a spell checker service.</span>
<span class="cm"> * A spell checker service checks the spelling of all words in a</span>
<span class="cm"> * given passage. A passage is any number of words separated by</span>
<span class="cm"> * a space character and the following punctuation marks: comma,</span>
<span class="cm"> * period, exclamation mark, question mark, semi-colon, and colon.</span>
<span class="cm">**/</span>
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">SpellChecker</span> <span class="o">{</span>
<span class="cm">/**</span>
<span class="cm"> * Checks a given passage for spelling errors. A passage is any</span>
<span class="cm"> * number of words separated by a space and any of the following</span>
<span class="cm"> * punctuation marks: comma (,), period (.), exclamation mark (!),</span>
<span class="cm"> * question mark (?), semi-colon (;), and colon(:).</span>
<span class="cm"> * @param passage the passage to spell check.</span>
<span class="cm"> * @return An array of misspelled words or null if no</span>
<span class="cm"> * words are misspelled.</span>
<span class="cm"> **/</span>
<span class="kd">public</span> <span class="n">String</span><span class="o">[]</span> <span class="nf">check</span><span class="o">(</span><span class="n">String</span> <span class="n">passage</span><span class="o">);</span>
<span class="o">}</span>
</pre></div>
<p>Once created, you can build the project by launching Ant from the project directory.</p>
<div class="codehilite"><pre><span class="nv">$ </span>ant
Buildfile: build.xml
clean:
compile:
<span class="o">[</span>mkdir<span class="o">]</span> Created dir: d:<span class="se">\c</span>lement<span class="se">\w</span>orkspaces<span class="se">\s</span>andbox<span class="se">\i</span>pojo<span class="se">\e</span>xamples<span class="se">\t</span>utorial-ant<span class="se">\</span>
spell.services<span class="se">\o</span>utput
<span class="o">[</span>mkdir<span class="o">]</span> Created dir: d:<span class="se">\c</span>lement<span class="se">\w</span>orkspaces<span class="se">\s</span>andbox<span class="se">\i</span>pojo<span class="se">\e</span>xamples<span class="se">\t</span>utorial-ant<span class="se">\</span>
spell.services<span class="se">\o</span>utput<span class="se">\c</span>lasses
<span class="o">[</span>javac<span class="o">]</span> Compiling 2 <span class="nb">source </span>files to d:<span class="se">\c</span>lement<span class="se">\w</span>orkspaces<span class="se">\s</span>andbox<span class="se">\i</span>pojo<span class="se">\e</span>xamples<span class="se">\</span>
tutorial-ant<span class="se">\s</span>pell.services<span class="se">\o</span>utput<span class="se">\c</span>lasses
package:
<span class="o">[</span>bnd<span class="o">]</span> spell.services 2
BUILD SUCCESSFUL
Total <span class="nb">time</span>: 0 seconds
</pre></div>
<p>The created bundle is inside the output directory (spell.services.jar). The build process uses <a href="http://bnd.bndtools.org/">BND</a>. The bundle manifest is described in the spell.services.bnd file.</p>
<p>Once this project is done, we are able to implement a Dictionary service.</p>
<h3 id="the-spellenglish-project-providing-an-osgi-service">The spell.english project: Providing an OSGi service<a class="headerlink" href="#the-spellenglish-project-providing-an-osgi-service" title="Permanent link">&para;</a></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.</p>
<p>The first step is to implement the service. Go in the spell.english directory and open the "src/spell/english/EnglishDictionary.java" file. Replace its content with:</p>
<div class="codehilite"><pre><span class="kn">package</span> <span class="n">spell</span><span class="o">.</span><span class="na">english</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.felix.ipojo.annotations.Component</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.felix.ipojo.annotations.Instantiate</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.felix.ipojo.annotations.Provides</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">spell.services.DictionaryService</span><span class="o">;</span>
<span class="cm">/**</span>
<span class="cm"> * An implementation of the Dictionary service containing English words</span>
<span class="cm"> * see DictionaryService for details of the service.</span>
<span class="cm"> **/</span>
<span class="nd">@Component</span> <span class="c1">// It&#39;s an iPOJO Component</span>
<span class="nd">@Provides</span> <span class="c1">// We provide a service</span>
<span class="nd">@Instantiate</span> <span class="c1">// We declare an instance of our component</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">EnglishDictionary</span> <span class="kd">implements</span> <span class="n">DictionaryService</span> <span class="o">{</span>
<span class="c1">// The set of words contained in the dictionary.</span>
<span class="n">String</span><span class="o">[]</span> <span class="n">dictionary</span> <span class="o">=</span> <span class="o">{</span> <span class="s">&quot;welcome&quot;</span><span class="o">,</span> <span class="s">&quot;to&quot;</span><span class="o">,</span> <span class="s">&quot;the&quot;</span><span class="o">,</span> <span class="s">&quot;ipojo&quot;</span><span class="o">,</span> <span class="s">&quot;tutorial&quot;</span> <span class="o">};</span>
<span class="cm">/**</span>
<span class="cm"> * Implements DictionaryService.checkWord(). Determines</span>
<span class="cm"> * if the passed in word is contained in the dictionary.</span>
<span class="cm"> * @param word the word to be checked.</span>
<span class="cm"> * @return true if the word is in the dictionary,</span>
<span class="cm"> * false otherwise.</span>
<span class="cm"> **/</span>
<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">checkWord</span><span class="o">(</span><span class="n">String</span> <span class="n">word</span><span class="o">)</span> <span class="o">{</span>
<span class="n">word</span> <span class="o">=</span> <span class="n">word</span><span class="o">.</span><span class="na">toLowerCase</span><span class="o">();</span>
<span class="c1">// This is very inefficient</span>
<span class="k">for</span> <span class="o">(</span><span class="n">String</span> <span class="n">dict</span> <span class="o">:</span> <span class="n">dictionary</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="n">dict</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="n">word</span><span class="o">))</span> <span class="o">{</span>
<span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="k">return</span> <span class="kc">false</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
</pre></div>
<p>Notice that this class does not contains neither OSGi nor iPOJO specific code except a few annotations. It is just an implementation of the Dictionary Service interface.</p>
<p>The <code>@Component</code> annotation is used to declare an iPOJO component. The <code>@Provides</code> annotation indicates that the component provides a service. Provided service interfaces are computed by iPOJO, so it is not necessary to specify them. Finally, the <code>@Instantiate</code> annotation instructs iPOJO to create an instance of our component. The relation between components and instances is the same than between classes and objects in the object-oriented programming.</p>
<p>Then, we are able to create the bundle. In the spell.english directory launch the ant command:</p>
<div class="codehilite"><pre><span class="nv">$ </span>ant
Buildfile: /Users/clement/Projects/felix-trunk/ipojo/distributions/ten-minutes-tutorial/target/test/spell.english/build.xml
clean:
buildclasspath:
<span class="o">[</span>copy<span class="o">]</span> Copying 1 file to /Users/clement/Projects/felix-trunk/ipojo/distributions/ten-minutes-tutorial/target/test/spell.english/libs
<span class="o">[</span>copy<span class="o">]</span> Copying 1 file to /Users/clement/Projects/felix-trunk/ipojo/distributions/ten-minutes-tutorial/target/test/spell.english/libs
compile:
<span class="o">[</span>mkdir<span class="o">]</span> Created dir: /Users/clement/Projects/felix-trunk/ipojo/distributions/ten-minutes-tutorial/target/test/spell.english/output
<span class="o">[</span>mkdir<span class="o">]</span> Created dir: /Users/clement/Projects/felix-trunk/ipojo/distributions/ten-minutes-tutorial/target/test/spell.english/output/classes
<span class="o">[</span>javac<span class="o">]</span> /Users/clement/Projects/felix-trunk/ipojo/distributions/ten-minutes-tutorial/target/test/spell.english/build.xml:57: warning: <span class="s1">&#39;includeantruntime&#39;</span> was not <span class="nb">set</span>, defaulting to build.sysclasspath<span class="o">=</span>last; <span class="nb">set </span>to <span class="nb">false </span><span class="k">for </span>repeatable builds
<span class="o">[</span>javac<span class="o">]</span> Compiling 1 <span class="nb">source </span>file to /Users/clement/Projects/felix-trunk/ipojo/distributions/ten-minutes-tutorial/target/test/spell.english/output/classes
package:
<span class="o">[</span>bnd<span class="o">]</span> <span class="c"># addAll &#39;output/classes&#39; with :,</span>
<span class="o">[</span>bnd<span class="o">]</span> <span class="c"># addAll &#39;spell.english.bnd&#39; with ,</span>
<span class="o">[</span>bnd<span class="o">]</span> Updating classpath after classpathref setting
<span class="o">[</span>bnd<span class="o">]</span> <span class="c"># spell.english (spell.english.jar) 1</span>
<span class="o">[</span>ipojo<span class="o">]</span> Input bundle file : /Users/clement/Projects/felix-trunk/ipojo/distributions/ten-minutes-tutorial/target/test/spell.english/output/spell.english.jar
<span class="o">[</span>ipojo<span class="o">]</span> No metadata file found - trying to use only annotations
<span class="o">[</span>ipojo<span class="o">]</span> Start manipulation
Apache Felix iPOJO Manipulator - 1.9.0-SNAPSHOT
<span class="o">[</span>ipojo<span class="o">]</span> Bundle manipulation - SUCCESS
<span class="o">[</span>ipojo<span class="o">]</span> Output file : /Users/clement/Projects/felix-trunk/ipojo/distributions/ten-minutes-tutorial/target/test/spell.english/output/spell.english.jar
BUILD SUCCESSFUL
Total <span class="nb">time</span>: 0 seconds
</pre></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 <code>spell.english.bnd</code> file.</p>
<h3 id="the-spellchecker-project-requiring-an-osgi-service">The spell.checker project: Requiring an OSGi service<a class="headerlink" href="#the-spellchecker-project-requiring-an-osgi-service" title="Permanent link">&para;</a></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, go the the spell.checker directory and open the file <code>src/spell/checker/SpellCheck.java</code>. Replace its content with:</p>
<div class="codehilite"><pre><span class="kn">package</span> <span class="n">spell</span><span class="o">.</span><span class="na">checker</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.felix.ipojo.annotations.Component</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.felix.ipojo.annotations.Instantiate</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.felix.ipojo.annotations.Provides</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.felix.ipojo.annotations.Requires</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">spell.services.DictionaryService</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">spell.services.SpellChecker</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.util.ArrayList</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.util.List</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.util.StringTokenizer</span><span class="o">;</span>
<span class="nd">@Component</span>
<span class="nd">@Provides</span>
<span class="nd">@Instantiate</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">SpellCheck</span> <span class="kd">implements</span> <span class="n">SpellChecker</span> <span class="o">{</span>
<span class="nd">@Requires</span> <span class="c1">// This is a service dependency.</span>
<span class="kd">private</span> <span class="n">DictionaryService</span> <span class="n">dictionary</span><span class="o">;</span>
<span class="cm">/**</span>
<span class="cm"> * Implements SpellChecker.check(). Checks the given passage for misspelled words.</span>
<span class="cm"> *</span>
<span class="cm"> * @param passage the passage to spell check.</span>
<span class="cm"> * @return An array of misspelled words or null if no words are misspelled.</span>
<span class="cm"> */</span>
<span class="kd">public</span> <span class="n">String</span><span class="o">[]</span> <span class="nf">check</span><span class="o">(</span><span class="n">String</span> <span class="n">passage</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// No misspelled words for an empty string.</span>
<span class="k">if</span> <span class="o">((</span><span class="n">passage</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">||</span> <span class="o">(</span><span class="n">passage</span><span class="o">.</span><span class="na">length</span><span class="o">()</span> <span class="o">==</span> <span class="mi">0</span><span class="o">))</span> <span class="o">{</span>
<span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
<span class="o">}</span>
<span class="n">List</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span> <span class="n">errorList</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;();</span>
<span class="c1">// Tokenize the passage using spaces and punctuation.</span>
<span class="n">StringTokenizer</span> <span class="n">st</span> <span class="o">=</span> <span class="k">new</span> <span class="n">StringTokenizer</span><span class="o">(</span><span class="n">passage</span><span class="o">,</span> <span class="s">&quot; ,.!?;:&quot;</span><span class="o">);</span>
<span class="c1">// Loop through each word in the passage.</span>
<span class="k">while</span> <span class="o">(</span><span class="n">st</span><span class="o">.</span><span class="na">hasMoreTokens</span><span class="o">())</span> <span class="o">{</span>
<span class="n">String</span> <span class="n">word</span> <span class="o">=</span> <span class="n">st</span><span class="o">.</span><span class="na">nextToken</span><span class="o">();</span>
<span class="c1">// Check the current word.</span>
<span class="k">if</span> <span class="o">(!</span><span class="n">dictionary</span><span class="o">.</span><span class="na">checkWord</span><span class="o">(</span><span class="n">word</span><span class="o">))</span> <span class="o">{</span>
<span class="c1">// If the word is not correct, then add it</span>
<span class="c1">// to the incorrect word list.</span>
<span class="n">errorList</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">word</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="c1">// Return null if no words are incorrect.</span>
<span class="k">if</span> <span class="o">(</span><span class="n">errorList</span><span class="o">.</span><span class="na">size</span><span class="o">()</span> <span class="o">==</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
<span class="o">}</span>
<span class="c1">// Return the array of incorrect words.</span>
<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;Wrong words:&quot;</span> <span class="o">+</span> <span class="n">errorList</span><span class="o">);</span>
<span class="k">return</span> <span class="n">errorList</span><span class="o">.</span><span class="na">toArray</span><span class="o">(</span><span class="k">new</span> <span class="n">String</span><span class="o">[</span><span class="n">errorList</span><span class="o">.</span><span class="na">size</span><span class="o">()]);</span>
<span class="o">}</span>
<span class="o">}</span>
</pre></div>
<p>This class implements the SpellChecker service interface as it provides it. Moreover, it has a <em>special</em> field <code>dictionary</code>. This field represents the required service. iPOJO injects 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 and described using annotations. When the used dictionary service leaves, iPOJO tries 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>The @Component, @Instantiate and @Provides annotations were already presented. The <code>@Requires</code> annotation specifies a service dependency. This example shows field injection, but iPOJO also supports constructor injection and method injection (with <code>@Bind</code> and <code>@Unbind</code>).</p>
<p>Finally, we are able to build the bundle. As for previous projects, launch Ant from the project directory.</p>
<h3 id="the-spellcheckergui-project">The spell.checker.gui project<a class="headerlink" href="#the-spellcheckergui-project" title="Permanent link">&para;</a></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>Go to the spell.checker.gui directory. Open the <code>src\spell\gui\SpellCheckerGui.java</code>. Replace its content with:</p>
<div class="codehilite"><pre><span class="kn">package</span> <span class="n">spell</span><span class="o">.</span><span class="na">gui</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.felix.ipojo.annotations.*</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">spell.services.SpellChecker</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">javax.swing.*</span><span class="o">;</span>
<span class="cm">/**</span>
<span class="cm"> * A very simple Gui interacting with the CheckSpeller service</span>
<span class="cm"> */</span>
<span class="nd">@Component</span>
<span class="nd">@Instantiate</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">SpellCheckerGui</span> <span class="kd">extends</span> <span class="n">JFrame</span> <span class="o">{</span>
<span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">long</span> <span class="n">serialVersionUID</span> <span class="o">=</span> <span class="mi">1L</span><span class="o">;</span>
<span class="cm">/**</span>
<span class="cm"> * Swing component where the user write the passage to check.</span>
<span class="cm"> */</span>
<span class="kd">private</span> <span class="n">JTextField</span> <span class="n">passage</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
<span class="cm">/**</span>
<span class="cm"> * Area where the result is displayed.</span>
<span class="cm"> */</span>
<span class="kd">private</span> <span class="n">JLabel</span> <span class="n">result</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
<span class="cm">/**</span>
<span class="cm"> * Service dependency on the SpellChecker.</span>
<span class="cm"> */</span>
<span class="nd">@Requires</span>
<span class="kd">private</span> <span class="n">SpellChecker</span> <span class="n">checker</span><span class="o">;</span>
<span class="cm">/**</span>
<span class="cm"> * Constructor.</span>
<span class="cm"> * Initialize the GUI.</span>
<span class="cm"> */</span>
<span class="kd">public</span> <span class="nf">SpellCheckerGui</span><span class="o">()</span> <span class="o">{</span>
<span class="kd">super</span><span class="o">();</span>
<span class="n">initComponents</span><span class="o">();</span>
<span class="k">this</span><span class="o">.</span><span class="na">setTitle</span><span class="o">(</span><span class="s">&quot;Spellchecker Gui&quot;</span><span class="o">);</span>
<span class="o">}</span>
<span class="cm">/**</span>
<span class="cm"> * Initialize the Swing Gui.</span>
<span class="cm"> */</span>
<span class="kd">private</span> <span class="kt">void</span> <span class="nf">initComponents</span><span class="o">()</span> <span class="o">{</span>
<span class="n">java</span><span class="o">.</span><span class="na">awt</span><span class="o">.</span><span class="na">GridBagConstraints</span> <span class="n">gridBagConstraints</span><span class="o">;</span>
<span class="c1">// The check button</span>
<span class="n">JButton</span> <span class="n">checkButton</span> <span class="o">=</span> <span class="k">new</span> <span class="n">JButton</span><span class="o">();</span>
<span class="n">result</span> <span class="o">=</span> <span class="k">new</span> <span class="n">JLabel</span><span class="o">();</span>
<span class="n">passage</span> <span class="o">=</span> <span class="k">new</span> <span class="n">JTextField</span><span class="o">();</span>
<span class="n">setDefaultCloseOperation</span><span class="o">(</span><span class="n">javax</span><span class="o">.</span><span class="na">swing</span><span class="o">.</span><span class="na">WindowConstants</span><span class="o">.</span><span class="na">EXIT_ON_CLOSE</span><span class="o">);</span> <span class="c1">// Stop Felix...</span>
<span class="n">getContentPane</span><span class="o">().</span><span class="na">setLayout</span><span class="o">(</span><span class="k">new</span> <span class="n">java</span><span class="o">.</span><span class="na">awt</span><span class="o">.</span><span class="na">GridBagLayout</span><span class="o">());</span>
<span class="n">checkButton</span><span class="o">.</span><span class="na">setText</span><span class="o">(</span><span class="s">&quot;Check&quot;</span><span class="o">);</span>
<span class="n">checkButton</span><span class="o">.</span><span class="na">addActionListener</span><span class="o">(</span><span class="k">new</span> <span class="n">java</span><span class="o">.</span><span class="na">awt</span><span class="o">.</span><span class="na">event</span><span class="o">.</span><span class="na">ActionListener</span><span class="o">()</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">actionPerformed</span><span class="o">(</span><span class="n">java</span><span class="o">.</span><span class="na">awt</span><span class="o">.</span><span class="na">event</span><span class="o">.</span><span class="na">ActionEvent</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
<span class="n">check</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">});</span>
<span class="n">gridBagConstraints</span> <span class="o">=</span> <span class="k">new</span> <span class="n">java</span><span class="o">.</span><span class="na">awt</span><span class="o">.</span><span class="na">GridBagConstraints</span><span class="o">();</span>
<span class="n">gridBagConstraints</span><span class="o">.</span><span class="na">gridx</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span>
<span class="n">gridBagConstraints</span><span class="o">.</span><span class="na">gridy</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span>
<span class="n">gridBagConstraints</span><span class="o">.</span><span class="na">insets</span> <span class="o">=</span> <span class="k">new</span> <span class="n">java</span><span class="o">.</span><span class="na">awt</span><span class="o">.</span><span class="na">Insets</span><span class="o">(</span><span class="mi">2</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="mi">2</span><span class="o">);</span>
<span class="n">getContentPane</span><span class="o">().</span><span class="na">add</span><span class="o">(</span><span class="n">checkButton</span><span class="o">,</span> <span class="n">gridBagConstraints</span><span class="o">);</span>
<span class="n">result</span><span class="o">.</span><span class="na">setPreferredSize</span><span class="o">(</span><span class="k">new</span> <span class="n">java</span><span class="o">.</span><span class="na">awt</span><span class="o">.</span><span class="na">Dimension</span><span class="o">(</span><span class="mi">175</span><span class="o">,</span> <span class="mi">20</span><span class="o">));</span>
<span class="n">gridBagConstraints</span> <span class="o">=</span> <span class="k">new</span> <span class="n">java</span><span class="o">.</span><span class="na">awt</span><span class="o">.</span><span class="na">GridBagConstraints</span><span class="o">();</span>
<span class="n">gridBagConstraints</span><span class="o">.</span><span class="na">gridx</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span>
<span class="n">gridBagConstraints</span><span class="o">.</span><span class="na">gridy</span> <span class="o">=</span> <span class="mi">2</span><span class="o">;</span>
<span class="n">gridBagConstraints</span><span class="o">.</span><span class="na">fill</span> <span class="o">=</span> <span class="n">java</span><span class="o">.</span><span class="na">awt</span><span class="o">.</span><span class="na">GridBagConstraints</span><span class="o">.</span><span class="na">HORIZONTAL</span><span class="o">;</span>
<span class="n">gridBagConstraints</span><span class="o">.</span><span class="na">insets</span> <span class="o">=</span> <span class="k">new</span> <span class="n">java</span><span class="o">.</span><span class="na">awt</span><span class="o">.</span><span class="na">Insets</span><span class="o">(</span><span class="mi">2</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="mi">2</span><span class="o">);</span>
<span class="n">getContentPane</span><span class="o">().</span><span class="na">add</span><span class="o">(</span><span class="n">result</span><span class="o">,</span> <span class="n">gridBagConstraints</span><span class="o">);</span>
<span class="n">passage</span><span class="o">.</span><span class="na">setPreferredSize</span><span class="o">(</span><span class="k">new</span> <span class="n">java</span><span class="o">.</span><span class="na">awt</span><span class="o">.</span><span class="na">Dimension</span><span class="o">(</span><span class="mi">175</span><span class="o">,</span> <span class="mi">20</span><span class="o">));</span>
<span class="n">gridBagConstraints</span> <span class="o">=</span> <span class="k">new</span> <span class="n">java</span><span class="o">.</span><span class="na">awt</span><span class="o">.</span><span class="na">GridBagConstraints</span><span class="o">();</span>
<span class="n">gridBagConstraints</span><span class="o">.</span><span class="na">gridx</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span>
<span class="n">gridBagConstraints</span><span class="o">.</span><span class="na">gridy</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span>
<span class="n">gridBagConstraints</span><span class="o">.</span><span class="na">fill</span> <span class="o">=</span> <span class="n">java</span><span class="o">.</span><span class="na">awt</span><span class="o">.</span><span class="na">GridBagConstraints</span><span class="o">.</span><span class="na">HORIZONTAL</span><span class="o">;</span>
<span class="n">gridBagConstraints</span><span class="o">.</span><span class="na">insets</span> <span class="o">=</span> <span class="k">new</span> <span class="n">java</span><span class="o">.</span><span class="na">awt</span><span class="o">.</span><span class="na">Insets</span><span class="o">(</span><span class="mi">2</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="mi">2</span><span class="o">);</span>
<span class="n">getContentPane</span><span class="o">().</span><span class="na">add</span><span class="o">(</span><span class="n">passage</span><span class="o">,</span> <span class="n">gridBagConstraints</span><span class="o">);</span>
<span class="n">pack</span><span class="o">();</span>
<span class="o">}</span>
<span class="cm">/**</span>
<span class="cm"> * Check Button action.</span>
<span class="cm"> * Collects the user input and checks it.</span>
<span class="cm"> */</span>
<span class="kd">private</span> <span class="kt">void</span> <span class="nf">check</span><span class="o">()</span> <span class="o">{</span>
<span class="n">String</span><span class="o">[]</span> <span class="n">result</span> <span class="o">=</span> <span class="n">checker</span><span class="o">.</span><span class="na">check</span><span class="o">(</span><span class="n">passage</span><span class="o">.</span><span class="na">getText</span><span class="o">());</span>
<span class="k">if</span> <span class="o">(</span><span class="n">result</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="na">result</span><span class="o">.</span><span class="na">setText</span><span class="o">(</span><span class="n">result</span><span class="o">.</span><span class="na">length</span> <span class="o">+</span> <span class="s">&quot; word(s) are misspelled&quot;</span><span class="o">);</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="na">result</span><span class="o">.</span><span class="na">setText</span><span class="o">(</span><span class="s">&quot;All words are correct&quot;</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="cm">/**</span>
<span class="cm"> * Start callback.</span>
<span class="cm"> * This method will be called when the instance becomes valid.</span>
<span class="cm"> * It set the Gui visibility to true.</span>
<span class="cm"> */</span>
<span class="nd">@Validate</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">start</span><span class="o">()</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="na">setVisible</span><span class="o">(</span><span class="kc">true</span><span class="o">);</span>
<span class="o">}</span>
<span class="cm">/**</span>
<span class="cm"> * Stop callback.</span>
<span class="cm"> * This method will be called when the instance becomes invalid or stops.</span>
<span class="cm"> * It deletes the Gui.</span>
<span class="cm"> */</span>
<span class="nd">@Invalidate</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">stop</span><span class="o">()</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="na">dispose</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
</pre></div>
<p>Look at the three last methods. The <em>check</em> methods collects the user input and uses a <em>Check speller</em> service to check this input. The speller is injected into the <code>checker</code> field thanks to the <code>@Requires</code> annotation. This method is called when the user presses the button. The <em>start</em> and <em>stop</em> methods are lifecycle callbacks. As we display the user interface when the instance is created and to dispose it when the instance stops, we need a way to be notified when we need to execute these actions. iPOJO provides an easy way to do this. The component provides two callback methods for its activation and deactivation. Callbacks are used when the component needs to be informed about a component state change. In iPOJO, the component state is either <em>INVALID</em> (i.e., not all of the component's constraints are satisfied) or <em>VALID</em> (i.e., all of the component's constraints are satisfied). In this example, the start callback method sets the GUI visibility to true; the stop callback method deletes the GUI. The <code>@Validate</code> and <code>@Invalidate</code> annotations are used to specify these callbacks.</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 id="running-the-application">Running the application<a class="headerlink" href="#running-the-application" title="Permanent link">&para;</a></h2>
<p>We have all the bundles required to start playing with the application.</p>
<p>To run the example, start Felix. A distribution of Felix is provided in the felix-framework-VERSION 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="codehilite"><pre>java -jar bin/felix.jar
</pre></div>
<p>You can check installed bundles by using the '<em>lb</em>' command:</p>
<div class="codehilite"><pre>____________________________
Welcome to Apache Felix Gogo
g! lb
START LEVEL 1
ID|State |Level|Name
0|Active | 0|System Bundle <span class="o">(</span>4.2.1<span class="o">)</span>
1|Active | 1|Apache Felix Bundle Repository <span class="o">(</span>1.6.6<span class="o">)</span>
2|Active | 1|Apache Felix Gogo Command <span class="o">(</span>0.12.0<span class="o">)</span>
3|Active | 1|Apache Felix Gogo Runtime <span class="o">(</span>0.10.0<span class="o">)</span>
4|Active | 1|Apache Felix Gogo Shell <span class="o">(</span>0.10.0<span class="o">)</span>
5|Active | 1|Apache Felix iPOJO <span class="o">(</span>1.9.0.SNAPSHOT<span class="o">)</span>
6|Active | 1|Apache Felix iPOJO Gogo Command <span class="o">(</span>1.0.1<span class="o">)</span>
g!
</pre></div>
<p>iPOJO runtime is the bundle 5. Once started, install the four created bundles as below:</p>
<div class="codehilite"><pre>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>
<p>The new set of bundles is:</p>
<div class="codehilite"><pre>g! lb
START LEVEL 1
ID|State |Level|Name
0|Active | 0|System Bundle <span class="o">(</span>4.2.1<span class="o">)</span>
1|Active | 1|Apache Felix Bundle Repository <span class="o">(</span>1.6.6<span class="o">)</span>
2|Active | 1|Apache Felix Gogo Command <span class="o">(</span>0.12.0<span class="o">)</span>
3|Active | 1|Apache Felix Gogo Runtime <span class="o">(</span>0.10.0<span class="o">)</span>
4|Active | 1|Apache Felix Gogo Shell <span class="o">(</span>0.10.0<span class="o">)</span>
5|Active | 1|Apache Felix iPOJO <span class="o">(</span>1.9.0.SNAPSHOT<span class="o">)</span>
6|Active | 1|Apache Felix iPOJO Gogo Command <span class="o">(</span>1.0.1<span class="o">)</span>
7|Active | 1|spell.services <span class="o">(</span>0.0.0<span class="o">)</span>
8|Active | 1|spell.english <span class="o">(</span>0.0.0<span class="o">)</span>
9|Active | 1|spell.checker <span class="o">(</span>0.0.0<span class="o">)</span>
10|Active | 1|spell.checker.gui <span class="o">(</span>0.0.0<span class="o">)</span>
</pre></div>
<p>iPOJO provides a command to check created instances:</p>
<div class="codehilite"><pre>g! instances
Instance org.apache.felix.ipojo.arch.gogo.Arch-0 -&gt; valid
Instance spell.checker.SpellCheck-0 -&gt; valid
Instance spell.gui.SpellCheckerGui-0 -&gt; valid
Instance spell.english.EnglishDictionary-0 -&gt; valid
</pre></div>
<p>As you can see, all our instances are valid.</p>
<p>In the gui (that should have appeared), you can interact with the spell service by entering a passage and clicking on the check button:</p>
<p><img src="ss.png"></p>
<p>Then, stop the <em>Dictionary</em> service provider (with the <em>stop 8</em>) command. The GUI disappears. Indeed, Spell Checker service cannot be provided as it depends on the Dictionary service.</p>
<p><img src="spell2.png"></p>
<p>You can check the validity of the instances and see that the SpellChecker and the Gui are invalid.</p>
<div class="codehilite"><pre>g! instances
Instance org.apache.felix.ipojo.arch.gogo.Arch-0 -&gt; valid
Instance spell.checker.SpellCheck-0 -&gt; invalid
Instance spell.gui.SpellCheckerGui-0 -&gt; invalid
</pre></div>
<p>Then, restart the Dictionary service provider with the <em>start 8</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 9</em> command. As for the last manipulation, the GUI disappears.</p>
<p><img src="spell3.png"></p>
<p>This time, the Gui is invalid, but the English dictionary is valid:</p>
<div class="codehilite"><pre>g! instances
Instance org.apache.felix.ipojo.arch.gogo.Arch-0 -&gt; valid
Instance spell.gui.SpellCheckerGui-0 -&gt; invalid
Instance spell.english.EnglishDictionary-0 -&gt; valid
</pre></div>
<h2 id="conclusion">Conclusion<a class="headerlink" href="#conclusion" title="Permanent link">&para;</a></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 <a href="mailto:users-subscribe@felix.apache.org">users-subscribe@felix.apache.org</a>; after subscribing, email questions or feedback to <a href="mailto:users@felix.apache.org">users@felix.apache.org</a>.</p>
</div>
</div>
<hr/>
<div class="container">
<footer id="footer">
<div class="row">
<div class="trademarkFooter span7">
Apache Felix, Felix, Apache, the Apache feather logo, and the Apache Felix project
logo are trademarks of The Apache Software Foundation. All other marks mentioned
may be trademarks or registered trademarks of their respective owners.
</div>
<div class="timestamp span3 offset2">
Rev. 1762306 by cziegeler on Mon, 26 Sep 2016 09:54:12 +0000
</div>
</div>
</footer>
</div>
</body>
<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 type="text/javascript">
try{
var pageTracker = _gat._getTracker("UA-1518442-4");
pageTracker._trackPageview();
} catch(err) {}
</script>
</html>