felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From build...@apache.org
Subject svn commit: r859838 [2/3] - in /websites/staging/felix/trunk/content: ./ documentation/ documentation/subprojects/ documentation/subprojects/apache-felix-ipojo/ documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-devguide/
Date Thu, 25 Apr 2013 09:36:25 GMT
Modified: websites/staging/felix/trunk/content/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-devguide/how-to-write-your-own-handler.html
==============================================================================
--- websites/staging/felix/trunk/content/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-devguide/how-to-write-your-own-handler.html (original)
+++ websites/staging/felix/trunk/content/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-devguide/how-to-write-your-own-handler.html Thu Apr 25 09:36:24 2013
@@ -177,15 +177,16 @@
 <li><a href="#handler-metadata">Handler metadata</a></li>
 <li><a href="#handler-implementation">Handler implementation</a><ul>
 <li><a href="#loghandler-class">LogHandler class</a></li>
-<li><a href="#initializecomponentfactory-method">InitializeComponentFactory Method</a></li>
 <li><a href="#configure-method">Configure Method</a></li>
 <li><a href="#statechanged-method">StateChanged Method</a></li>
 </ul>
 </li>
+<li><a href="#start-and-stop">Start and Stop</a></li>
 </ul>
 </li>
 <li><a href="#handler-packaging">Handler packaging</a><ul>
 <li><a href="#handler-usage">Handler usage</a></li>
+<li><a href="#playing-with-the-handler">Playing with the handler</a></li>
 </ul>
 </li>
 <li><a href="#properties-handler-example">Properties Handler example</a><ul>
@@ -196,9 +197,9 @@
 <li><a href="#onget-and-onset-methods">onGet and onSet methods</a></li>
 </ul>
 </li>
+<li><a href="#creating-the-annotation">Creating the annotation</a></li>
 <li><a href="#handler-packaging_1">Handler packaging</a></li>
-<li><a href="#handler-usage_1">Handler usage</a></li>
-<li><a href="#download">Download</a></li>
+<li><a href="#playing-with-the-handler_1">Playing with the handler</a></li>
 </ul>
 </li>
 <li><a href="#advanced-topics">Advanced topics</a><ul>
@@ -214,6 +215,7 @@
 </ul>
 </div>
 <p>First, iPOJO concepts are briefly explained. The second section explains the steps to create a handler. The two last sections describes the implementation and the usage of two small example handlers : a Log Handler, logging messages inside the OSGi log service, and a Property Handler, injecting properties values inside fields.</p>
+<p>The code is available in this <a href="ipojo-handler-tutorial-project.zip">archive file</a>.</p>
 <h2 id="ipojo-concepts">iPOJO Concepts</h2>
 <p>iPOJO is a service oriented component model aiming to simplify OSGi applications development. iPOJO is based on the POJO concepts. A POJO is a simple Java class without any dependency on its runtime environment. In iPOJO, POJO are encapsulated in a container managing the relation between the POJO and the external world. This container keeps separated the POJO from the external "wild" world. Moreover, this container can be extended, using handlers.</p>
 <p>Basically, iPOJO contains two main concepts: component type and component instance. A component type is a type of component. A component type defines its implementation class, its creation policy, and its container. A component instance is a configured instance of a component type. This instance is created with the component type factory. A component instance inherits of all component type characteristics but has a unique name and its own configuration (set of &lt;key, value&gt;).</p>
@@ -231,40 +233,28 @@ iPOJO core contains 6 "core" handlers ma
 <p>As explain above, the handler interacts with the POJO, with the component's container and with the external world (e.g. : other components, services, bundles, the OSGi framework, ...). The skeleton of such an agent is defined in iPOJO is defined by the <code>PrimitiveHandler</code> (abstract) class that can be found in the <code>org.apache.felix.ipojo</code> package.</p>
 <p>You need to implement the three basic lifecycle methods of this class, but you can extends this model by redefining some other methods (e.g. : to intercept POJO method calls, field accesses, ...).</p>
 <h3 id="declaring-your-handler">Declaring your handler</h3>
-<p>You first need to declare your handler, so iPOJO will be able to initialize, configure and use it when needed. First, you must give a name and an XML namespace to your handler. By doing that, iPOJO can recognize that a certain component uses your handler, so it can initialize it. You need to declare, in the <code>metadata.xml</code> of the bundle containing your handler, the class name of your handler and its name and XML namespace. You can, of course, declare several handlers, and even declare components using these handlers, in the same bundle.
-Then, you must know that a handler is a component (almost) like standard iPOJO components : it can use other handlers (like core handlers : service requirements, provided services, ...). You can consequently describe your handler's required services, provided services, etc. in its metadata.xml, as for classic iPOJO components.</p>
-<div class="codehilite"><pre><span class="nt">&lt;ipojo&gt;</span>
- ... 
-    <span class="nt">&lt;handler</span> <span class="na">className=</span><span class="s">&quot;your.handler.class&quot;</span> 
-        <span class="na">name=</span><span class="s">&quot;HandlerName&quot;</span> 
-        <span class="na">namespace=</span><span class="s">&quot;the.namespace.of.your.handler&quot;</span><span class="nt">&gt;</span>
-        ...
-        <span class="nt">&lt;provides</span> <span class="na">interface=</span><span class="s">&quot;a.provided.Service&quot;</span><span class="nt">&gt;</span>        
-            <span class="nt">&lt;property</span> <span class="na">field=</span><span class="s">&quot;a_field&quot;</span> 
-                <span class="na">name=</span><span class="s">&quot;a.property&quot;</span> <span class="na">value=</span><span class="s">&quot;a.value&quot;</span><span class="nt">/&gt;</span>
-        <span class="nt">&lt;/provides&gt;</span>
-        ...
-    <span class="nt">&lt;/handler&gt;</span>
-<span class="nt">&lt;/ipojo&gt;</span>
+<p>You first need to declare your handler, so iPOJO will be able to initialize, configure and use it when needed. First, you must give a name and a namespace to your handler. By doing that, iPOJO can recognize that a certain component uses your handler, so it can initialize it. You need to declare, using the <code>@Handler</code> annotations. You can, of course, declare several handlers, and even declare components using these handlers, in the same bundle.</p>
+<div class="codehilite"><pre><span class="nd">@Handler</span><span class="o">(</span><span class="n">name</span> <span class="o">=</span> <span class="s">&quot;Log&quot;</span><span class="o">,</span> <span class="n">namespace</span> <span class="o">=</span> <span class="n">LogHandler</span><span class="o">.</span><span class="na">NAMESPACE</span><span class="o">)</span>
+<span class="kd">public</span> <span class="kd">class</span> <span class="nc">LogHandler</span> <span class="kd">extends</span> <span class="n">PrimitiveHandler</span> <span class="o">{</span>
+    <span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">NAMESPACE</span> <span class="o">=</span> <span class="s">&quot;org.apache.felix.ipojo.log.handler&quot;</span><span class="o">;</span>
+    <span class="c1">// ...</span>
+<span class="o">}</span>
 </pre></div>
 
 
+<p>Then, you must know that a handler is a component (almost) like standard iPOJO components : it can use other handlers (like core handlers : service requirements, provided services, ...). You can consequently describe your handler's required services, provided services, etc. </p>
 <div class="alert alert-info">
 In order to use iPOJO annotations processing, the namespace must be a valid package name and the name must be a valid annotation name (without the '@'). Refer to the <a href="#annotations">annotations</a> section.
 </div>
 
-<div class="alert alert-info">
-It is a good idea to provide a documented XML schema description (XSD) with your handler to help users to configure your handler and to validate their configurations. Refer to the <a href="#xsd">XML Schema</a> section.
-</div>
-
 <h3 id="handler-lifecycle">Handler lifecycle</h3>
 <p>A handler lifecycle is composed of four different states.</p>
 <p><img src="Handler-Lifecycle.png"></p>
 <ul>
-<li>First, when iPOJO parses the <code>metadata.xml</code> of a bundle, it detects that a certain component type use your handler (using XML qualified names, see the following ìUsing your handlerî section). When it finds such a reference, it initializes the handler by calling the <code>initializeComponentFactory()</code> method. This method should be static but actually can't be so for some technical reasons. Consequently, a ìmockî instance of the handler is created, the <code>initializeComponentFactory()</code> method is called, and this instance is destroyed. This method aims to check the validity of the component type description, avoiding starting invalid factories.If you override this method, you should here set up the component description (e.g. : common properties, exported services, ...) and check the handler configuration. The parameters passed to this method are the <code>ComponentTypeDescription</code> and the component's <code>Metadata</code> (i.e. : 
 the structure of the component type declaration).</li>
+<li>First, when iPOJO parses the metadata of a bundle, it detects that a certain component type use your handler (using XML qualified names, see the following <em>Using your handler</em> section). When it finds such a reference, it initializes the handler by calling the <code>initializeComponentFactory()</code> method. This method should be static but actually can't be so for some technical reasons. Consequently, a <em>mock</em> instance of the handler is created, the <code>initializeComponentFactory()</code> method is called, and this instance is destroyed. This method aims to check the validity of the component type description, avoiding starting invalid factories.If you override this method, you should here set up the component description (e.g. : common properties, exported services, ...) and check the handler configuration. The parameters passed to this method are the <code>ComponentTypeDescription</code> and the component's <code>Metadata</code> (i.e. : the structure o
 f the component type declaration).</li>
 <li>Once your handler has been initialized, iPOJO configures it for each created instance of the using components. The <code>ComponentTypeDescription</code> and the instance specific properties are passed to the <code>configure()</code> method of your handler.This method is mandatory so you have to implement it. Here you should check the handler configuration (if not already done in the <code>initializeComponentFactory()</code> method) and configure the handler with given instance specific properties.</li>
 <li>Then, iPOJO starts the handler, following the component instance lifecycle, by calling the <code>start()</code> method. You have to put in this method the activation code of your handler. A freshly started handler is by default in an active state (if all its used handlers, like required services, are in an active state).</li>
-<li>Once started, the handler state can be either in a valid or in an invalid state, depending on its used handlers (a handler is an iPOJO component, so it can depend on other handlers, like service dependencies, provided services, ... See the ìHandler extends Componentî section). The validity of your handler depends on used handlers status, but it also can be changed in your handler code by using the <code>setValidity()</code> method.</li>
+<li>Once started, the handler state can be either in a valid or in an invalid state, depending on its used handlers (a handler is an iPOJO component, so it can depend on other handlers, like service dependencies, provided services, ... See the <em>Handler extends Component</em> section). The validity of your handler depends on used handlers status, but it also can be changed in your handler code by using the <code>setValidity()</code> method.</li>
 <li>Finally, when the component instance is stopped (generally just before being destroyed), the stop method of the handler is called. Place here the inactivation code of your handler.</li>
 </ul>
 <div class="alert alert-warning">
@@ -273,7 +263,7 @@ Keep in mind that the <code>stop()</code
 
 <h3 id="reading-handler-and-instance-configurations">Reading handler and instance configurations</h3>
 <p>Your handler need to read how it is configured in the using component type description. The configuration is written in the <code>metadata.xml</code> of the using bundle, but is passed to the <code>initializeComponentFactory()</code> and <code>configure()</code> methods as an <code>Element</code> object.</p>
-<p>The <code>Element</code> type (placed in the <code>org.apache.felix.ipojo.metadata package</code>), coupled with the <code>Attribute</code> type, is used to retrieve the structure and the content of the component configuration. The <code>Element</code> parameter, passed to the initialization and configuration methods, represents the root of the component type description (i.e. the root of the tree is the <code>component</code> XML tag).</p>
+<p>The <code>Element</code> type (from the <code>org.apache.felix.ipojo.metadata package</code>), coupled with the <code>Attribute</code> type, is used to retrieve the structure and the content of the component configuration. The <code>Element</code> parameter, passed to the initialization and configuration methods, represents the root of the component type description (i.e. the root of the tree is the <code>component</code> tag).</p>
 <p>Several methods allows to browse the entire configuration from the root <code>Element</code> :</p>
 <ul>
 <li>The <code>getElement()</code> methods let you access the content of an <code>Element</code> (i.e. the children elements)</li>
@@ -281,23 +271,84 @@ Keep in mind that the <code>stop()</code
 <li>The <code>containsElement()</code> and <code>containsAttribute()</code> methods test the presence of a child-element or an attribute in an <code>Element</code>.</li>
 </ul>
 <p><em>Note :</em> As described in the <a href="#description">description</a> section, a name and a namespace are associated to each handler. To safely retrieve the configuration of this handler from the component metadata, you can take inspiration from the following snippet (the <code>componentMetadata</code> variable is the component root <code>Element</code> passed to the <code>initializeComponentFactory()</code> and <code>configure()</code> methods) :</p>
-<div class="codehilite"><pre> <span class="n">Element</span><span class="o">[]</span> <span class="n">handlerConfig</span> <span class="o">=</span> 
-           <span class="n">componentMetadata</span><span class="o">.</span><span class="na">getElements</span><span class="o">(</span><span class="s">&quot;HandlerName&quot;</span><span class="o">,</span>
-                               <span class="s">&quot;the.namespace.of.your.handler&quot;</span><span class="o">);</span>
+<div class="codehilite"><pre><span class="n">Element</span><span class="o">[]</span> <span class="n">log_elements</span> <span class="o">=</span> <span class="n">metadata</span><span class="o">.</span><span class="na">getElements</span><span class="o">(</span><span class="s">&quot;log&quot;</span><span class="o">,</span> <span class="n">NAMESPACE</span><span class="o">);</span>
+</pre></div>
+
+
+<p>For example, the log handler provided in the archive file has the following configure method: </p>
+<div class="codehilite"><pre><span class="cm">/**</span>
+<span class="cm"> * Parses the component&#39;s metadata to retrieve the log level in which we log messages.</span>
+<span class="cm"> *</span>
+<span class="cm"> * @param metadata      component&#39;s metadata</span>
+<span class="cm"> * @param configuration instance configuration (unused in this example)</span>
+<span class="cm"> * @throws ConfigurationException the configuration is inconsistent</span>
+<span class="cm"> */</span>
+<span class="nd">@Override</span>
+<span class="kd">public</span> <span class="kt">void</span> <span class="nf">configure</span><span class="o">(</span><span class="n">Element</span> <span class="n">metadata</span><span class="o">,</span> <span class="n">Dictionary</span> <span class="n">configuration</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">ConfigurationException</span> <span class="o">{</span>
+    <span class="c1">// First parse the metadata to check if the log handler logLevel</span>
+
+    <span class="c1">// Get all Namespace:log element from the metadata</span>
+    <span class="n">Element</span><span class="o">[]</span> <span class="n">log_elements</span> <span class="o">=</span> <span class="n">metadata</span><span class="o">.</span><span class="na">getElements</span><span class="o">(</span><span class="s">&quot;log&quot;</span><span class="o">,</span> <span class="n">NAMESPACE</span><span class="o">);</span>
+
+    <span class="c1">// If an element match, parse the logLevel attribute of the first found element</span>
+    <span class="k">if</span> <span class="o">(</span><span class="n">log_elements</span><span class="o">[</span><span class="mi">0</span><span class="o">].</span><span class="na">containsAttribute</span><span class="o">(</span><span class="s">&quot;level&quot;</span><span class="o">))</span> <span class="o">{</span>
+        <span class="n">String</span> <span class="n">l</span> <span class="o">=</span> <span class="n">log_elements</span><span class="o">[</span><span class="mi">0</span><span class="o">].</span><span class="na">getAttribute</span><span class="o">(</span><span class="s">&quot;level&quot;</span><span class="o">);</span>
+        <span class="k">if</span> <span class="o">(</span><span class="n">l</span><span class="o">.</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="s">&quot;info&quot;</span><span class="o">))</span> <span class="o">{</span>
+            <span class="n">logLevel</span> <span class="o">=</span> <span class="n">LogService</span><span class="o">.</span><span class="na">LOG_INFO</span><span class="o">;</span>
+        <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">l</span><span class="o">.</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="s">&quot;error&quot;</span><span class="o">))</span> <span class="o">{</span>
+            <span class="n">logLevel</span> <span class="o">=</span> <span class="n">LogService</span><span class="o">.</span><span class="na">LOG_ERROR</span><span class="o">;</span>
+        <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">l</span><span class="o">.</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="s">&quot;warning&quot;</span><span class="o">))</span> <span class="o">{</span>
+            <span class="n">logLevel</span> <span class="o">=</span> <span class="n">LogService</span><span class="o">.</span><span class="na">LOG_WARNING</span><span class="o">;</span>
+        <span class="o">}</span>
+    <span class="o">}</span>
+
+    <span class="n">instanceManager</span> <span class="o">=</span> <span class="n">getInstanceManager</span><span class="o">();</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>You can also access instance configuration (properties defined in the <code>instance</code> tag). The instance properties are directly passed, as a <code>Dictionary,</code> to the <code>configure()</code> method. With these properties, you can easily allow instances to override some component fixed configuration. The property handler given in the archive file extract the location of the loaded properties file from the instance configuration:</p>
+<div class="codehilite"><pre><span class="c1">// Look if the instance overrides file location :</span>
+<span class="n">String</span> <span class="n">instanceFile</span> <span class="o">=</span> <span class="o">(</span><span class="n">String</span><span class="o">)</span> <span class="n">configuration</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">&quot;properties.file&quot;</span><span class="o">);</span>
+<span class="k">if</span> <span class="o">(</span><span class="n">instanceFile</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
+    <span class="n">m_file</span> <span class="o">=</span> <span class="n">instanceFile</span><span class="o">;</span>
+<span class="o">}</span>
 </pre></div>
 
 
-<p>You will also need to read specific instance configuration (properties defined in the <code>instance</code> XML tag). The instance properties are directly passed, as a <code>Dictionary,</code> to the <code>configure()</code> method. With these properties, you can easily allow instances to override some component fixed configuration.</p>
 <h3 id="interacting-with-the-pojo">Interacting with the POJO</h3>
 <p>One of the most interesting features of an handler is the ability to interact with the component's POJO. Indeed, you can intercept method calls and returns, inject values in the POJO's fields...</p>
 <p>The <code>getPojoMetadata()</code> method of the PrimitiveHandler class lets you access the structure of the POJO (represented by the <code>PojoMetadata</code> type) without having to use (slow) reflection. It allows you to list all fields and methods of the POJO, and get informations about implemented interfaces and the super-class. The <code>PojoMetadata</code> class implements the following operations :</p>
 <ul>
 <li>The <code>getInterfaces()</code> method returns the list of implemented interfaces, while the <code>isInterfaceImplemented()</code> methods test if a given interface is implemented by the POJO.</li>
 <li>The <code>getSuperClass()</code> method returns the name of the class extended by the POJO (or <code>null</code> instead of <code>java.lang.Object</code>).</li>
-<li>The <code>getField()</code> methods lets you access the fields of the POJO. The returned object is a <code>FieldMetadata</code> that provides informations about a particular field inside the POJO.</li>
-<li>The <code>getMethod()</code> methods lets you access the methods of the POJO. The returned object is a <code>MethodMetadata</code> that provides informations about a particular method in the POJO.</li>
+<li>The <code>getField()</code> methods lets you access the fields of the POJO. The returned object is a <code>FieldMetadata</code> that provides information about a particular field inside the POJO.</li>
+<li>The <code>getMethod()</code> methods lets you access the methods of the POJO. The returned object is a <code>MethodMetadata</code> that provides information about a particular method in the POJO.</li>
 </ul>
 <p>Once you've retrieved informations about the POJO structure, you can interact with it, via the <code>InstanceManager</code>, accessible in your handler by the <code>getInstanceManager()</code> method. It allows you to register interceptors, that are called before and after POJO method calls or field accesses.</p>
+<p>The property handler is registering field interceptors on injected properties:</p>
+<div class="codehilite"><pre><span class="c1">//First get Pojo Metadata metadata :</span>
+<span class="n">PojoMetadata</span> <span class="n">pojoMeta</span> <span class="o">=</span> <span class="n">getPojoMetadata</span><span class="o">();</span>
+<span class="n">Enumeration</span> <span class="n">e</span> <span class="o">=</span> <span class="n">m_properties</span><span class="o">.</span><span class="na">keys</span><span class="o">();</span>
+<span class="k">while</span> <span class="o">(</span><span class="n">e</span><span class="o">.</span><span class="na">hasMoreElements</span><span class="o">())</span> <span class="o">{</span>
+    <span class="n">String</span> <span class="n">field</span> <span class="o">=</span> <span class="o">(</span><span class="n">String</span><span class="o">)</span> <span class="n">e</span><span class="o">.</span><span class="na">nextElement</span><span class="o">();</span>
+    <span class="n">FieldMetadata</span> <span class="n">fm</span> <span class="o">=</span> <span class="n">pojoMeta</span><span class="o">.</span><span class="na">getField</span><span class="o">(</span><span class="n">field</span><span class="o">);</span>
+
+    <span class="k">if</span> <span class="o">(</span><span class="n">fm</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// The field does not exist</span>
+        <span class="k">throw</span> <span class="k">new</span> <span class="nf">ConfigurationException</span><span class="o">(</span><span class="s">&quot;The field &quot;</span> <span class="o">+</span> <span class="n">field</span> <span class="o">+</span> <span class="s">&quot; is declared in the properties file but does not exist in the pojo&quot;</span><span class="o">);</span>
+    <span class="o">}</span>
+
+    <span class="c1">// Then check that the field is a String field</span>
+    <span class="k">if</span> <span class="o">(!</span><span class="n">fm</span><span class="o">.</span><span class="na">getFieldType</span><span class="o">().</span><span class="na">equals</span><span class="o">(</span><span class="n">String</span><span class="o">.</span><span class="na">class</span><span class="o">.</span><span class="na">getName</span><span class="o">()))</span> <span class="o">{</span>
+        <span class="k">throw</span> <span class="k">new</span> <span class="nf">ConfigurationException</span><span class="o">(</span><span class="s">&quot;The field &quot;</span> <span class="o">+</span> <span class="n">field</span> <span class="o">+</span> <span class="s">&quot; exists in the pojo, but is not a String&quot;</span><span class="o">);</span>
+    <span class="o">}</span>
+
+    <span class="c1">// All checks are ok, register the interceptor.</span>
+    <span class="n">getInstanceManager</span><span class="o">().</span><span class="na">register</span><span class="o">(</span><span class="n">fm</span><span class="o">,</span> <span class="k">this</span><span class="o">);</span>
+<span class="o">}</span>
+</pre></div>
+
+
 <div class="alert alert-info">
 The InstanceManager manages the component instance attached to your handler instance. Thus, it can't be available in the <code>initializeComponentFactory()</code> because this method is run before the creation of any component instance.
 </div>
@@ -307,6 +358,34 @@ The InstanceManager manages the componen
 <li>The <code>void onSet(Object pojo, String fieldName, Object value)</code> method: This method is called each time a field of the POJO is assigned. The first parameter is the instance of the concerned POJO, the second is the name of the accessed field and the third is the value assigned to the POJO's field. If the field type is a primitive type, this method receives the boxed object.</li>
 <li>The <code>Object onGet(Object pojo, String fieldName, Object value)</code> method : This method is called each time a field of the POJO is read. The first parameter is the instance of the concerned POJO, the second is the name of the accessed field and the third is the actual value of the POJO's field. If the field type is a primitive type, this method receives the boxed object. The returned object is the value the intercepted read process will return. It's the standard way to inject a value in the field : returning a specific object whatever the field really contains.</li>
 </ul>
+<p>The property handler contains the following <code>onGetz and</code>onSet` methods:</p>
+<div class="codehilite"><pre><span class="cm">/**</span>
+<span class="cm"> * This method is called at each time the pojo &#39;get&#39; a listened field. The method return the stored value.</span>
+<span class="cm"> * @param pojo : pojo object getting the field</span>
+<span class="cm"> * @param field : field name.</span>
+<span class="cm"> * @param o : previous value.</span>
+<span class="cm"> * @return the stored value.</span>
+<span class="cm"> */</span>
+<span class="kd">public</span> <span class="n">Object</span> <span class="nf">onGet</span><span class="o">(</span><span class="n">Object</span> <span class="n">pojo</span><span class="o">,</span> <span class="n">String</span> <span class="n">field</span><span class="o">,</span> <span class="n">Object</span> <span class="n">o</span><span class="o">)</span> <span class="o">{</span>
+    <span class="c1">// When the pojo requires a value for a managed field, this method is invoked.</span>
+    <span class="c1">// So, we have just to return the stored value.</span>
+    <span class="k">return</span> <span class="n">m_properties</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">field</span><span class="o">);</span>
+<span class="o">}</span>
+
+<span class="cm">/**</span>
+<span class="cm"> * This method is called at each time the pojo &#39;set&#39; a listened field. This method updates the local properties.</span>
+<span class="cm"> * @param pojo : pojo object setting the field</span>
+<span class="cm"> * @param field : field name</span>
+<span class="cm"> * @param newvalue : new value</span>
+<span class="cm"> */</span>
+<span class="kd">public</span> <span class="kt">void</span> <span class="nf">onSet</span><span class="o">(</span><span class="n">Object</span> <span class="n">pojo</span><span class="o">,</span> <span class="n">String</span> <span class="n">field</span><span class="o">,</span> <span class="n">Object</span> <span class="n">newvalue</span><span class="o">)</span> <span class="o">{</span>
+    <span class="c1">// When the pojo set a value to a managed field, this method is invoked.</span>
+    <span class="c1">// So, we update the stored value.</span>
+    <span class="n">m_properties</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">field</span><span class="o">,</span> <span class="n">newvalue</span><span class="o">);</span>
+<span class="o">}</span>
+</pre></div>
+
+
 <p>You need to implements some of the following methods to intercept methods accesses. When these methods are called, the first parameter is the POJO's instance on which the intercepted method is called and the second parameter contains the descriptor of the called method.</p>
 <ul>
 <li>The <code>void onEntry(Object pojo, Method method, Object[] args)</code> method: This method is called before the execution of an intercepted method. The third parameter is the list of parameters with which the method have been called. The method is executed just after the execution of the <code>onEntry()</code> callback.</li>
@@ -314,13 +393,16 @@ The InstanceManager manages the componen
 <li>The <code>void onError(Object pojo, Method method, Throwable throwable)</code> method: This method is called right after the unexpected return of an intercepted method (i.e. when an uncaught exception occurred). The third parameter is the thrown object that caused the method termination.</li>
 <li>The <code>void onFinally(Object pojo, Method method)</code> method: This method is called after the termination of an intercepted method (expected or not), after the call of the <code>onExit()</code> or <code>onError()</code> callback.</li>
 </ul>
-<p><em>Warning :</em> The <code>InstanceManager</code> has to know your handler wants to intercept fields or methods access, otherwise the implemented callbacks won't be called. Thus you need to register each field and method you want to intercept, so the <code>InstanceManager</code> will call the appropriated callbacks when the specified field or method is accessed :</p>
-<div class="codehilite"><pre> <span class="n">getInstanceManager</span><span class="o">().</span><span class="na">register</span><span class="o">(</span><span class="n">anInterestingFieldMetadata</span><span class="o">,</span> <span class="k">this</span><span class="o">);</span>
- <span class="o">...</span>
- <span class="n">getInstanceManager</span><span class="o">().</span><span class="na">register</span><span class="o">(</span><span class="n">anInterestingMethodMetadata</span><span class="o">,</span> <span class="k">this</span><span class="o">);</span>
- <span class="o">...</span>
-</pre></div>
+<div class="alert alert-warning">
+The <code>InstanceManager<code> has to know your handler wants to intercept fields or methods access, otherwise the implemented callbacks won't be called. Thus you need to register each field and method you want to intercept, so the <code>InstanceManager</code> will call the appropriated callbacks when the specified field or method is accessed :
 
+    <pre>
+     getInstanceManager().register(anInterestingFieldMetadata, this);
+     ...
+     getInstanceManager().register(anInterestingMethodMetadata, this);
+     ...
+    </pre>
+</div>
 
 <div class="alert alert-info">
 The <code>PrimitiveHandler</code> abstract class implements the <code>FieldInterceptor</code> and <code>MethodInterceptor</code> interfaces, which declares the methods described just above. You can create your own interceptor class (implementing one or both of these interfaces) and give it to the <code>InstanceManager</code> register method instead of the handler object itself.
@@ -345,25 +427,46 @@ The <code>PrimitiveHandler</code> abstra
 </pre></div>
 
 
+<p>Obviously, you probably want to use annotations. You just have to provide the annotation classes: <code>handler_namespace.handler_element</code>. For instance, the log handler provides the <code>org.apache.felix.ipojo.log.handler.Log</code> annotation:</p>
+<div class="codehilite"><pre><span class="kn">package</span> <span class="n">org</span><span class="o">.</span><span class="na">apache</span><span class="o">.</span><span class="na">felix</span><span class="o">.</span><span class="na">ipojo</span><span class="o">.</span><span class="na">log</span><span class="o">.</span><span class="na">handler</span><span class="o">;</span>
+
+<span class="cm">/**</span>
+<span class="cm"> * The annotation used to configure the LogHandler.</span>
+<span class="cm"> */</span>
+<span class="kd">public</span> <span class="nd">@interface</span> <span class="n">Log</span> <span class="o">{</span>
+
+    <span class="kd">public</span> <span class="kd">enum</span> <span class="n">Level</span> <span class="o">{</span>
+        <span class="n">INFO</span><span class="o">,</span> <span class="n">ERROR</span><span class="o">,</span> <span class="n">WARNING</span>
+    <span class="o">}</span>
+
+    <span class="cm">/**</span>
+<span class="cm">     * @return the log level</span>
+<span class="cm">     */</span>
+    <span class="n">Level</span> <span class="nf">level</span><span class="o">();</span>
+<span class="o">}</span>
+</pre></div>
+
+
 <p>The remainder of this document describes two examples of handlers:
 <em> A log handler logging messages in the OSGi Log Service
 </em> A properties handler reading a property files to configure POJO field</p>
 <h2 id="log-handler-example">Log Handler example</h2>
-<p>This section describes how to create a simple handler. This handler logs a message in the <em>OSGi Log Service</em> (if present) when the component instance state changes. The code source of this handler is downloadable <a href="loghandler.zip">here</a>.</p>
+<p>This section describes how to create a simple handler. This handler logs a message in the <em>OSGi Log Service</em> (if present) when the component instance state changes.</p>
 <h3 id="handler-metadata">Handler metadata</h3>
-<p>The handler namespace is <code>org.apache.felix.ipojo.log.handler.LogHandler</code>. It is also the name of the handler implementation class. You can note that the handler has an optional dependency on a OSGi log service. If no log services are found, a default implementation is used instead.</p>
-<div class="codehilite"><pre><span class="nt">&lt;ipojo&gt;</span>
-    <span class="c">&lt;!-- Declare the handler --&gt;</span>
-    <span class="nt">&lt;handler</span> 
-        <span class="na">classname=</span><span class="s">&quot;org.apache.felix.ipojo.handler.log.LogHandler&quot;</span>  
-        <span class="na">name=</span><span class="s">&quot;log&quot;</span>  
-        <span class="na">namespace=</span><span class="s">&quot;org.apache.felix.ipojo.log.handler.LogHandler&quot;</span> <span class="nt">&gt;</span>
-        <span class="c">&lt;!-- The log service dependency --&gt;</span>
-        <span class="nt">&lt;requires</span> <span class="na">field=</span><span class="s">&quot;m_log&quot;</span>  <span class="na">optional=</span><span class="s">&quot;true&quot;</span>  
-        <span class="na">default-implementation=</span>
-         <span class="s">&quot;org.apache.felix.ipojo.handler.log.PrimitiveLogService&quot;</span><span class="nt">/&gt;</span>
-    <span class="nt">&lt;/handler&gt;</span>
-<span class="nt">&lt;/ipojo&gt;</span>
+<p>The handler namespace is <code>org.apache.felix.ipojo.log.handler.LogHandler</code>. It is also the name of the handler implementation class. You can note that the handler has an optional dependency on a OSGi log service.</p>
+<div class="codehilite"><pre><span class="c1">// Declare a handler.</span>
+<span class="nd">@Handler</span><span class="o">(</span><span class="n">name</span> <span class="o">=</span> <span class="s">&quot;Log&quot;</span><span class="o">,</span> <span class="n">namespace</span> <span class="o">=</span> <span class="n">LogHandler</span><span class="o">.</span><span class="na">NAMESPACE</span><span class="o">)</span>
+<span class="kd">public</span> <span class="kd">class</span> <span class="nc">LogHandler</span> <span class="kd">extends</span> <span class="n">PrimitiveHandler</span> <span class="o">{</span>
+
+    <span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">NAMESPACE</span> <span class="o">=</span> <span class="s">&quot;org.apache.felix.ipojo.log.handler&quot;</span><span class="o">;</span>
+
+    <span class="c1">// Handlers are iPOJO components, so can use service dependencies</span>
+    <span class="nd">@Requires</span><span class="o">(</span><span class="n">optional</span> <span class="o">=</span> <span class="kc">true</span><span class="o">,</span> <span class="n">nullable</span> <span class="o">=</span> <span class="kc">false</span><span class="o">)</span>
+    <span class="n">LogService</span> <span class="n">log</span><span class="o">;</span>
+    <span class="kd">private</span> <span class="n">InstanceManager</span> <span class="n">instanceManager</span><span class="o">;</span>
+    <span class="kd">private</span> <span class="kt">int</span> <span class="n">logLevel</span><span class="o">;</span>
+
+<span class="c1">//...</span>
 </pre></div>
 
 
@@ -374,71 +477,78 @@ The <code>PrimitiveHandler</code> abstra
 <h4 id="loghandler-class">LogHandler class</h4>
 <p>The handler is implemented inside the <code>LogHandler</code> class in the <code>org.apache.felix.ipojo.handler.log</code> package. This class extends the <code>org.apache.felix.ipojo.PrimitiveHandler</code> class.
 The handler needs to be notified when component instances becomes valid or invalid, thus it implements the <code>InstanceStateListener</code> interface.</p>
-<h4 id="initializecomponentfactory-method">InitializeComponentFactory Method</h4>
-<p>This method parses and checks the component type metadata. The handler needs a log element from its namespace. According to the result, the configure method can throw an exception or parse the level attribute (to get the logging level).</p>
-<div class="codehilite"><pre><span class="kd">public</span> <span class="kt">void</span> <span class="nf">initializeComponentFactory</span><span class="o">(</span><span class="n">ComponentTypeDescription</span> <span class="n">typeDesc</span><span class="o">,</span>
-                                       <span class="n">Element</span> <span class="n">metadata</span><span class="o">)</span>
-                                      <span class="kd">throws</span> <span class="n">ConfigurationException</span> <span class="o">{</span>
-
-    <span class="c1">// Get all Namespace:log element from the metadata </span>
-    <span class="n">Element</span><span class="o">[]</span> <span class="n">log_elements</span> <span class="o">=</span> <span class="n">metadata</span><span class="o">.</span><span class="na">getElements</span><span class="o">(</span><span class="s">&quot;log&quot;</span><span class="o">,</span> <span class="n">NAMESPACE</span><span class="o">);</span>  
-    <span class="k">if</span> <span class="o">(</span><span class="n">log</span> <span class="n">elements</span><span class="o">.</span><span class="na">length</span> <span class="o">==</span> <span class="mi">1</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// There must be exactly one configuration element.</span>
-        <span class="n">Element</span> <span class="n">log_element</span> <span class="o">=</span> <span class="n">log_elements</span><span class="o">[</span><span class="mi">0</span><span class="o">];</span> <span class="c1">// There must be a level attribute </span>
-                                               <span class="c1">// in the configuration element </span>
-        <span class="k">if</span><span class="o">(</span><span class="n">log_element</span><span class="o">.</span><span class="na">containsAttribute</span><span class="o">(</span><span class="s">&quot;level&quot;</span><span class="o">))</span> <span class="o">{</span> 
-            <span class="n">String</span> <span class="n">level</span> <span class="o">=</span> <span class="n">log_element</span><span class="o">.</span><span class="na">getAttribute</span><span class="o">(</span><span class="s">&quot;level&quot;</span><span class="o">);</span> <span class="c1">// Check the value of the </span>
-                                                              <span class="c1">// level attribute</span>
-            <span class="k">if</span> <span class="o">(</span><span class="n">level</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
-                <span class="k">throw</span> <span class="k">new</span> <span class="nf">ConfigurationException</span><span class="o">(</span><span class="s">&quot;No level attribute found in the configuration&quot;</span><span class="o">);</span>
-            <span class="o">}</span>
-            <span class="k">if</span> <span class="o">(!</span><span class="n">level</span><span class="o">.</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="s">&quot;info&quot;</span><span class="o">)</span> <span class="o">&amp;&amp;</span> 
-                <span class="o">!</span><span class="n">level</span><span class="o">.</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="s">&quot;warning&quot;</span><span class="o">)</span> <span class="o">&amp;&amp;</span> 
-                <span class="o">!</span><span class="n">level</span><span class="o">.</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="s">&quot;error&quot;</span><span class="o">))</span> <span class="o">{</span>
-                <span class="k">throw</span> <span class="k">new</span> <span class="nf">ConfigurationException</span><span class="o">(</span><span class="s">&quot;Bad log level specified, &quot;</span>
-                <span class="o">+</span> <span class="s">&quot;accepted values are : info, warning, error&quot;</span><span class="o">);</span>
-            <span class="o">}</span>
+<h4 id="configure-method">Configure Method</h4>
+<p>This method reads the component description and configures the handler. Then, the handler registers itself to the instance manager to be informed of the component's validity changes.</p>
+<div class="codehilite"><pre><span class="cm">/**</span>
+<span class="cm"> * Parses the component&#39;s metadata to retrieve the log level in which we log messages.</span>
+<span class="cm"> *</span>
+<span class="cm"> * @param metadata      component&#39;s metadata</span>
+<span class="cm"> * @param configuration instance configuration (unused in this example)</span>
+<span class="cm"> * @throws ConfigurationException the configuration is inconsistent</span>
+<span class="cm"> */</span>
+<span class="nd">@Override</span>
+<span class="kd">public</span> <span class="kt">void</span> <span class="nf">configure</span><span class="o">(</span><span class="n">Element</span> <span class="n">metadata</span><span class="o">,</span> <span class="n">Dictionary</span> <span class="n">configuration</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">ConfigurationException</span> <span class="o">{</span>
+    <span class="c1">// First parse the metadata to check if the log handler logLevel</span>
+
+    <span class="c1">// Get all Namespace:log element from the metadata</span>
+    <span class="n">Element</span><span class="o">[]</span> <span class="n">log_elements</span> <span class="o">=</span> <span class="n">metadata</span><span class="o">.</span><span class="na">getElements</span><span class="o">(</span><span class="s">&quot;log&quot;</span><span class="o">,</span> <span class="n">NAMESPACE</span><span class="o">);</span>
+
+    <span class="c1">// If an element match, parse the logLevel attribute of the first found element</span>
+    <span class="k">if</span> <span class="o">(</span><span class="n">log_elements</span><span class="o">[</span><span class="mi">0</span><span class="o">].</span><span class="na">containsAttribute</span><span class="o">(</span><span class="s">&quot;level&quot;</span><span class="o">))</span> <span class="o">{</span>
+        <span class="n">String</span> <span class="n">l</span> <span class="o">=</span> <span class="n">log_elements</span><span class="o">[</span><span class="mi">0</span><span class="o">].</span><span class="na">getAttribute</span><span class="o">(</span><span class="s">&quot;level&quot;</span><span class="o">);</span>
+        <span class="k">if</span> <span class="o">(</span><span class="n">l</span><span class="o">.</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="s">&quot;info&quot;</span><span class="o">))</span> <span class="o">{</span>
+            <span class="n">logLevel</span> <span class="o">=</span> <span class="n">LogService</span><span class="o">.</span><span class="na">LOG_INFO</span><span class="o">;</span>
+        <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">l</span><span class="o">.</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="s">&quot;error&quot;</span><span class="o">))</span> <span class="o">{</span>
+            <span class="n">logLevel</span> <span class="o">=</span> <span class="n">LogService</span><span class="o">.</span><span class="na">LOG_ERROR</span><span class="o">;</span>
+        <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">l</span><span class="o">.</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="s">&quot;warning&quot;</span><span class="o">))</span> <span class="o">{</span>
+            <span class="n">logLevel</span> <span class="o">=</span> <span class="n">LogService</span><span class="o">.</span><span class="na">LOG_WARNING</span><span class="o">;</span>
         <span class="o">}</span>
     <span class="o">}</span>
+
+    <span class="n">instanceManager</span> <span class="o">=</span> <span class="n">getInstanceManager</span><span class="o">();</span>
 <span class="o">}</span>
 </pre></div>
 
 
-<h4 id="configure-method">Configure Method</h4>
-<p>This method reads the component description and configures the handler. Then, the handler registers itself to the instance manager to be informed of the component's validity changes.</p>
-<div class="codehilite"><pre><span class="kd">public</span> <span class="kt">void</span> <span class="nf">configure</span><span class="o">(</span><span class="n">Element</span> <span class="n">metadata</span><span class="o">,</span> <span class="n">Dictionary</span> <span class="n">config</span><span class="o">)</span>
-                                <span class="kd">throws</span> <span class="n">ConfigurationException</span> <span class="o">{</span>
-
-   <span class="c1">// Get the configuration element</span>
-   <span class="n">Element</span> <span class="n">log_element</span> <span class="o">=</span> <span class="n">metadata</span><span class="o">.</span><span class="na">getElements</span><span class="o">(</span><span class="s">&quot;log&quot;</span><span class="o">,</span> <span class="n">NAMESPACE</span><span class="o">)[</span><span class="mi">0</span><span class="o">];</span>
-
-   <span class="c1">// Get the level attribute&#39;s value</span>
-   <span class="n">String</span> <span class="n">level</span> <span class="o">=</span> <span class="n">log_element</span><span class="o">.</span><span class="na">getAttribute</span><span class="o">(</span><span class="s">&quot;level&quot;</span><span class="o">);</span>
-
-   <span class="c1">// Extract the log level</span>
-   <span class="k">if</span><span class="o">(</span><span class="n">level</span><span class="o">.</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="s">&quot;info&quot;</span><span class="o">))</span> <span class="o">{</span> 
-       <span class="n">m_level</span> <span class="o">=</span> <span class="n">LogService</span><span class="o">.</span><span class="na">LOG_INFO</span><span class="o">;</span>
-    <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">level</span><span class="o">.</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="s">&quot;warning&quot;</span><span class="o">))</span> <span class="o">{</span> 
-       <span class="n">m_level</span> <span class="o">=</span> <span class="n">LogService</span><span class="o">.</span><span class="na">LOG_WARNING</span><span class="o">;</span>
-    <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">level</span><span class="o">.</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="s">&quot;error&quot;</span><span class="o">))</span> <span class="o">{</span> 
-       <span class="n">m_level</span> <span class="o">=</span> <span class="n">LogService</span><span class="o">.</span><span class="na">LOG_ERROR</span><span class="o">;</span>
+<h4 id="statechanged-method">StateChanged Method</h4>
+<p>This method is called by the instance manager to notify that the component instance state changes. The handler needs to log a message containing the new state.</p>
+<div class="codehilite"><pre><span class="cm">/**</span>
+<span class="cm"> * Logging messages when the instance state is changing</span>
+<span class="cm"> *</span>
+<span class="cm"> * @param state the new state</span>
+<span class="cm"> */</span>
+<span class="kd">public</span> <span class="kt">void</span> <span class="nf">stateChanged</span><span class="o">(</span><span class="kt">int</span> <span class="n">state</span><span class="o">)</span> <span class="o">{</span>
+    <span class="k">if</span> <span class="o">(</span><span class="n">log</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
+        <span class="k">if</span> <span class="o">(</span><span class="n">state</span> <span class="o">==</span> <span class="n">InstanceManager</span><span class="o">.</span><span class="na">VALID</span><span class="o">)</span> <span class="o">{</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;The component instance &quot;</span> <span class="o">+</span> <span class="n">instanceManager</span><span class="o">.</span><span class="na">getInstanceName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot; becomes valid&quot;</span><span class="o">);</span>
+            <span class="n">log</span><span class="o">.</span><span class="na">log</span><span class="o">(</span><span class="n">logLevel</span><span class="o">,</span> <span class="s">&quot;The component instance &quot;</span> <span class="o">+</span> <span class="n">instanceManager</span><span class="o">.</span><span class="na">getInstanceName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot; becomes valid&quot;</span><span class="o">);</span>
+        <span class="o">}</span>
+        <span class="k">if</span> <span class="o">(</span><span class="n">state</span> <span class="o">==</span> <span class="n">InstanceManager</span><span class="o">.</span><span class="na">INVALID</span><span class="o">)</span> <span class="o">{</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;The component instance &quot;</span> <span class="o">+</span> <span class="n">instanceManager</span><span class="o">.</span><span class="na">getInstanceName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot; becomes invalid&quot;</span><span class="o">);</span>
+            <span class="n">log</span><span class="o">.</span><span class="na">log</span><span class="o">(</span><span class="n">logLevel</span><span class="o">,</span> <span class="s">&quot;The component instance &quot;</span> <span class="o">+</span> <span class="n">instanceManager</span><span class="o">.</span><span class="na">getInstanceName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot; becomes invalid&quot;</span><span class="o">);</span>
+        <span class="o">}</span>
     <span class="o">}</span>
-
-    <span class="n">m_manager</span> <span class="o">=</span> <span class="n">getInstanceManager</span><span class="o">();</span> 
-    <span class="n">m_context</span> <span class="o">=</span> <span class="n">m_manager</span><span class="o">.</span><span class="na">getContext</span><span class="o">();</span>
 <span class="o">}</span>
 </pre></div>
 
 
-<h4 id="statechanged-method">StateChanged Method</h4>
-<p>This method is called by the instance manager to notify that the component instance state changes. The handler needs to log a message containing the new state.</p>
-<div class="codehilite"><pre><span class="kd">public</span> <span class="kt">void</span> <span class="nf">stateChanged</span><span class="o">(</span><span class="kt">int</span> <span class="n">state</span><span class="o">)</span> <span class="o">{</span>
-    <span class="c1">// Log the state changed events</span>
-    <span class="k">if</span> <span class="o">(</span><span class="n">state</span> <span class="o">==</span> <span class="n">InstanceManager</span><span class="o">.</span><span class="na">VALID</span><span class="o">)</span> <span class="o">{</span> 
-        <span class="n">m_log</span><span class="o">.</span><span class="na">log</span><span class="o">(</span><span class="n">m_level</span><span class="o">,</span> <span class="s">&quot;The component instance &quot;</span> 
-            <span class="o">+</span> <span class="n">m_manager</span><span class="o">.</span><span class="na">getInstanceName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot; becomes VALID&quot;</span><span class="o">);</span> 
-    <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">state</span> <span class="o">==</span> <span class="n">InstanceManager</span><span class="o">.</span><span class="na">INVALID</span><span class="o">)</span> <span class="o">{</span> 
-        <span class="n">m_log</span><span class="o">.</span><span class="na">log</span><span class="o">(</span><span class="n">m_level</span><span class="o">,</span> <span class="s">&quot;The component instance &quot;</span> 
-            <span class="o">+</span> <span class="n">m_manager</span><span class="o">.</span><span class="na">getInstanceName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot; becomes INVALID&quot;</span><span class="o">);</span> 
+<h3 id="start-and-stop">Start and Stop</h3>
+<p>The handler also contains two methods called by the instance manager when the underlying instance starts and stops.</p>
+<div class="codehilite"><pre><span class="cm">/**</span>
+<span class="cm"> * The instance is starting.</span>
+<span class="cm"> */</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">if</span> <span class="o">(</span><span class="n">log</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
+        <span class="n">log</span><span class="o">.</span><span class="na">log</span><span class="o">(</span><span class="n">logLevel</span><span class="o">,</span> <span class="s">&quot;The component instance &quot;</span> <span class="o">+</span> <span class="n">instanceManager</span><span class="o">.</span><span class="na">getInstanceName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot; is starting&quot;</span><span class="o">);</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+
+<span class="cm">/**</span>
+<span class="cm"> * The instance is stopping.</span>
+<span class="cm"> */</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">if</span> <span class="o">(</span><span class="n">log</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
+        <span class="n">log</span><span class="o">.</span><span class="na">log</span><span class="o">(</span><span class="n">logLevel</span><span class="o">,</span> <span class="s">&quot;The component instance &quot;</span> <span class="o">+</span> <span class="n">instanceManager</span><span class="o">.</span><span class="na">getInstanceName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot; is stopping&quot;</span><span class="o">);</span>
     <span class="o">}</span>
 <span class="o">}</span>
 </pre></div>
@@ -447,23 +557,84 @@ The handler needs to be notified when co
 <h2 id="handler-packaging">Handler packaging</h2>
 <p>This handler needs to be packaged inside an iPOJO bundle. The bundle will import the <code>org.apache.felix.ipojo</code>, <code>org.osgi.framework</code> and <code>org.osgi.service.log</code> packages.</p>
 <h3 id="handler-usage">Handler usage</h3>
-<p>To use this handler, a component needs to declare an <code>org.apache.felix.ipojo.log.handler.LogHandler:log</code> XML element, with a level attribute. This level attribute's value can be <code>"error"</code>, <code>"warning"</code> or <code>"info"</code>. Here is an usage example:</p>
-<div class="codehilite"><pre><span class="nt">&lt;ipojo</span> <span class="na">xmlns:log=</span><span class="s">&quot;org.apache.felix.ipojo.log.handler.LogHandler&quot;</span><span class="nt">&gt;</span>
+<p>To use this handler, a component use the <code>Log</code> annotation, with a level attribute. This level attribute's value can be <code>"error"</code>, <code>"warning"</code> or <code>"info"</code>. Here is an usage example:</p>
+<div class="codehilite"><pre>package org.apache.felix.ipojo.log.handler.example;
 
-    <span class="c">&lt;!-- Declare a component using the LogHandler --&gt;</span>
-    <span class="nt">&lt;component</span> <span class="na">classname=</span><span class="s">&quot;...&quot;</span><span class="nt">&gt;</span>
-        ...
-        <span class="c">&lt;!-- Configuration of the LogHandler ?</span>
-<span class="c">        &lt;log:log level=&quot;WARNING&quot;/&gt;</span>
-<span class="c">     &lt;/component&gt;</span>
-<span class="c">     ...</span>
-<span class="c">&lt;/ipojo&gt;</span>
+import org.apache.felix.ipojo.annotations.*;
+import org.apache.felix.ipojo.foo.FooService;
+import org.apache.felix.ipojo.log.handler.Log;
+
+@Component(immediate = true)
+@Log(level = Log.Level.INFO) // We configure the handler.
+@Instantiate(name = &quot;my.simple.consumer&quot;)
+public class SimpleComponent {
+
+    @Requires
+    FooService fs;
+
+    @Validate
+    public void starting() {
+        System.out.println(&quot;Starting...&quot;);
+        fs.foo();
+    }
+
+    @Invalidate
+    public void stopping() {
+        System.out.println(&quot;Stopping...&quot;);
+    }
+}
+</pre></div>
+
+
+<h3 id="playing-with-the-handler">Playing with the handler</h3>
+<p>The archive contains a project named <code>Log-Handler-In-Felix</code>, which once built, provides a Felix framework with all the bundles deployed.</p>
+<p>Unzip the archive, and build the whole project using Maven: <code>mvn clean install</code>. It builds the log handler and the property handler. Then navigate to the felix-framework-VERSION directory:</p>
+<div class="codehilite"><pre>mvn clean install
+<span class="c">#...</span>
+<span class="nb">cd </span>Log-Handler-In-Felix/target/felix-framework-4.2.1/
+java -jar bin/felix.jar
+</pre></div>
+
+
+<p>Once you have launched Felix, you get the Gogo Shell prompt:</p>
+<div class="codehilite"><pre>Starting...
+Foo
+The component instance my.simple.consumer becomes valid
+____________________________
+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.8.6<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|iPOJO Log Handler Consumer <span class="o">(</span>1.9.0.SNAPSHOT<span class="o">)</span>
+    8|Active     |    1|iPOJO Foo Service <span class="o">(</span>1.9.0.SNAPSHOT<span class="o">)</span>
+    9|Active     |    1|iPOJO Log Handler <span class="o">(</span>1.9.0.SNAPSHOT<span class="o">)</span>
+   10|Active     |    1|Apache Felix Log Service <span class="o">(</span>1.0.1<span class="o">)</span>
+g!
+</pre></div>
+
+
+<p>You can already see some of the messages printed by the handler (<code>The component instance my.simple.consumer becomes valid</code>). To see more message, stop and start the Foo Service bundle:</p>
+<div class="codehilite"><pre>g! stop 8
+The component instance my.simple.consumer becomes invalid
+Stopping...
+g! start 8
+g! Starting...
+Foo
+The component instance my.simple.consumer becomes valid
 </pre></div>
 
 
+<p>By stopping the Foo service bundle, you withdrew the foo service from the service registry making our component invalid (and unhappy). The handler is notified of the new state and logs a message. When the bundle restarts, the service is republished. So the instance becomes valid again. The handler is notified and logs another message.</p>
 <h2 id="properties-handler-example">Properties Handler example</h2>
 <p>This section presents a second handler. This handler loads a property file containing field name and initial value. Then it injects and maintains these values inside POJO fields. In this example, only String values are managed.</p>
-<p>You can find the sources of this example handler in the <code>example/handler/property</code> directory of the iPOJO sources.</p>
 <p>This handler is always valid, so do not participate to the component instance lifecycle. Moreover, the handler does not need to be notified when the component instance state changed. But, it need to be notified when POJO fields need a value or change their value.</p>
 <h3 id="handler-implementation_1">Handler implementation</h3>
 <p>The handler needs to override following methods:</p>
@@ -480,66 +651,111 @@ The handler needs to be notified when co
 <li>The properties file name (<code>m_file</code>) </li>
 </ul>
 <p><em>Note:</em> the file name is the absolute path on the local machine of the file.</p>
+<div class="codehilite"><pre><span class="cm">/**</span>
+<span class="cm"> * This handler load a properties file containing property value.</span>
+<span class="cm"> * The handler injects this values inside fields. When stopped the handler stores updated value inside the file. The</span>
+<span class="cm"> * properties file contains &lt;pre&gt;field-name : field-value&lt;/pre&gt; (field-value are strings)</span>
+<span class="cm"> *</span>
+<span class="cm"> * Instances can override file locations by setting the {@literal properties.file} property.</span>
+<span class="cm"> *</span>
+<span class="cm"> * @author &lt;a href=&quot;mailto:dev@felix.apache.org&quot;&gt;Felix Project Team&lt;/a&gt;</span>
+<span class="cm"> */</span>
+<span class="nd">@Handler</span><span class="o">(</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;properties&quot;</span><span class="o">,</span> <span class="n">namespace</span> <span class="o">=</span> <span class="n">PropertiesHandler</span><span class="o">.</span><span class="na">NAMESPACE</span><span class="o">)</span>
+<span class="kd">public</span> <span class="kd">class</span> <span class="nc">PropertiesHandler</span> <span class="kd">extends</span> <span class="n">PrimitiveHandler</span> <span class="o">{</span>
+
+    <span class="cm">/**</span>
+<span class="cm">     * The Handler namespace.</span>
+<span class="cm">     */</span>
+    <span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">NAMESPACE</span> <span class="o">=</span> <span class="s">&quot;org.apache.felix.ipojo.handler.properties&quot;</span><span class="o">;</span>
+
+    <span class="cm">/**</span>
+<span class="cm">     * The loaded properties.</span>
+<span class="cm">     */</span>
+    <span class="kd">private</span> <span class="n">Properties</span> <span class="n">m_properties</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Properties</span><span class="o">();</span>
+
+    <span class="cm">/**</span>
+<span class="cm">     * The properties file location, configured in the component&#39;s metadata.</span>
+<span class="cm">     */</span>
+    <span class="kd">private</span> <span class="n">String</span> <span class="n">m_file</span><span class="o">;</span>
+</pre></div>
+
+
 <h4 id="configure-method_1">Configure Method</h4>
-<p>This method begins by parsing the component type metadata. The handler needs a properties element from its namespace. According to the result, the configure method can return immediately or parse the file attribute (to get the properties file path). Then, it builds a field list (String array) to register to field notification. By registering with a field array, the handler will be a part of the component instance container and will be notified of field access.</p>
-<div class="codehilite"><pre><span class="kd">public</span> <span class="kt">void</span> <span class="nf">configure</span><span class="o">(</span><span class="n">Element</span> <span class="n">metadata</span><span class="o">,</span> <span class="n">Dictionary</span> <span class="n">configuration</span><span class="o">)</span>
-                                             <span class="kd">throws</span> <span class="n">ConfigurationException</span> <span class="o">{</span>
-    <span class="c1">// Parse metadata to get &lt;properties file=&quot;$file&quot;/&gt;</span>
-    <span class="c1">// Get all example.handler.properties:properties element</span>
-    <span class="n">Element</span><span class="o">[]</span> <span class="n">elem</span> <span class="o">=</span> <span class="n">metadata</span><span class="o">.</span><span class="na">getElements</span><span class="o">(</span><span class="s">&quot;properties&quot;</span><span class="o">,</span> <span class="n">NAMESPACE</span><span class="o">).</span>   
+<p>This method begins by parsing the component type metadata. The handler needs a properties element from its namespace. According to the result, the configure method can return immediately or parse the file attribute (to get the properties file path). Then, it builds a field list (String array) to register to field notification. By registering with a field array, the handler is going to be notified of field access.</p>
+<div class="codehilite"><pre><span class="cm">/**</span>
+<span class="cm"> * This method is the first to be invoked.</span>
+<span class="cm"> * This method aims to configure the handler. It receives the component type metadata and the instance</span>
+<span class="cm"> * configuration. The method parses given metadata and registers fields to inject.</span>
+<span class="cm"> *</span>
+<span class="cm"> * Step 3 : when the instance configuration contains the properties.file property, it overrides the properties file location.</span>
+<span class="cm"> *</span>
+<span class="cm"> * @param metadata : component type metadata</span>
+<span class="cm"> * @param configuration : instance description</span>
+<span class="cm"> * @throws ConfigurationException : the configuration of the handler has failed.</span>
+<span class="cm"> */</span>
+<span class="nd">@SuppressWarnings</span><span class="o">(</span><span class="s">&quot;unchecked&quot;</span><span class="o">)</span>
+<span class="kd">public</span> <span class="kt">void</span> <span class="nf">configure</span><span class="o">(</span><span class="n">Element</span> <span class="n">metadata</span><span class="o">,</span> <span class="n">Dictionary</span> <span class="n">configuration</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">ConfigurationException</span> <span class="o">{</span>
+    <span class="c1">// Get all elements to configure the handler</span>
+    <span class="n">Element</span><span class="o">[]</span> <span class="n">elem</span> <span class="o">=</span> <span class="n">metadata</span><span class="o">.</span><span class="na">getElements</span><span class="o">(</span><span class="s">&quot;properties&quot;</span><span class="o">,</span> <span class="n">NAMESPACE</span><span class="o">);</span>
+
     <span class="k">switch</span> <span class="o">(</span><span class="n">elem</span><span class="o">.</span><span class="na">length</span><span class="o">)</span> <span class="o">{</span>
-        <span class="k">case</span> <span class="mi">0</span><span class="o">:</span> <span class="c1">// No matching element in metadata, throw a configuration error. </span>
+        <span class="k">case</span> <span class="mi">0</span><span class="o">:</span>
+            <span class="c1">// No matching element in metadata, throw a configuration error.</span>
+            <span class="c1">// It actually happen only if you force the handler to be plugged.</span>
             <span class="k">throw</span> <span class="k">new</span> <span class="nf">ConfigurationException</span><span class="o">(</span><span class="s">&quot;No properties found&quot;</span><span class="o">);</span>
-        <span class="k">case</span> <span class="mi">1</span><span class="o">:</span> <span class="c1">// One &#39;properties&#39; found, get attributes</span>
-            <span class="n">m_file</span> <span class="o">=</span> <span class="n">elem</span><span class="o">[</span><span class="mi">0</span><span class="o">].</span><span class="na">getAttribute</span><span class="o">(</span><span class="s">&quot;file&quot;</span><span class="o">);</span> 
-            <span class="k">if</span> <span class="o">(</span><span class="n">m_file</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// if file is null, throw a configuration error. </span>
-                <span class="k">throw</span> <span class="k">new</span> <span class="nf">ConfigurationException</span><span class="o">(</span><span class="s">&quot;Malformed properties &quot;</span> 
-                   <span class="o">+</span> <span class="s">&quot; element : file attribute must be set&quot;</span><span class="o">);</span>
-             <span class="o">}</span> 
-             <span class="k">break</span><span class="o">;</span>
-         <span class="k">default</span><span class="o">:</span> <span class="c1">// To simplify we handle only one properties element. </span>
-             <span class="k">throw</span> <span class="k">new</span> <span class="nf">ConfigurationException</span><span class="o">(</span><span class="s">&quot;Only one properties element is supported&quot;</span><span class="o">);</span> 
-     <span class="o">}</span>  
-     <span class="c1">// Look if the instance configuration overrides file location : </span>
-     <span class="n">String</span> <span class="n">instanceFile</span> <span class="o">=</span> <span class="o">(</span><span class="n">String</span><span class="o">)</span> <span class="n">configuration</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">&quot;properties.file&quot;</span><span class="o">);</span> 
-     <span class="k">if</span> <span class="o">(</span><span class="n">instanceFile</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
-         <span class="n">m_file</span> <span class="o">=</span> <span class="n">instanceFile</span><span class="o">;</span> 
-      <span class="o">}</span>
-      <span class="c1">// Load properties</span>
-      <span class="k">try</span><span class="o">{</span>
-          <span class="n">loadProperties</span><span class="o">();</span>
-      <span class="o">}</span> <span class="k">catch</span><span class="o">(</span><span class="n">IOException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
-          <span class="k">throw</span> <span class="k">new</span> <span class="nf">ConfigurationException</span><span class="o">(</span><span class="s">&quot;Error when reading the &quot;</span> 
-             <span class="o">+</span>  <span class="n">m_file</span> <span class="o">+</span> <span class="s">&quot; file : &quot;</span> <span class="o">+</span> <span class="n">e</span><span class="o">.</span><span class="na">getMessage</span><span class="o">());</span> 
-      <span class="o">}</span>
-      <span class="c1">// Register fields </span>
-      <span class="c1">// By convention, properties file entries are field name, </span>
-      <span class="c1">// so looks for each property to get field list.</span>
-      <span class="c1">//First get the Pojo Metadata metadata:</span>
-      <span class="n">PojoMetadata</span> <span class="n">pojoMeta</span> <span class="o">=</span> <span class="n">getPojoMetadata</span><span class="o">();</span>
-      <span class="n">Enumeration</span> <span class="n">e</span> <span class="o">=</span> <span class="n">m_properties</span><span class="o">.</span><span class="na">keys</span><span class="o">();</span>
-      <span class="k">while</span><span class="o">(</span><span class="n">e</span><span class="o">.</span><span class="na">hasMoreElements</span><span class="o">())</span> <span class="o">{</span> 
-          <span class="n">String</span> <span class="n">field</span> <span class="o">=</span> <span class="o">(</span><span class="n">String</span><span class="o">)</span> <span class="n">e</span><span class="o">.</span><span class="na">nextElement</span><span class="o">();</span>
-          <span class="n">FieldMetadata</span> <span class="n">fm</span> <span class="o">=</span> <span class="n">pojoMeta</span><span class="o">.</span><span class="na">getField</span><span class="o">(</span><span class="n">field</span><span class="o">);</span>
-          <span class="k">if</span><span class="o">(</span><span class="n">fm</span><span class="o">==</span><span class="kc">null</span><span class="o">){</span>
-              <span class="c1">//The field does not exist</span>
-              <span class="k">throw</span> <span class="k">new</span> <span class="nf">ConfigurationException</span><span class="o">(</span><span class="s">&quot;The field &quot;</span> <span class="o">+</span> <span class="n">field</span> <span class="o">+</span> 
-                   <span class="s">&quot; is declared in the properties file but does not &quot;</span> <span class="o">+</span>
-                   <span class="s">&quot; exist in the pojo&quot;</span><span class="o">);</span>
-          <span class="o">}</span>
-          <span class="c1">// Then check that the field is a String field </span>
-          <span class="k">if</span> <span class="o">(!</span><span class="n">fm</span><span class="o">.</span><span class="na">getFieldType</span><span class="o">().</span><span class="na">equals</span><span class="o">(</span><span class="n">String</span><span class="o">.</span><span class="na">class</span><span class="o">.</span><span class="na">getName</span><span class="o">()))</span> <span class="o">{</span> 
-              <span class="k">throw</span> <span class="k">new</span> <span class="nf">ConfigurationException</span><span class="o">(</span><span class="s">&quot;The field &quot;</span> <span class="o">+</span> <span class="n">field</span> <span class="o">+</span> 
-                  <span class="s">&quot; exists in the pojo, but is not a String&quot;</span><span class="o">);</span> 
-          <span class="o">}</span> 
-          <span class="c1">// All checks are ok, register the interceptor. </span>
-          <span class="n">getInstanceManager</span><span class="o">().</span><span class="na">register</span><span class="o">(</span><span class="n">fm</span><span class="o">,</span> <span class="k">this</span><span class="o">);</span>
-      <span class="o">}</span>
-  <span class="o">}</span>
+        <span class="k">case</span> <span class="mi">1</span><span class="o">:</span>
+            <span class="c1">// One &#39;properties&#39; found, get attributes.</span>
+            <span class="n">m_file</span> <span class="o">=</span> <span class="n">elem</span><span class="o">[</span><span class="mi">0</span><span class="o">].</span><span class="na">getAttribute</span><span class="o">(</span><span class="s">&quot;file&quot;</span><span class="o">);</span>
+            <span class="k">if</span> <span class="o">(</span><span class="n">m_file</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
+                <span class="c1">// if file is null, throw a configuration error.</span>
+                <span class="k">throw</span> <span class="k">new</span> <span class="nf">ConfigurationException</span><span class="o">(</span><span class="s">&quot;Malformed properties element : file attribute must be set&quot;</span><span class="o">);</span>
+            <span class="o">}</span>
+            <span class="k">break</span><span class="o">;</span>
+        <span class="k">default</span><span class="o">:</span>
+            <span class="c1">// To simplify we handle only one properties element.</span>
+            <span class="k">throw</span> <span class="k">new</span> <span class="nf">ConfigurationException</span><span class="o">(</span><span class="s">&quot;Only one properties element is supported&quot;</span><span class="o">);</span>
+    <span class="o">}</span>
+
+    <span class="c1">// Look if the instance overrides file location :</span>
+    <span class="n">String</span> <span class="n">instanceFile</span> <span class="o">=</span> <span class="o">(</span><span class="n">String</span><span class="o">)</span> <span class="n">configuration</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">&quot;properties.file&quot;</span><span class="o">);</span>
+    <span class="k">if</span> <span class="o">(</span><span class="n">instanceFile</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
+        <span class="n">m_file</span> <span class="o">=</span> <span class="n">instanceFile</span><span class="o">;</span>
+    <span class="o">}</span>
+
+    <span class="c1">// Load properties</span>
+    <span class="k">try</span> <span class="o">{</span>
+        <span class="n">loadProperties</span><span class="o">();</span>
+    <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">IOException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
+        <span class="k">throw</span> <span class="k">new</span> <span class="nf">ConfigurationException</span><span class="o">(</span><span class="s">&quot;Error when reading the &quot;</span> <span class="o">+</span> <span class="n">m_file</span> <span class="o">+</span> <span class="s">&quot; file : &quot;</span> <span class="o">+</span> <span class="n">e</span><span class="o">.</span><span class="na">getMessage</span><span class="o">());</span>
+    <span class="o">}</span>
+
+    <span class="c1">// Register fields</span>
+    <span class="c1">// By convention, properties file entry are field name, so look for each property to get field list.</span>
+
+    <span class="c1">//First get Pojo Metadata metadata :</span>
+    <span class="n">PojoMetadata</span> <span class="n">pojoMeta</span> <span class="o">=</span> <span class="n">getPojoMetadata</span><span class="o">();</span>
+    <span class="n">Enumeration</span> <span class="n">e</span> <span class="o">=</span> <span class="n">m_properties</span><span class="o">.</span><span class="na">keys</span><span class="o">();</span>
+    <span class="k">while</span> <span class="o">(</span><span class="n">e</span><span class="o">.</span><span class="na">hasMoreElements</span><span class="o">())</span> <span class="o">{</span>
+        <span class="n">String</span> <span class="n">field</span> <span class="o">=</span> <span class="o">(</span><span class="n">String</span><span class="o">)</span> <span class="n">e</span><span class="o">.</span><span class="na">nextElement</span><span class="o">();</span>
+        <span class="n">FieldMetadata</span> <span class="n">fm</span> <span class="o">=</span> <span class="n">pojoMeta</span><span class="o">.</span><span class="na">getField</span><span class="o">(</span><span class="n">field</span><span class="o">);</span>
+
+        <span class="k">if</span> <span class="o">(</span><span class="n">fm</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// The field does not exist</span>
+            <span class="k">throw</span> <span class="k">new</span> <span class="nf">ConfigurationException</span><span class="o">(</span><span class="s">&quot;The field &quot;</span> <span class="o">+</span> <span class="n">field</span> <span class="o">+</span> <span class="s">&quot; is declared in the properties file but does not exist in the pojo&quot;</span><span class="o">);</span>
+        <span class="o">}</span>
+
+        <span class="c1">// Then check that the field is a String field</span>
+        <span class="k">if</span> <span class="o">(!</span><span class="n">fm</span><span class="o">.</span><span class="na">getFieldType</span><span class="o">().</span><span class="na">equals</span><span class="o">(</span><span class="n">String</span><span class="o">.</span><span class="na">class</span><span class="o">.</span><span class="na">getName</span><span class="o">()))</span> <span class="o">{</span>
+            <span class="k">throw</span> <span class="k">new</span> <span class="nf">ConfigurationException</span><span class="o">(</span><span class="s">&quot;The field &quot;</span> <span class="o">+</span> <span class="n">field</span> <span class="o">+</span> <span class="s">&quot; exists in the pojo, but is not a String&quot;</span><span class="o">);</span>
+        <span class="o">}</span>
+
+        <span class="c1">// All checks are ok, register the interceptor.</span>
+        <span class="n">getInstanceManager</span><span class="o">().</span><span class="na">register</span><span class="o">(</span><span class="n">fm</span><span class="o">,</span> <span class="k">this</span><span class="o">);</span>
+    <span class="o">}</span>
 <span class="o">}</span>
 </pre></div>
 
 
+<p>Notice that the handler is using the instance configuration. So instances can set their own file location using the <code>properties.file</code> property.</p>
 <h4 id="the-start-and-stop-methods">The start and stop methods</h4>
 <p>The start method does nothing, but needs to be implemented.</p>
 <div class="codehilite"><pre><span class="kd">public</span> <span class="kt">void</span> <span class="nf">start</span><span class="o">()</span> <span class="o">{}</span>
@@ -577,13 +793,68 @@ The handler needs to be notified when co
 </pre></div>
 
 
+<h3 id="creating-the-annotation">Creating the annotation</h3>
+<p>The handler provides an annotation to ease its use:</p>
+<div class="codehilite"><pre><span class="kn">package</span> <span class="n">org</span><span class="o">.</span><span class="na">apache</span><span class="o">.</span><span class="na">felix</span><span class="o">.</span><span class="na">ipojo</span><span class="o">.</span><span class="na">handler</span><span class="o">.</span><span class="na">properties</span><span class="o">;</span>
+
+<span class="cm">/**</span>
+<span class="cm"> * The Properties annotation.</span>
+<span class="cm"> * This annotation may be used in POJO class to used the Property handler.</span>
+<span class="cm"> * @author &lt;a href=&quot;mailto:dev@felix.apache.org&quot;&gt;Felix Project Team&lt;/a&gt;</span>
+<span class="cm"> */</span>
+<span class="kd">public</span> <span class="nd">@interface</span> <span class="n">Properties</span> <span class="o">{</span>
+
+    <span class="cm">/**</span>
+<span class="cm">     * Returns the property file used by the handler.</span>
+<span class="cm">     */</span>
+    <span class="n">String</span> <span class="nf">file</span><span class="o">();</span>
+
+<span class="o">}</span>
+</pre></div>
+
+
 <h3 id="handler-packaging_1">Handler packaging</h3>
 <p>This handler needs to be inside a bundle importing the <code>org.apache.felix.ipojo</code> packages and exporting the <code>org.apache.felix.ipojo.properties.handler</code> package.</p>
-<h3 id="handler-usage_1">Handler usage</h3>
-<p>To use this handler, a component need to declare an <code>Properties</code> XML element in the <code>org.apache.felix.ipojo.properties.handler</code> namespace, with a <code>file</code> attribute indicating the absolute file path of the properties file.</p>
-<p><em>Note :</em> you need to escape '\' (anti-slash characters) in the file path name.</p>
-<h3 id="download">Download</h3>
-<p>The PropertiesHandler is available <a href="property-handler.zip">here</a>. The archive file contains the handler implementation and a simple component using this handler.</p>
+<h3 id="playing-with-the-handler_1">Playing with the handler</h3>
+<p>As for the log handler , the archive contains a felix distribution with all bundles deployed.</p>
+<div class="codehilite"><pre><span class="nb">cd </span>Property-Handler-In-Felix/target/felix-framework-4.2.1/
+java -jar bin/felix.jar
+</pre></div>
+
+
+<p>In Gogo you immediately see the loaded properties:</p>
+<div class="codehilite"><pre>-- listing properties --
+<span class="nv">property2</span><span class="o">=</span><span class="s2">&quot;bbb&quot;</span>
+<span class="nv">property1</span><span class="o">=</span><span class="s2">&quot;aaa&quot;</span>
+PropertiesTester is starting ...
+Property 1 : <span class="s2">&quot;aaa&quot;</span>
+Property 2 : <span class="s2">&quot;bbb&quot;</span>
+Update properties
+-- listing properties --
+<span class="nv">property2</span><span class="o">=</span><span class="s2">&quot;bbb&quot;</span>
+<span class="nv">property1</span><span class="o">=</span><span class="s2">&quot;aaa&quot;</span>
+PropertiesTester is starting ...
+Property 1 : <span class="s2">&quot;aaa&quot;</span>
+Property 2 : <span class="s2">&quot;bbb&quot;</span>
+Update properties
+____________________________
+Welcome to Apache Felix Gogo
+g!
+</pre></div>
+
+

[... 25 lines stripped ...]


Mime
View raw message