blob: 1715fe1150dab6b2276fbd88344a5602836de912 [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 Advanced Tutorial</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="the-ipojo-snack-bar">The iPOJO Snack Bar<a class="headerlink" href="#the-ipojo-snack-bar" title="Permanent link">&para;</a></h1>
<p><em>This tutorial illustrates some advanced features of iPOJO</em></p>
<div class="toc">
<ul>
<li><a href="#the-ipojo-snack-bar">The iPOJO Snack Bar</a><ul>
<li><a href="#context">Context</a></li>
<li><a href="#preparing-the-tutorial">Preparing the tutorial</a></li>
<li><a href="#writing-a-component-providing-two-services">Writing a component providing two services</a></li>
<li><a href="#publishing-service-properties">Publishing service properties</a></li>
<li><a href="#publishing-dynamic-properties">Publishing 'dynamic' properties</a></li>
<li><a href="#configuring-instances">Configuring instances</a></li>
<li><a href="#using-filter-in-service-requirements">Using filter in service requirements</a></li>
<li><a href="#immediate-component-instance">Immediate component instance</a></li>
<li><a href="#creating-instances-from-an-external-component-type">Creating instances from an external component type</a></li>
<li><a href="#deploying-the-application">Deploying the application</a></li>
<li><a href="#using-the-lifecycle-controller">Using the lifecycle controller</a></li>
<li><a href="#reconfiguring-an-instance">Reconfiguring an instance</a></li>
<li><a href="#conclusion">Conclusion</a></li>
</ul>
</li>
</ul>
</div>
<h2 id="context">Context<a class="headerlink" href="#context" title="Permanent link">&para;</a></h2>
<p>This tutorial is based on a very simple application; customers are using a vendor service to buy hot dog or pop corn according to the availability of appropriate providers. Both of the vendors implement (and provide) the vendor service. The hot dog vendor depends on two others services to get the ingredients (buns and wiener). To sell pop corn, the pop corn vendor requires having enough corn in stock.</p>
<p><img src="vendor.png"></p>
<h2 id="preparing-the-tutorial">Preparing the tutorial<a class="headerlink" href="#preparing-the-tutorial" title="Permanent link">&para;</a></h2>
<p>The tutorial archive is available <a href="http://people.apache.org/~clement/ipojo/tutorials/advanced/advanced.tutorial.zip">here</a>. This archive contains both the source code and a pre-configured version of Felix. First, unzip the archive. Then, launch <code>ant</code> to compile the bundles composing this tutorial. Once compiled, you can launch Felix and start the tutorial. To launch, Felix launch the following command from the <code>felix</code> directory:</p>
<div class="codehilite"><pre>java -jar bin/felix.jar
</pre></div>
<h2 id="writing-a-component-providing-two-services">Writing a component providing two services<a class="headerlink" href="#writing-a-component-providing-two-services" title="Permanent link">&para;</a></h2>
<p>The sources of this project are inside the <em>vendor.buns-and-wieners</em> directory.
The hot dog vendor requires at the same time the bun service and the wiener service. In our application these services are provided by the same component. This component can be implemented as follows (src\org\apache\felix\ipojo\example\vendor\provider\BunWienerProvider.java):</p>
<div class="codehilite"><pre><span class="kd">public</span> <span class="kd">class</span> <span class="nc">BunWienerProvider</span> <span class="kd">implements</span> <span class="n">BunService</span><span class="o">,</span> <span class="n">WienerService</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">getBun</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;Get a bun&quot;</span><span class="o">);</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">getWiener</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;Get a wiener&quot;</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
</pre></div>
<p>This class just implements the two service interfaces. Its descriptor (contained in the metadata.xml file) is:</p>
<div class="codehilite"><pre><span class="nt">&lt;ipojo&gt;</span>
<span class="nt">&lt;component</span>
<span class="na">classname=</span><span class="s">&quot;org.apache.felix.ipojo.example.vendor.provider.BunWienerProvider&quot;</span>
<span class="na">name=</span><span class="s">&quot;buns_and_wieners&quot;</span> <span class="na">public=</span><span class="s">&quot;false&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;provides/&gt;</span>
<span class="nt">&lt;/component&gt;</span>
<span class="nt">&lt;instance</span> <span class="na">component=</span><span class="s">&quot;buns_and_wieners&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/ipojo&gt;</span>
</pre></div>
<p>In the descriptor, we declare a component type for this vendor which contains the implementation class. The <code>classname</code> attribute contains the qualified name of the component implementation. The "name" attribute is the component type name. It is only used to refer to this type.</p>
<p>The <code>public=false</code> attribute disables factory exposition. A component type publishing a factory provides a way to create instance of this type from outside this descriptor. In our case, we want to guarantee that only one instance (singleton) can be created, so we disable the factory mechanism.</p>
<p>iPOJO manages service publication and providing automatically at runtime. The <code>&lt;provides/&gt;</code> element means that the component provides services. If this element is not present, iPOJO will publish all implemented interfaces by the implementation class (and parent class too). In our case, it will publish the BunService and WienerService interfaces.</p>
<p>Finally, we create one instance of our component. The instance contains the component attribute describing the component type to use. We use the component type name to target the wanted component type. </p>
<p>At runtime, the bundle containing this component will create an instance which provides the BunService and the WienerService.</p>
<h2 id="publishing-service-properties">Publishing service properties<a class="headerlink" href="#publishing-service-properties" title="Permanent link">&para;</a></h2>
<p>The sources of this project are inside the <em>vendor.hotdog</em> directory.
The hot dog vendor only provides the Vendor service. To provide this service, it uses a bun service and a wiener service. The following code (contained in the <em>src/org/apache/felix/ipojo/example/vendor/hotdog/HotDogVendor.java</em> file) shows a very simple implementation of this component:</p>
<div class="codehilite"><pre><span class="kd">public</span> <span class="kd">class</span> <span class="nc">HotDogVendor</span> <span class="kd">implements</span> <span class="n">Vendor</span> <span class="o">{</span>
<span class="cm">/**</span>
<span class="cm"> * Bun provider (required service).</span>
<span class="cm"> */</span>
<span class="kd">private</span> <span class="n">Bun</span> <span class="n">bunProvider</span><span class="o">;</span>
<span class="cm">/**</span>
<span class="cm"> * Wiener provider (required service). </span>
<span class="cm"> */</span>
<span class="kd">private</span> <span class="n">Wiener</span> <span class="n">wienerProvider</span><span class="o">;</span>
<span class="cm">/**</span>
<span class="cm"> * Sell method.</span>
<span class="cm"> * To provide an hotdog, the vendor consume a bun and a wiener.</span>
<span class="cm"> * This method is synchronized to avoid serving to client </span>
<span class="cm"> * at the same time.</span>
<span class="cm"> * @return a hotdog.</span>
<span class="cm"> * @see org.apache.felix.ipojo.example.vendor.service.Vendor#sell()</span>
<span class="cm"> */</span>
<span class="kd">public</span> <span class="kd">synchronized</span> <span class="n">Product</span> <span class="nf">sell</span><span class="o">()</span> <span class="o">{</span>
<span class="n">bunProvider</span><span class="o">.</span><span class="na">getBun</span><span class="o">();</span>
<span class="n">wienerProvider</span><span class="o">.</span><span class="na">getWiener</span><span class="o">();</span>
<span class="k">return</span> <span class="k">new</span> <span class="nf">HotDog</span><span class="o">();</span>
<span class="o">}</span>
</pre></div>
<p>Once implemented, we need to describe this component type. The descriptor file is the <em>metadata.xml</em> file. The field attributes in the "requires" elements are used to inject the required services. At runtime, iPOJO injects automatically a BunService provider in the "bunProvider" field and a WienerService provider in the "wienerProvider" field. The implementation uses these fields the same way it would have used any other fields (as illustrated in the sell method).</p>
<div class="codehilite"><pre><span class="nt">&lt;ipojo&gt;</span>
<span class="nt">&lt;component</span>
<span class="na">classname=</span><span class="s">&quot;org.apache.felix.ipojo.example.vendor.hotdog.HotDogVendor&quot;</span>
<span class="na">name=</span><span class="s">&quot;HD&quot;</span> <span class="na">public=</span><span class="s">&quot;false&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;provides/&gt;</span>
<span class="nt">&lt;requires</span> <span class="na">field=</span><span class="s">&quot;bunProvider&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;requires</span> <span class="na">field=</span><span class="s">&quot;wienerProvider&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/component&gt;</span>
<span class="nt">&lt;instance</span> <span class="na">component=</span><span class="s">&quot;HD&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/ipojo&gt;</span>
</pre></div>
<p>The component type declares a provided service (the Vendor Service). Then, the component declares the two service dependencies (using the "requires" element). However, we would like to add a service property on the Vendor service describing the sold product (here, "hotdog"). To achieve this, we just need to add a property element in the "provides" tags: </p>
<div class="codehilite"><pre><span class="nt">&lt;ipojo&gt;</span>
<span class="nt">&lt;component</span>
<span class="na">classname=</span><span class="s">&quot;org.apache.felix.ipojo.example.vendor.hotdog.HotDogVendor&quot;</span>
<span class="na">name=</span><span class="s">&quot;HD&quot;</span> <span class="na">public=</span><span class="s">&quot;false&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;provides&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;product&quot;</span> <span class="na">type=</span><span class="s">&quot;string&quot;</span> <span class="na">value=</span><span class="s">&quot;hotdog&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/provides&gt;</span>
<span class="nt">&lt;requires</span> <span class="na">field=</span><span class="s">&quot;bunProvider&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;requires</span> <span class="na">field=</span><span class="s">&quot;wienerProvider&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/component&gt;</span>
<span class="nt">&lt;instance</span> <span class="na">component=</span><span class="s">&quot;HD&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/ipojo&gt;</span>
</pre></div>
<p>iPOJO then publishes the "product" property in the "vendor" service registration. This property has the "hotdog" value.</p>
<h2 id="publishing-dynamic-properties">Publishing 'dynamic' properties<a class="headerlink" href="#publishing-dynamic-properties" title="Permanent link">&para;</a></h2>
<p>The bun service and the wiener service can also expose service properties. In our case, these service properties will describe the stock of ingredients. Each time the service is used, the property value is decreased.
To achieve this, we modify the current implementation to add a field representing the property:</p>
<div class="codehilite"><pre><span class="kd">public</span> <span class="kd">class</span> <span class="nc">BunWienerProvider</span> <span class="kd">implements</span> <span class="n">BunService</span><span class="o">,</span> <span class="n">WienerService</span> <span class="o">{</span>
<span class="kd">private</span> <span class="kt">int</span> <span class="n">bunStock</span><span class="o">;</span>
<span class="kd">private</span> <span class="kt">int</span> <span class="n">wienerStock</span><span class="o">;</span>
<span class="kd">public</span> <span class="kd">synchronized</span> <span class="kt">void</span> <span class="nf">getBun</span><span class="o">()</span> <span class="o">{</span>
<span class="n">bunStock</span> <span class="o">=</span> <span class="n">bunStock</span> <span class="o">-</span> <span class="mi">1</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">synchronized</span> <span class="kt">void</span> <span class="nf">getWiener</span><span class="o">()</span> <span class="o">{</span>
<span class="n">wienerStock</span> <span class="o">=</span> <span class="n">wienerStock</span> <span class="o">-</span> <span class="mi">1</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
</pre></div>
<p>The stock accesses are synchronized to avoid multiple accesses at the same time. The component type metadata must also be modified in order to describe this property:</p>
<div class="codehilite"><pre><span class="nt">&lt;ipojo&gt;</span>
<span class="nt">&lt;component</span>
<span class="na">classname=</span><span class="s">&quot;org.apache.felix.ipojo.example.vendor.provider.BunProvider&quot;</span>
<span class="na">name=</span><span class="s">&quot;buns_and_wieners&quot;</span> <span class="na">public=</span><span class="s">&quot;false&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;provides&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;buns&quot;</span> <span class="na">field=</span><span class="s">&quot;bunStock&quot;</span> <span class="na">value=</span><span class="s">&quot;10&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;wieners&quot;</span> <span class="na">field=</span><span class="s">&quot;wienerStock&quot;</span> <span class="na">value=</span><span class="s">&quot;10&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/provides&gt;</span>
<span class="nt">&lt;/component&gt;</span>
<span class="nt">&lt;instance</span> <span class="na">component=</span><span class="s">&quot;buns_and_wieners&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/ipojo&gt;</span>
</pre></div>
<p>In the <code>provides</code> element, two properties are added. This property contains a <code>field</code> attribute aiming to attach the service property with a field of the implementation class. Then a default value is given. In the code, the property fields will obtain the initial value (10). Then each time the fields are modified, the service property is updated (as well as the OSGiâ„¢ service registration). Notice that iPOJO support method injection for property too. In this case, a getter method is called to inject the property value.</p>
<h2 id="configuring-instances">Configuring instances<a class="headerlink" href="#configuring-instances" title="Permanent link">&para;</a></h2>
<p>In the previous example, the properties were configured in the component type description. It is also possible to customize any property value in the instance declaration. This way, each instance can obtain different values.</p>
<div class="codehilite"><pre><span class="nt">&lt;ipojo&gt;</span>
<span class="nt">&lt;component</span>
<span class="na">classname=</span><span class="s">&quot;org.apache.felix.ipojo.example.vendor.provider.BunProvider&quot;</span>
<span class="na">name=</span><span class="s">&quot;buns_and_wieners&quot;</span> <span class="na">public=</span><span class="s">&quot;false&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;provides&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;buns&quot;</span> <span class="na">field=</span><span class="s">&quot;bunStock&quot;</span> <span class="na">value=</span><span class="s">&quot;10&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;wieners&quot;</span> <span class="na">field=</span><span class="s">&quot;wienerStock&quot;</span> <span class="na">value=</span><span class="s">&quot;10&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/provides&gt;</span>
<span class="nt">&lt;/component&gt;</span>
<span class="nt">&lt;instance</span> <span class="na">component=</span><span class="s">&quot;buns_and_wieners&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;buns&quot;</span> <span class="na">value=</span><span class="s">&quot;9&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;wieners&quot;</span> <span class="na">value=</span><span class="s">&quot;8&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/instance&gt;</span>
<span class="nt">&lt;/ipojo&gt;</span>
</pre></div>
<p>The previous metadata shows how to push a configuration in instance declarations. The instance declaration contains two property elements containing the name of the value of the property. Instance configuration override component type initial value. Properties are optional by default ; that's means that they do not need to receive a value. In this case, default values are the same as the Java default fields values (boolean : false, int : 0, double : 0.0d, ...). You can specify that a property must receive a default value from either the component type description or the instance configuration by setting the <code>mandatory</code> attribute to <code>true</code>. </p>
<h2 id="using-filter-in-service-requirements">Using filter in service requirements<a class="headerlink" href="#using-filter-in-service-requirements" title="Permanent link">&para;</a></h2>
<p>Now that bun and wiener providers publish their remaining stock, the hot dog provider can look for a bun service and a wiener service with a non empty stock. To achieve this, we must describe an LDAP filter in the service requirement description. The following XML snipped shows this metadata:</p>
<div class="codehilite"><pre><span class="nt">&lt;ipojo&gt;</span>
<span class="nt">&lt;component</span>
<span class="na">classname=</span><span class="s">&quot;org.apache.felix.ipojo.example.vendor.hotdog.HotDogVendor&quot;</span>
<span class="na">name=</span><span class="s">&quot;HD&quot;</span> <span class="na">public=</span><span class="s">&quot;false&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;provides&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;product&quot;</span> <span class="na">type=</span><span class="s">&quot;string&quot;</span> <span class="na">value=</span><span class="s">&quot;hotdog&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/provides&gt;</span>
<span class="nt">&lt;requires</span> <span class="na">field=</span><span class="s">&quot;bunProvider&quot;</span> <span class="na">filter=</span><span class="s">&quot;(buns&gt;=1)&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;requires</span> <span class="na">field=</span><span class="s">&quot;wienerProvider&quot;</span> <span class="na">filter=</span><span class="s">&quot;(wieners&gt;=1)&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/component&gt;</span>
<span class="nt">&lt;instance</span> <span class="na">component=</span><span class="s">&quot;HD&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/ipojo&gt;</span>
</pre></div>
<p>When a provider does no more matches with the LDAP filter, the provider is no more used, and another (matching with the filter) is tracked. If no provider fulfilling the constraint is found, the instance becomes invalid and waits a matching provider.</p>
<div class="alert alert-warning">
<strong>Instance invalidation and services</strong>
<br/>
When an instance becomes invalid, all its provided services are withdrawn from the service registry. So, this instance is no more *accessible* from the service registry.
</div>
<h2 id="immediate-component-instance">Immediate component instance<a class="headerlink" href="#immediate-component-instance" title="Permanent link">&para;</a></h2>
<p>Now that we get the hot dog provider, we are going to implement customers. Customers are implemented in the <em>vendor.customer</em> project). A customer simply looks for a vendor service and buys a product:</p>
<div class="codehilite"><pre><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Customer</span> <span class="o">{</span>
<span class="kd">private</span> <span class="n">VendorService</span> <span class="n">vendor</span><span class="o">;</span>
<span class="kd">private</span> <span class="n">String</span> <span class="n">name</span><span class="o">;</span>
<span class="kd">public</span> <span class="nf">Customer</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;Customer &quot;</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s">&quot; bought &quot;</span>
<span class="o">+</span> <span class="n">vendor</span><span class="o">.</span><span class="na">sell</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot; from &quot;</span> <span class="o">+</span> <span class="n">vendor</span><span class="o">.</span><span class="na">getName</span><span class="o">());</span>
<span class="o">}</span>
</pre></div>
<p>The previous code shows a possible implementation of a customer. However, the "sell" method is called in a constructor, and the constructor can only be called only if an object of the class is created. With iPOJO there are two different way to "activate" an instance as soon as it becomes valid. </p>
<p>The first one uses the lifecycle callback (described in the previous tutorial). The second one is by declaring the component as an immediate component. An immediate component instance creates an object of its implementation as soon as it becomes valid. </p>
<div class="codehilite"><pre><span class="nt">&lt;ipojo&gt;</span>
<span class="nt">&lt;component</span>
<span class="na">classname=</span><span class="s">&quot;org.apache.felix.ipojo.example.vendor.customer.Customer&quot;</span>
<span class="na">name=</span><span class="s">&quot;customer&quot;</span> <span class="na">immediate=</span><span class="s">&quot;true&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;requires</span> <span class="na">field=</span><span class="s">&quot;vendor&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">field=</span><span class="s">&quot;name&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/properties&gt;</span>
<span class="nt">&lt;/component&gt;</span>
<span class="nt">&lt;/ipojo&gt;</span>
</pre></div>
<p>To declare a component immediate, just add <code>immediate=true</code> in the component descriptor. Then as soon as the vendor service is available, the object is created. Moreover, this type declares a property (to give a name to the customers). This property is not a service property, but just an internal property. As for service properties, the name field will be injected by a value necessary given during the instance creation (i.e. contained inside the instance configuration).</p>
<p>By default, all all components that do not provide any service are immediate. Other components create call their constructors when they are used for the first time. </p>
<div class="alert alert-warning">
<strong>Difference between 'validate' and 'immediate'</strong>
<br/>
There is a difference between immediate components and components with a <code>validate</code> lifecycle callback. Indeed, the callback is call at each time the instance becomes valid and calls the constructor only if no object already exists. On the other side, the immediate component's constructor is call only once.
</div>
<h2 id="creating-instances-from-an-external-component-type">Creating instances from an external component type<a class="headerlink" href="#creating-instances-from-an-external-component-type" title="Permanent link">&para;</a></h2>
<p>In the previous section we have declared a customer component type, which does not have the <code>public=false</code> attribute. This feature allows separate deployment from instance creation. Moreover, we didn't declare instances in the descriptor. </p>
<p>Another metadata file can be used to declare instances from the customer type, this descriptor being contained in another bundle. The following descriptor creates 10 customer instances (look at the <em>vendor.customer.creator\metadata.xml</em> file):</p>
<div class="codehilite"><pre><span class="nt">&lt;ipojo&gt;</span>
<span class="nt">&lt;instance</span> <span class="na">component=</span><span class="s">&quot;customer&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;name&quot;</span> <span class="na">value=</span><span class="s">&quot;customer-1&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/instance&gt;</span>
<span class="nt">&lt;instance</span> <span class="na">component=</span><span class="s">&quot;customer&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;name&quot;</span> <span class="na">value=</span><span class="s">&quot;customer-2&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/instance&gt;</span>
<span class="nt">&lt;instance</span> <span class="na">component=</span><span class="s">&quot;customer&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;name&quot;</span> <span class="na">value=</span><span class="s">&quot;customer-3&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/instance&gt;</span>
<span class="nt">&lt;instance</span> <span class="na">component=</span><span class="s">&quot;customer&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;name&quot;</span> <span class="na">value=</span><span class="s">&quot;customer-4&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/instance&gt;</span>
<span class="nt">&lt;instance</span> <span class="na">component=</span><span class="s">&quot;customer&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;name&quot;</span> <span class="na">value=</span><span class="s">&quot;customer-5&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/instance&gt;</span>
<span class="nt">&lt;instance</span> <span class="na">component=</span><span class="s">&quot;customer&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;name&quot;</span> <span class="na">value=</span><span class="s">&quot;customer-6&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/instance&gt;</span>
<span class="nt">&lt;instance</span> <span class="na">component=</span><span class="s">&quot;customer&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;name&quot;</span> <span class="na">value=</span><span class="s">&quot;customer-7&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/instance&gt;</span>
<span class="nt">&lt;instance</span> <span class="na">component=</span><span class="s">&quot;customer&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;name&quot;</span> <span class="na">value=</span><span class="s">&quot;customer-8&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/instance&gt;</span>
<span class="nt">&lt;instance</span> <span class="na">component=</span><span class="s">&quot;customer&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;name&quot;</span> <span class="na">value=</span><span class="s">&quot;customer-9&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/instance&gt;</span>
<span class="nt">&lt;instance</span> <span class="na">component=</span><span class="s">&quot;customer&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;name&quot;</span> <span class="na">value=</span><span class="s">&quot;customer-10&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/instance&gt;</span>
<span class="nt">&lt;/ipojo&gt;</span>
</pre></div>
<p>Once deployed, this bundle looks for the required factory. If it's not available the bundle waits for the factory. As soon as the required factory is available, all instances are created. When this bundle is stopped, all instances are destroyed. </p>
<h2 id="deploying-the-application">Deploying the application<a class="headerlink" href="#deploying-the-application" title="Permanent link">&para;</a></h2>
<p>Compile the bundles, by launching ant at the root of the tutorial. Then launch Felix is indicated above.
Once started, launch the following commands </p>
<div class="codehilite"><pre>start file:../vendor.services/output/vendor.services.jar
start file:../vendor.buns-and-wieners/output/vendor.buns-and-wieners.jar
start file:../vendor.hotdog/output/vendor.hotdog.jar
start file:../vendor.customer/output/vendor.customer.jar
start file:../vendor.customer.creator/output/vendor.customer.creator.jar
</pre></div>
<p>Something like this should appear:</p>
<div class="codehilite"><pre>Customer customer-1 bought Hotdog from Fenway Park
Customer customer-2 bought Hotdog from Fenway Park
Customer customer-3 bought Hotdog from Fenway Park
Customer customer-4 bought Hotdog from Fenway Park
Customer customer-5 bought Hotdog from Fenway Park
Customer customer-6 bought Hotdog from Fenway Park
Customer customer-7 bought Hotdog from Fenway Park
Customer customer-8 bought Hotdog from Fenway Park
</pre></div>
<p>Only 8 customers can buy a hot-dog, as the stock of wieners and buns can't supply more hot-dog. The remainder of this tutorial will try to solve the problem of these two hungry customers.</p>
<h2 id="using-the-lifecycle-controller">Using the lifecycle controller<a class="headerlink" href="#using-the-lifecycle-controller" title="Permanent link">&para;</a></h2>
<p>Sometimes you want to invalidate your instance in the code (for example: to unregister a service). That's possible with the lifecycle controller handler.
Let's take the popcorn vendor with a corn stock from the <em>vendor.popcorn</em> project. Each time it sells some popcorn, its stock is decreased. When the stock reaches 0, it cannot sell popcorns any more (so the vendor service needs to be withdrawn).</p>
<p>The following implementation (<em>src\org\apache\felix\ipojo\example\vendor\popcorn\PopCornVendor.java</em>) uses a field to control the lifecycle.</p>
<div class="codehilite"><pre><span class="cm">/**</span>
<span class="cm"> * The corn stock.</span>
<span class="cm"> */</span>
<span class="kd">private</span> <span class="kt">int</span> <span class="n">m_corn_stock</span><span class="o">;</span>
<span class="cm">/**</span>
<span class="cm"> * Lifecycle controller.</span>
<span class="cm"> * If set to false, the instance becomes invalid. </span>
<span class="cm"> */</span>
<span class="kd">private</span> <span class="kt">boolean</span> <span class="n">m_can_sell</span> <span class="o">=</span> <span class="kc">true</span><span class="o">;</span>
<span class="cm">/**</span>
<span class="cm"> * The sell method.</span>
<span class="cm"> * To provide popcorn, the vendor needs to decrease its corn stock level.</span>
<span class="cm"> * This method is synchronized to avoid to client being serve at </span>
<span class="cm"> * the same time. </span>
<span class="cm"> * @return</span>
<span class="cm"> * @see org.apache.felix.ipojo.example.vendor.service.Vendor#sell()</span>
<span class="cm"> */</span>
<span class="kd">public</span> <span class="kd">synchronized</span> <span class="n">Product</span> <span class="nf">sell</span><span class="o">()</span> <span class="o">{</span>
<span class="n">m_corn_stock</span><span class="o">--;</span>
<span class="k">if</span> <span class="o">(</span><span class="n">m_corn_stock</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="n">m_can_sell</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// Last pop corn</span>
<span class="n">m_can_sell</span> <span class="o">=</span> <span class="kc">false</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;Stop selling popcorn </span>
<span class="s"> ... Run out of stock&quot;</span><span class="o">);</span>
<span class="k">return</span> <span class="k">new</span> <span class="nf">PopCorn</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">m_corn_stock</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// Normal case</span>
<span class="k">return</span> <span class="k">new</span> <span class="nf">PopCorn</span><span class="o">();</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span> <span class="c1">// Cannot serve.</span>
<span class="k">return</span> <span class="n">PopCorn</span><span class="o">.</span><span class="na">NO_MORE_POPCORN</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
</pre></div>
<p>Once the field is set to "false", the instance is invalidated (the vendor service is no more available). To configure the controller, you can use the following metadata:</p>
<div class="codehilite"><pre><span class="nt">&lt;ipojo&gt;</span>
<span class="nt">&lt;component</span>
<span class="na">classname=</span><span class="s">&quot;org.apache.felix.ipojo.example.vendor.popcorn.PopCornVendor&quot;</span>
<span class="na">name=</span><span class="s">&quot;popcorn&quot;</span> <span class="na">public=</span><span class="s">&quot;false&quot;</span> <span class="na">architecture=</span><span class="s">&quot;true&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;provides/&gt;</span>
<span class="nt">&lt;controller</span> <span class="na">field=</span><span class="s">&quot;m_can_sell&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/component&gt;</span>
<span class="nt">&lt;instance</span> <span class="na">component=</span><span class="s">&quot;popcorn&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/ipojo&gt;</span>
</pre></div>
<p>The instance can be re-validated by setting the field to true.
So, no deploy the pop corn vendor.</p>
<div class="codehilite"><pre>-&gt; start file:../vendor.popcorn/output/vendor.popcorn.jar
Customer customer-10 bought popcorn from D &amp; P
Customer customer-9 bought popcorn from D &amp; P
</pre></div>
<p>Our two last customers are no more hungry. However, new customers arrives, we have the following situation:</p>
<div class="codehilite"><pre>-&gt; update 10
Customer customer-1 bought popcorn from D &amp; P
Customer customer-2 bought popcorn from D &amp; P
Stop selling popcorn ... Run out of stock
Customer customer-3 bought popcorn from D &amp; P
</pre></div>
<p>To recreate new customers, just update the customer.creator bundle (bundle 10). So, now we have 7 customers hungry! There is neither popcorn nor hotdog!</p>
<h2 id="reconfiguring-an-instance">Reconfiguring an instance<a class="headerlink" href="#reconfiguring-an-instance" title="Permanent link">&para;</a></h2>
<p>OSGi specified the Configuration Admin mechanism aiming to handler service and bundle configuration. This section will describe how you can use the Configuration Admin and iPOJO to add corn inside our popcorn vendor.
First, we will change the pop corn vendor to add a method reinjecting the new stock:</p>
<div class="codehilite"><pre><span class="cm">/**</span>
<span class="cm"> * A transporter refills the stock of corn.</span>
<span class="cm"> * This method is synchronized to avoid to client being served </span>
<span class="cm"> * during the update.</span>
<span class="cm"> * @param newStock : the stock of corn to add to the current stock.</span>
<span class="cm"> */</span>
<span class="kd">public</span> <span class="kd">synchronized</span> <span class="kt">void</span> <span class="nf">refillStock</span><span class="o">(</span><span class="kt">int</span> <span class="n">newStock</span><span class="o">)</span> <span class="o">{</span>
<span class="n">m_corn_stock</span> <span class="o">+=</span> <span class="n">newStock</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;Refill the stock : &quot;</span> <span class="o">+</span> <span class="n">m_corn_stock</span><span class="o">);</span>
<span class="k">if</span> <span class="o">(</span><span class="n">m_corn_stock</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span>
<span class="n">m_can_sell</span> <span class="o">=</span> <span class="kc">true</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
</pre></div>
<p>Once added, we need to update the component type descriptor to use this method:</p>
<div class="codehilite"><pre><span class="nt">&lt;ipojo&gt;</span>
<span class="nt">&lt;component</span>
<span class="na">classname=</span><span class="s">&quot;org.apache.felix.ipojo.example.vendor.popcorn.PopCornVendor&quot;</span>
<span class="na">name=</span><span class="s">&quot;popcorn&quot;</span> <span class="na">architecture=</span><span class="s">&quot;true&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;provides/&gt;</span>
<span class="nt">&lt;controller</span> <span class="na">field=</span><span class="s">&quot;m_can_sell&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;stock&quot;</span> <span class="na">method=</span><span class="s">&quot;refillStock&quot;</span> <span class="na">value=</span><span class="s">&quot;5&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/properties&gt;</span>
<span class="nt">&lt;/component&gt;</span>
<span class="nt">&lt;instance</span> <span class="na">component=</span><span class="s">&quot;popcorn&quot;</span> <span class="na">name=</span><span class="s">&quot;SuperPopCorn&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;managed.service.pid&quot;</span> <span class="na">value=</span><span class="s">&quot;Super.PopCorn.Stock&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/instance&gt;</span>
<span class="nt">&lt;/ipojo&gt;</span>
</pre></div>
<p>We add two different things. First we add a "stock" property attached to the <em>refillStock</em> method. When this instance is configured or reconfigured, this method is called to push the new stock value. Then we add the <em>managed.service.pid</em> property inside the instance creation. This property will be used by the Configuration Admin to attach configuration to instances. The property value must be unique.
So now, our popcorn vendor can be reconfigured dynamically to get increments its corn stock.
However, we need to create something refilling the stock ... a corn transporter !</p>
<p>Inside the <em>vendor.corn.transporter</em> project, we have a component dealing with the ConfigurationAdmin to push the new pop corn vendor configuration.
The implementation is contained in the <em>src\org\apache\felix\ipojo\example\vendor\corn\transporter\CornTransporter.java</em> file.</p>
<div class="codehilite"><pre><span class="kd">public</span> <span class="kd">class</span> <span class="nc">CornTransporter</span> <span class="o">{</span>
<span class="kd">private</span> <span class="n">ConfigurationAdmin</span> <span class="n">m_configAdmin</span><span class="o">;</span>
<span class="cm">/**</span>
<span class="cm"> * Reconfigure the popcorn vendor with the configuration admin. </span>
<span class="cm"> */</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">refillStock</span><span class="o">()</span> <span class="o">{</span>
<span class="k">try</span> <span class="o">{</span>
<span class="c1">// Retrieve or Create the instance configuration</span>
<span class="c1">// from the configuration admin</span>
<span class="n">Configuration</span> <span class="n">configuration</span> <span class="o">=</span>
<span class="n">m_configAdmin</span><span class="o">.</span><span class="na">getConfiguration</span><span class="o">(</span><span class="s">&quot;Super.PopCorn.Stock&quot;</span><span class="o">,</span>
<span class="s">&quot;file:../vendor.popcorn/output/vendor.popcorn.jar&quot;</span><span class="o">);</span>
<span class="n">configuration</span><span class="o">.</span><span class="na">setBundleLocation</span><span class="o">(</span>
<span class="s">&quot;file:../vendor.popcorn/output/vendor.popcorn.jar&quot;</span><span class="o">);</span>
<span class="n">Properties</span> <span class="n">props</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Properties</span><span class="o">();</span>
<span class="n">props</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="s">&quot;stock&quot;</span><span class="o">,</span> <span class="k">new</span> <span class="n">Integer</span><span class="o">(</span><span class="mi">15</span><span class="o">));</span> <span class="c1">// Delivered corn</span>
<span class="n">configuration</span><span class="o">.</span><span class="na">update</span><span class="o">(</span><span class="n">props</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;Update the configuration of &quot;</span>
<span class="o">+</span> <span class="n">configuration</span><span class="o">.</span><span class="na">getPid</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot;(&quot;</span>
<span class="o">+</span> <span class="n">configuration</span><span class="o">.</span><span class="na">getBundleLocation</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot;)&quot;</span><span class="o">);</span>
<span class="n">configuration</span><span class="o">.</span><span class="na">delete</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="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
</pre></div>
<p>Create a new configuration from the configuration admin and configure this configuration to add corn. Then, we update this configuration. This will reconfigured our popcorn vendor. More information on the Configuration Admin is available in the OSGi R4 Compendium.</p>
<p>So, now if we deploy this bundle, we will provide enough corn to feed all the customers:</p>
<div class="codehilite"><pre>-&gt; start file:../vendor.corn.transporter/output/vendor.corn.transporter.jar
Update configuration of Super.PopCorn.Stock<span class="o">(</span>
file:../vendor.popcorn/output/vendor.popcorn.jar<span class="o">)</span>
Refill the stock : 5
Customer customer-10 bought popcorn from D &amp; P
Customer customer-9 bought popcorn from D &amp; P
Customer customer-8 bought popcorn from D &amp; P
Customer customer-7 bought popcorn from D &amp; P
Customer customer-6 bought popcorn from D &amp; P
Customer customer-5 bought popcorn from D &amp; P
Customer customer-4 bought popcorn from D &amp; P
</pre></div>
<p>That's it!</p>
<h2 id="conclusion">Conclusion<a class="headerlink" href="#conclusion" title="Permanent link">&para;</a></h2>
<p>This small tutorial has presented some of of the iPOJO features. 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. 1700393 by cziegeler on Tue, 1 Sep 2015 06:04:06 +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>