| <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| <html><head> |
| |
| |
| |
| <title>Apache Felix - apache-felix-ipojo-api</title> |
| <link rel="stylesheet" href="apache-felix-ipojo-api_files/site.css" type="text/css" media="all"> |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| </head><body> |
| <div class="title"><div class="logo"><a href="http://felix.apache.org/site/index.html"><img alt="Apache Felix" src="apache-felix-ipojo-api_files/logo.png" border="0"></a></div><div class="header"><a href="http://www.apache.org/"><img alt="Apache" src="apache-felix-ipojo-api_files/apache.png" border="0"></a></div></div> |
| <div class="menu"> |
| <ul> |
| <li><a href="http://felix.apache.org/site/news.html" title="news">news</a></li> |
| <li><a href="http://felix.apache.org/site/license.html" title="license">license</a></li> |
| <li><a href="http://felix.apache.org/site/downloads.cgi" rel="nofollow">downloads</a></li> |
| <li><a href="http://felix.apache.org/site/documentation.html" title="documentation">documentation</a></li> |
| <li><a href="http://felix.apache.org/site/mailinglists.html" title="mailinglists">mailing lists</a></li> |
| <li><a href="http://felix.apache.org/site/contributing.html" title="Contributing">contributing</a></li> |
| <li><a href="http://www.apache.org/" rel="nofollow">asf</a></li> |
| <li><a href="http://www.apache.org/foundation/sponsorship.html" rel="nofollow">sponsorship</a></li> |
| <li><a href="http://www.apache.org/foundation/thanks.html" rel="nofollow">sponsors</a> |
| <!-- ApacheCon Ad --> |
| <iframe src="apache-felix-ipojo-api_files/button.html" style="border-width: 0pt; float: left;" scrolling="no" frameborder="0" height="135" width="135"></iframe> |
| <p style="height: 100px;"> |
| <!-- ApacheCon Ad --> |
| </p></li></ul> </div> |
| <div class="main"> |
| <style type="text/css"> |
| @import url(http://people.apache.org/~clement/ipojo/site/superfish.css); |
| </style> |
| |
| <style type="text/css"> |
| @import url(http://people.apache.org/~clement/ipojo/site/style.css); |
| </style> |
| |
| <p> |
| <script class="javascript" src="apache-felix-ipojo-api_files/shCore.js"></script> |
| <script class="javascript" src="apache-felix-ipojo-api_files/shBrushCSharp.js"></script> |
| <script class="javascript" src="apache-felix-ipojo-api_files/shBrushPhp.js"></script> |
| <script class="javascript" src="apache-felix-ipojo-api_files/shBrushJScript.js"></script> |
| <script class="javascript" src="apache-felix-ipojo-api_files/shBrushVb.js"></script> |
| <script class="javascript" src="apache-felix-ipojo-api_files/shBrushSql.js"></script> |
| <script class="javascript" src="apache-felix-ipojo-api_files/shBrushXml.js"></script> |
| <script class="javascript" src="apache-felix-ipojo-api_files/shBrushShell.js"></script> |
| <script class="javascript" src="apache-felix-ipojo-api_files/shBrushDelphi.js"></script> |
| <script class="javascript" src="apache-felix-ipojo-api_files/shBrushPython.js"></script> |
| <script class="javascript" src="apache-felix-ipojo-api_files/shBrushJava.js"></script> |
| |
| <script type="text/javascript" src="apache-felix-ipojo-api_files/jquery-1.js"></script> |
| <script type="text/javascript" src="apache-felix-ipojo-api_files/hoverIntent.js"></script> |
| <script type="text/javascript" src="apache-felix-ipojo-api_files/superfish.js"></script> |
| <script type="text/javascript" src="apache-felix-ipojo-api_files/supersubs.js"></script> |
| |
| <script type="text/javascript"> |
| |
| $(document).ready(function(){ |
| $("ul.sf-menu").supersubs({ |
| minWidth: 14, // minimum width of sub-menus in em units |
| maxWidth: 30, // maximum width of sub-menus in em units |
| extraWidth: 1 // extra width can ensure lines don't sometimes turn over |
| // due to slight rounding differences and font-family |
| }).superfish(); // call supersubs first, then superfish, so that subs are |
| // not display:none when measuring. Call before initialising |
| // containing tabs for same reason. |
| }); |
| |
| </script> |
| </p><div class="main"> |
| <div class="page-header"> |
| <img src="apache-felix-ipojo-api_files/header.png" class="header"> |
| <a href="http://ipojo.org/"><img src="apache-felix-ipojo-api_files/ipojo.png" class="header-logo" width="225"></a> |
| <ul class="sf-menu sf-js-enabled sf-shadow" id="ipojo-menu"> |
| <li class="current"> |
| <!-- Menu Overview --> |
| <a href="" class="sf-with-ul">Overview<span class="sf-sub-indicator"> »</span><span class="sf-sub-indicator"> »</span><span class="sf-sub-indicator"> »</span></a> |
| <ul style="float: none; width: 14em; display: none; visibility: hidden;"> |
| <li style="white-space: normal; float: left; width: 100%;"> |
| <a style="float: none; width: auto;" href="http://felix.apache.org/site/apache-felix-ipojo.html" title="Apache Felix iPOJO">Home</a> |
| </li> |
| <li style="white-space: normal; float: left; width: 100%;"> |
| <a style="float: none; width: auto;" href="http://felix.apache.org/site/apache-felix-ipojo-why-choose-ipojo.html" title="apache-felix-ipojo-why-choose-ipojo">Why choose iPOJO</a> |
| </li> |
| <li style="white-space: normal; float: left; width: 100%;"> |
| <a style="float: none; width: auto;" href="http://felix.apache.org/site/apache-felix-ipojo-successstories.html" title="apache-felix-ipojo-successstories">Success stories</a> |
| </li> |
| <li style="white-space: normal; float: left; width: 100%;"> |
| <a style="float: none; width: auto;" href="http://felix.apache.org/site/apache-felix-ipojo-feature-overview.html" title="Apache Felix iPOJO Feature Overview">Features</a> |
| </li> |
| </ul> |
| </li> |
| |
| <li class=""> |
| <!-- Menu download --> |
| </li><li> |
| <a href="http://felix.apache.org/site/download.html" title="Download">Download </a> |
| </li> |
| |
| <li class=""> |
| <!-- Menu Documentation --> |
| <a href="" class="sf-with-ul">Documentation<span class="sf-sub-indicator"> »</span><span class="sf-sub-indicator"> »</span><span class="sf-sub-indicator"> »</span></a> |
| <ul style="float: none; width: 14em; display: none; visibility: hidden;"> |
| <!-- sub- menu : getting started --> |
| <li style="white-space: normal; float: left; width: 100%;" class=""> |
| <a style="float: none; width: auto;" href="" class="sf-with-ul">Getting Started<span class="sf-sub-indicator"> »</span><span class="sf-sub-indicator"> »</span><span class="sf-sub-indicator"> »</span></a> |
| <ul style="left: 14em; float: none; width: 14em; display: none; visibility: hidden;"> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/ipojo-in-10-minutes.html" title="iPOJO in 10 minutes">iPOJO in 10 minutes</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/how-to-use-ipojo-annotations.html" title="How to use iPOJO Annotations">Using Annotations</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/ipojo-hello-word-maven-based-tutorial.html" title="iPOJO Hello Word (Maven-Based) tutorial">Maven tutorial</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/ipojo-advanced-tutorial.html" title="iPOJO Advanced Tutorial">Advanced tutorial</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/apache-felix-ipojo-dosgi.html" title="apache-felix-ipojo-dosgi">Using Distributed OSGi</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/ipojo-composition-tutorial.html" title="iPOJO Composition Tutorial">Application Composition</a></li> |
| </ul> |
| </li> <!-- end of getting started --> |
| <!-- sub menu : Describing Components --> |
| <li style="white-space: normal; float: left; width: 100%;" class=""> |
| <a style="float: none; width: auto;" href="http://felix.apache.org/site/describing-components.html" class="sf-with-ul">Describing components<span class="sf-sub-indicator"> »</span><span class="sf-sub-indicator"> »</span><span class="sf-sub-indicator"> »</span></a> |
| <ul style="left: 14em; float: none; width: 14em; display: none; visibility: hidden;"> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/service-requirement-handler.html" title="Service Requirement Handler">Requiring a service</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/providing-osgi-services.html" title="Providing OSGi services">Providing a service</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/lifecycle-callback-handler.html" title="Lifecycle Callback Handler">Lifecycle management</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/configuration-handler.html" title="Configuration Handler">Configuration</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/architecture-handler.html" title="Architecture Handler">Introspection</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/controller-lifecycle-handler.html" title="Controller Lifecycle Handler">Impacting the lifecycle</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/event-admin-handlers.html" title="Event Admin Handlers">Asynchronous communication</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/ipojo-jmx-handler.html" title="iPOJO JMX Handler">JMX management</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/extender-pattern-handler.html" title="Extender Pattern Handler">Extender pattern</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/white-board-pattern-handler.html" title="White Board Pattern Handler">Whiteboard pattern</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/temporal-service-dependency.html" title="Temporal Service Dependency">Temporal dependencies</a></li> |
| </ul> |
| </li> <!-- End of describing components --> |
| <!-- sub- menu : User Guide --> |
| <li style="white-space: normal; float: left; width: 100%;" class=""> |
| <a style="float: none; width: auto;" href="" class="sf-with-ul">User Guide<span class="sf-sub-indicator"> »</span><span class="sf-sub-indicator"> »</span><span class="sf-sub-indicator"> »</span></a> |
| <ul style="left: 14em; float: none; width: 14em; display: none; visibility: hidden;"> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/using-xml-schemas.html" title="Using XML Schemas">XML Schemas</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="" title="apache-felix-ipojo-api">API</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/apache-felix-ipojo-testing-components.html" title="apache-felix-ipojo-testing-components">Testing components</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/apache-felix-ipojo-eclipse-integration.html" title="apache-felix-ipojo-eclipse-integration">Eclipse Integration</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/ipojo-faq.html" title="iPOJO FAQ">FAQ</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/ipojo-reference-card.html" title="iPOJO-Reference-Card">Reference Card</a></li> |
| </ul> |
| </li> <!-- end of user guide --> |
| <!-- sub- menu : Dev Guide --> |
| <li class="" style="white-space: normal; float: left; width: 100%;"> |
| <a style="float: none; width: auto;" href="" class="sf-with-ul">Advanced Topics<span class="sf-sub-indicator"> »</span><span class="sf-sub-indicator"> »</span><span class="sf-sub-indicator"> »</span></a> |
| <ul style="left: 14em; float: none; width: 14em; display: none; visibility: hidden;"> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/ipojo/api/1.2.0" rel="nofollow">Javadoc</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/how-to-write-your-own-handler.html" title="How to write your own handler">Handler guide</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/how-to-use-ipojo-manipulation-metadata.html" title="How to use iPOJO Manipulation Metadata">Manipulation Metadata </a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/dive-into-the-ipojo-manipulation-depths.html" title="Dive into the iPOJO Manipulation depths">Dive into the iPOJO Manipulation depths</a></li> |
| </ul> |
| </li> <!-- End of Dev guide --> |
| </ul> |
| </li> <!-- End of doc --> |
| <!-- Menu 4 : Tools --> |
| <li class=""> |
| <a href="" class="sf-with-ul">Tools<span class="sf-sub-indicator"> »</span><span class="sf-sub-indicator"> »</span><span class="sf-sub-indicator"> »</span></a> |
| <ul style="float: none; width: 14em; display: none; visibility: hidden;"> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/ipojo-ant-task.html" title="iPOJO Ant Task">Ant Task</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/ipojo-eclipse-plug-in.html" title="iPOJO Eclipse Plug-in">Eclipse Plugin</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/ipojo-maven-plug-in.html" title="iPOJO Maven Plug-in">Maven Plugin</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/apache-felix-ipojo-online-manipulator.html" title="apache-felix-ipojo-online-manipulator">Online Manipulator</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/ipojo-webconsole-plugin.html" title="iPOJO Webconsole Plugin">Webconsole plugin</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/apache-felix-ipojo-junit4osgi.html" title="apache-felix-ipojo-junit4osgi">Junit4OSGi</a></li> |
| </ul> |
| </li><!-- End of tools --> |
| <!-- Menu 5 : Misc --> |
| <li class=""> |
| <a href="" class="sf-with-ul">Misc<span class="sf-sub-indicator"> »</span><span class="sf-sub-indicator"> »</span><span class="sf-sub-indicator"> »</span></a> |
| <ul style="float: none; width: 14em; display: none; visibility: hidden;"> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/apache-felix-ipojo-issuestracker.html" title="apache-felix-ipojo-issuestracker">Issues Tracker</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/apache-felix-ipojo-supportedvms.html" title="apache-felix-ipojo-supportedVMs">Supported JVMs</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/apache-felix-ipojo-supportedosgi.html" title="apache-felix-ipojo-supportedOSGi">Supported OSGi Implementations</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://ipojo-dark-side.blogspot.com/" rel="nofollow">iPOJO's Dark Side Blog</a></li> |
| <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="http://felix.apache.org/site/article-presentations.html" title="Article & Presentations">Article & Presentations</a></li> |
| </ul> |
| </li><!-- End of misc --> |
| </ul> <!-- End of the menu --> |
| </div> <!-- Page header --> |
| |
| |
| <div class="content"> |
| |
| <h1><a name="apache-felix-ipojo-api-iPOJOAPI"></a>iPOJO API</h1> |
| |
| <p><em>The iPOJO API provides a third way to describe iPOJO components. |
| With the API, you can dynamically create new components types and |
| create instances from them. Your component types are described with an |
| API. To use the API, deploy and start the iPOJO-API bundle and the |
| iPOJO core bundle.</em></p> |
| |
| <div class="toc"> |
| <ul> |
| <li><a href="#apache-felix-ipojo-api-Asimpleexample">A simple example</a></li> |
| <li><a href="#apache-felix-ipojo-api-PrimitiveComponentTypebasics">Primitive Component Type basics</a></li> |
| <li><a href="#apache-felix-ipojo-api-ImmediateComponent%252CValidateandInvalidateMethod">Immediate Component, Validate and Invalidate Method</a></li> |
| <li><a href="#apache-felix-ipojo-api-Declaringservices">Declaring services</a></li> |
| <li><a href="#apache-felix-ipojo-api-ServiceDependencies">Service Dependencies</a></li> |
| <li><a href="#apache-felix-ipojo-api-Properties">Properties</a></li> |
| <li><a href="#apache-felix-ipojo-api-TemporalDependencies">Temporal Dependencies</a></li> |
| <li><a href="#apache-felix-ipojo-api-Instancecreation">Instance creation</a></li> |
| <li><a href="#apache-felix-ipojo-api-ManagedServiceandPropagation">Managed Service and Propagation</a></li> |
| <li><a href="#apache-felix-ipojo-api-ManagingiPOJOFactory">Managing iPOJO Factory</a></li> |
| <li><a href="#apache-felix-ipojo-api-AccesstotheintrospectionAPI">Access to the introspection API</a></li> |
| <li><a href="#apache-felix-ipojo-api-SingletonComponentType">Singleton Component Type</a></li> |
| <li><a href="#apache-felix-ipojo-api-Usingexternalhandlers">Using external handlers</a></li> |
| <li><a href="#apache-felix-ipojo-api-CreatingcompositionwiththeAPI">Creating composition with the API</a></li> |
| </ul></div> |
| |
| |
| <div class="panelMacro"><table class="infoMacro"><colgroup><col width="24"><col></colgroup><tbody><tr><td valign="top"><img src="apache-felix-ipojo-api_files/information.gif" alt="" border="0" align="absmiddle" height="16" width="16"></td><td><b>1.3.0-SNAPSHOT feature</b><br><p>The API is a 1.3.0-SNAPSHOT feature, and is not available in the 1.2.0 release.</p></td></tr></tbody></table></div> |
| |
| <h2><a name="apache-felix-ipojo-api-Asimpleexample"></a>A simple example</h2> |
| |
| <p>Let's imagine a simple component providing a service Foo. The following code is the implementation class of our component:</p> |
| <div class="code"> |
| <div class="dp-highlighter"><table class="dp-c" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="line1"><span></span><span class="keyword">package</span><span> org.example.service.impl; </span></td></tr><tr><td class="line2"> </td></tr><tr><td class="line1"><span class="keyword">import</span><span> org.example.service.Foo; </span></td></tr><tr><td class="line2"> </td></tr><tr><td class="line1"><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> FooImpl </span><span class="keyword">implements</span><span> Foo { </span></td></tr><tr><td class="line2"> </td></tr><tr><td class="line1"> <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> doSomething() { </span></td></tr><tr><td class="line2"> <span class="comment">// Do something...</span><span> </span></td></tr><tr><td class="line1"> } </td></tr><tr><td class="line2"> </td></tr><tr><td class="line1">} </td></tr></tbody></table></div><textarea style="display: none;" name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">package org.example.service.impl; |
| |
| import org.example.service.Foo; |
| |
| public class FooImpl implements Foo { |
| |
| public void doSomething() { |
| // Do something... |
| } |
| |
| }</textarea> |
| <script class="javascript"> |
| if(!window.newcodemacro_initialised) |
| { |
| window.newcodemacro_initialised = true; |
| window.oldonloadmethod = window.onload; |
| window.onload = function(){ |
| dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| if(window.oldonloadmethod) |
| { |
| window.oldonloadmethod(); |
| } |
| } |
| } |
| |
| </script> |
| </div> |
| |
| |
| <p>To create the component type and an instance of the component type |
| just create a class, get the bundle context (either from a |
| Bundle-Activator, or from an iPOJO component), and write the following |
| code:</p> |
| <div class="code"> |
| <div class="dp-highlighter"><table class="dp-c" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="line1"><span>new PrimitiveComponentType() </span></td></tr><tr><td class="line2"> .setBundleContext(context) </td></tr><tr><td class="line1"> .setClassName(FooImpl.<span class="keyword">class</span><span>.getName()) </span></td></tr><tr><td class="line2"> .addService(new Service()) <span class="comment">// Provide the Foo service</span><span> </span></td></tr><tr><td class="line1"> .createInstance(); <span class="comment">// Create the instance</span><span> </span></td></tr></tbody></table></div><textarea style="display: none;" name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">new PrimitiveComponentType() |
| .setBundleContext(context) |
| .setClassName(FooImpl.class.getName()) |
| .addService(new Service()) // Provide the Foo service |
| .createInstance(); // Create the instance</textarea> |
| <script class="javascript"> |
| if(!window.newcodemacro_initialised) |
| { |
| window.newcodemacro_initialised = true; |
| window.oldonloadmethod = window.onload; |
| window.onload = function(){ |
| dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| if(window.oldonloadmethod) |
| { |
| window.oldonloadmethod(); |
| } |
| } |
| } |
| |
| </script> |
| </div> |
| |
| |
| <p>So, now let's imagine another component using this service. The |
| following code is the implementation class of this component type:</p> |
| <div class="code"> |
| <div class="dp-highlighter"><table class="dp-c" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="line1"><span></span><span class="keyword">package</span><span> org.example.service.impl; </span></td></tr><tr><td class="line2"> </td></tr><tr><td class="line1"><span class="keyword">import</span><span> org.example.service.Foo; </span></td></tr><tr><td class="line2"> </td></tr><tr><td class="line1"><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> MyComponentImpl { </span></td></tr><tr><td class="line2"> <span class="keyword">private</span><span> Foo myFoo; </span></td></tr><tr><td class="line1"> </td></tr><tr><td class="line2"> <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> start() { </span></td></tr><tr><td class="line1"> myFoo.doSomething(); </td></tr><tr><td class="line2"> } </td></tr><tr><td class="line1"> </td></tr><tr><td class="line2">} </td></tr></tbody></table></div><textarea style="display: none;" name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">package org.example.service.impl; |
| |
| import org.example.service.Foo; |
| |
| public class MyComponentImpl { |
| private Foo myFoo; |
| |
| public void start() { |
| myFoo.doSomething(); |
| } |
| |
| }</textarea> |
| <script class="javascript"> |
| if(!window.newcodemacro_initialised) |
| { |
| window.newcodemacro_initialised = true; |
| window.oldonloadmethod = window.onload; |
| window.onload = function(){ |
| dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| if(window.oldonloadmethod) |
| { |
| window.oldonloadmethod(); |
| } |
| } |
| } |
| |
| </script> |
| </div> |
| |
| |
| <p>It is a regular iPOJO component expecting to get a Foo service in the <tt>myFoo</tt> |
| field. It also has a method executed when the instance is valid using |
| the Foo service. To describe this component, just write the following |
| lines:</p> |
| <div class="code"> |
| <div class="dp-highlighter"><table class="dp-c" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="line1"><span>new PrimitiveComponentType() </span></td></tr><tr><td class="line2"> .setBundleContext(context) </td></tr><tr><td class="line1"> .setClassName(MyComponentImpl.<span class="keyword">class</span><span>.getName()) </span></td></tr><tr><td class="line2"> .addDependency(new Dependency().setField(<span class="string">"myFoo"</span><span>)) </span></td></tr><tr><td class="line1"> .setValidateMethod(<span class="string">"start"</span><span>) </span></td></tr><tr><td class="line2"> .createInstance(); </td></tr></tbody></table></div><textarea style="display: none;" name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">new PrimitiveComponentType() |
| .setBundleContext(context) |
| .setClassName(MyComponentImpl.class.getName()) |
| .addDependency(new Dependency().setField("myFoo")) |
| .setValidateMethod("start") |
| .createInstance();</textarea> |
| <script class="javascript"> |
| if(!window.newcodemacro_initialised) |
| { |
| window.newcodemacro_initialised = true; |
| window.oldonloadmethod = window.onload; |
| window.onload = function(){ |
| dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| if(window.oldonloadmethod) |
| { |
| window.oldonloadmethod(); |
| } |
| } |
| } |
| |
| </script> |
| </div> |
| |
| |
| <h2><a name="apache-felix-ipojo-api-PrimitiveComponentTypebasics"></a>Primitive Component Type basics</h2> |
| |
| <p>When you create a new Primitive component type (so, using a Java |
| class as implementation), you must set the bundle context and the class |
| name. Everything else is optional. </p> |
| |
| <p>Note about inner classes: iPOJO is based on a bytecode manipulation. |
| The API embeds the manipulator. So, when you initialize the component |
| type, the specified class name is manipulated (if not already |
| manipulated). So, as this force using a customized classloader, inner |
| classes cannot be manipulated. So, inner class injection is not |
| supported with the API.</p> |
| |
| <h2><a name="apache-felix-ipojo-api-ImmediateComponent,ValidateandInvalidateMethod"></a>Immediate Component, Validate and Invalidate Method</h2> |
| <p>To set the component type as immediate, just call the <tt>setImmediate(immediate)</tt> method on the primitive component type object. <br> |
| To set validate and invalidate methods, just call the <tt>setValidate(method)</tt> and <tt>setInvalidate(method)</tt>. Specify the method name that you want to call in argument. </p> |
| |
| <h2><a name="apache-felix-ipojo-api-Declaringservices"></a>Declaring services</h2> |
| <p>To declare that a component provides a service, add a new service to your primitive component type.<br> |
| The Service object can be configured. By default, it exposed every implemented interface (regular iPOJO behavior). So, you can:</p> |
| <ul> |
| <li>Add service property</li> |
| <li>Set the creation strategy</li> |
| <li>Set the exposed service specifications</li> |
| </ul> |
| |
| |
| <div class="code"> |
| <div class="dp-highlighter"><table class="dp-c" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="line1"><span>new PrimitiveComponentType() </span></td></tr><tr><td class="line2"> .setBundleContext(context) </td></tr><tr><td class="line1"> .setClassName(org.example.service.impl.MyComponentImpl.<span class="keyword">class</span><span>.getName()) </span></td></tr><tr><td class="line2"> .addService(new Service() </td></tr><tr><td class="line1"> .addProperty(new ServiceProperty() </td></tr><tr><td class="line2"> .setField(<span class="string">"myServiceProperty"</span><span>) </span></td></tr><tr><td class="line1"> .setName(<span class="string">"sample.myProperty"</span><span>)) </span></td></tr><tr><td class="line2"> .setCreationStrategy(Service.INSTANCE_STRATEGY)) </td></tr><tr><td class="line1"> .createInstance(); </td></tr></tbody></table></div><textarea style="display: none;" name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">new PrimitiveComponentType() |
| .setBundleContext(context) |
| .setClassName(org.example.service.impl.MyComponentImpl.class.getName()) |
| .addService(new Service() |
| .addProperty(new ServiceProperty() |
| .setField("myServiceProperty") |
| .setName("sample.myProperty")) |
| .setCreationStrategy(Service.INSTANCE_STRATEGY)) |
| .createInstance();</textarea> |
| <script class="javascript"> |
| if(!window.newcodemacro_initialised) |
| { |
| window.newcodemacro_initialised = true; |
| window.oldonloadmethod = window.onload; |
| window.onload = function(){ |
| dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| if(window.oldonloadmethod) |
| { |
| window.oldonloadmethod(); |
| } |
| } |
| } |
| |
| </script> |
| </div> |
| |
| |
| <h2><a name="apache-felix-ipojo-api-ServiceDependencies"></a>Service Dependencies</h2> |
| <p>To declare a service dependency, create and add a Dependency object. |
| The dependency object offers all the iPOJO service dependency features. |
| You can set the injected field and/or bind/unbind methods. Here is an |
| example:</p> |
| <div class="code"> |
| <div class="dp-highlighter"><table class="dp-c" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="line1"><span>new PrimitiveComponentType() </span></td></tr><tr><td class="line2"> .setBundleContext(context) </td></tr><tr><td class="line1"> .setClassName(org.example.service.impl.MyComponentImpl.<span class="keyword">class</span><span>.getName()) </span></td></tr><tr><td class="line2"> .addDependency( </td></tr><tr><td class="line1"> new Dependency().setField(<span class="string">"myFoo"</span><span>) </span></td></tr><tr><td class="line2"> .setOptional(<span class="keyword">true</span><span>) </span></td></tr><tr><td class="line1"> .setDefaultImplementation(<span class="string">"org.sample.FooDefaultImplementation"</span><span>) </span></td></tr><tr><td class="line2"> ) </td></tr><tr><td class="line1"> .createInstance(); </td></tr></tbody></table></div><textarea style="display: none;" name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">new PrimitiveComponentType() |
| .setBundleContext(context) |
| .setClassName(org.example.service.impl.MyComponentImpl.class.getName()) |
| .addDependency( |
| new Dependency().setField("myFoo") |
| .setOptional(true) |
| .setDefaultImplementation("org.sample.FooDefaultImplementation") |
| ) |
| .createInstance();</textarea> |
| <script class="javascript"> |
| if(!window.newcodemacro_initialised) |
| { |
| window.newcodemacro_initialised = true; |
| window.oldonloadmethod = window.onload; |
| window.onload = function(){ |
| dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| if(window.oldonloadmethod) |
| { |
| window.oldonloadmethod(); |
| } |
| } |
| } |
| |
| </script> |
| </div> |
| |
| |
| <h2><a name="apache-felix-ipojo-api-Properties"></a>Properties</h2> |
| <p>Thanks to the <tt>addProperty</tt> method, you can create component properties. Here is some example of properties:</p> |
| <div class="code"> |
| <div class="dp-highlighter"><table class="dp-c" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="line1"><span>new PrimitiveComponentType() </span></td></tr><tr><td class="line2"> .setBundleContext(context) </td></tr><tr><td class="line1"> .setClassName(MyComponentImpl.<span class="keyword">class</span><span>.getName()) </span></td></tr><tr><td class="line2"> .addProperty(new Property() </td></tr><tr><td class="line1"> .setField(<span class="string">"myProperty"</span><span>) </span></td></tr><tr><td class="line2"> .setValue(<span class="string">"default-value"</span><span>) </span></td></tr><tr><td class="line1"> ) </td></tr><tr><td class="line2"> .addProperty(new Property() </td></tr><tr><td class="line1"> .setMethod(<span class="string">"setMethod"</span><span>) </span></td></tr><tr><td class="line2"> .setName(<span class="string">"prop"</span><span>) </span></td></tr><tr><td class="line1"> ) </td></tr><tr><td class="line2"> .createInstance(); </td></tr></tbody></table></div><textarea style="display: none;" name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">new PrimitiveComponentType() |
| .setBundleContext(context) |
| .setClassName(MyComponentImpl.class.getName()) |
| .addProperty(new Property() |
| .setField("myProperty") |
| .setValue("default-value") |
| ) |
| .addProperty(new Property() |
| .setMethod("setMethod") |
| .setName("prop") |
| ) |
| .createInstance();</textarea> |
| <script class="javascript"> |
| if(!window.newcodemacro_initialised) |
| { |
| window.newcodemacro_initialised = true; |
| window.oldonloadmethod = window.onload; |
| window.onload = function(){ |
| dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| if(window.oldonloadmethod) |
| { |
| window.oldonloadmethod(); |
| } |
| } |
| } |
| |
| </script> |
| </div> |
| |
| |
| <h2><a name="apache-felix-ipojo-api-TemporalDependencies"></a>Temporal Dependencies</h2> |
| <p>Temporal dependencies are also supported: </p> |
| <div class="code"> |
| <div class="dp-highlighter"><table class="dp-c" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="line1"><span>new PrimitiveComponentType() </span></td></tr><tr><td class="line2"> .setBundleContext(context) </td></tr><tr><td class="line1"> .setClassName(MyComponentImpl.<span class="keyword">class</span><span>.getName()) </span></td></tr><tr><td class="line2"> .addDependency( </td></tr><tr><td class="line1"> new TemporalDependency().setField(<span class="string">"myFoo"</span><span>) </span></td></tr><tr><td class="line2"> .setOnTimeoutPolicy(TemporalDependency.NULLABLE) </td></tr><tr><td class="line1"> .setTimeout(3000) </td></tr><tr><td class="line2"> .setProxy(<span class="keyword">true</span><span>) </span></td></tr><tr><td class="line1"> ) </td></tr><tr><td class="line2"> .createInstance(); </td></tr></tbody></table></div><textarea style="display: none;" name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">new PrimitiveComponentType() |
| .setBundleContext(context) |
| .setClassName(MyComponentImpl.class.getName()) |
| .addDependency( |
| new TemporalDependency().setField("myFoo") |
| .setOnTimeoutPolicy(TemporalDependency.NULLABLE) |
| .setTimeout(3000) |
| .setProxy(true) |
| ) |
| .createInstance();</textarea> |
| <script class="javascript"> |
| if(!window.newcodemacro_initialised) |
| { |
| window.newcodemacro_initialised = true; |
| window.oldonloadmethod = window.onload; |
| window.onload = function(){ |
| dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| if(window.oldonloadmethod) |
| { |
| window.oldonloadmethod(); |
| } |
| } |
| } |
| |
| </script> |
| </div> |
| |
| |
| <h2><a name="apache-felix-ipojo-api-Instancecreation"></a>Instance creation</h2> |
| <p>The API allows you to create instances from the component type you described. Three differents methods.<br> |
| The <tt>createInstance()</tt> method just creates an instance. The <tt>createInstance(String name)</tt> method allows to set the instance name. Finally, the <tt>createInstance(Dictionary configuration)</tt> |
| allows setting the instance configuration. All those methods returns |
| the ComponentInstance object allowing to manage the instance (stop, |
| start, dispose). </p> |
| |
| <h2><a name="apache-felix-ipojo-api-ManagedServiceandPropagation"></a>Managed Service and Propagation</h2> |
| <p>You can enable/disable the property propagation thanks to the <tt>setPropagation</tt> method on the PrimitiveComponentType object. <br> |
| You can also set the the managed service PID with the <tt>setManagedServicePID</tt> |
| method. This method should be only use to give a default value of for |
| singleton component. In all other case, the managed service pid has to |
| be provided inside the instance configuration.</p> |
| |
| <h2><a name="apache-felix-ipojo-api-ManagingiPOJOFactory"></a>Managing iPOJO Factory</h2> |
| <p>Beyond the PrimitiveComponentType, an iPOJO factory is hidden. You can configure this factory to be public or private with the <tt>setPublic</tt> method. You can also set the name of the factory with the <tt>setName</tt> method. </p> |
| |
| <p>Then, you can access to the Factory object by calling the <tt>getFactory</tt> method.</p> |
| |
| <h2><a name="apache-felix-ipojo-api-AccesstotheintrospectionAPI"></a>Access to the introspection API</h2> |
| <p>The API provides bridge to get access to the iPOJO introspection |
| API. The introspection API allows reconfiguring at runtime an instance |
| (properties, service dependencies...). From Service and Dependency, |
| Property and ServiceProperty objects, call the <tt>getXXXDescription</tt> |
| method. You must give the instance that you want to introspect in |
| argument. If the lookup success, you get an object allowing |
| reconfiguring the service, dependency or property. </p> |
| |
| <h2><a name="apache-felix-ipojo-api-SingletonComponentType"></a>Singleton Component Type</h2> |
| <p>If you are sure to create only one instance of your component type, |
| you can use the singleton component type class. This is a kind of |
| primitive component type, but when you start it (with the <tt>create</tt> method), it will automatically create an instance.</p> |
| <div class="code"> |
| <div class="dp-highlighter"><table class="dp-c" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="line1"><span>PrimitiveComponentType type = new SingletonComponentType() </span></td></tr><tr><td class="line2"> .setBundleContext(context) </td></tr><tr><td class="line1"> .setClassName(org.example.service.impl.MyComponentImpl.<span class="keyword">class</span><span>.getName()) </span></td></tr><tr><td class="line2"> .addDependency(new Dependency().setField(<span class="string">"myFoo"</span><span>)) </span></td></tr><tr><td class="line1"> .setValidateMethod(<span class="string">"start"</span><span>); </span></td></tr><tr><td class="line2"> </td></tr><tr><td class="line1"> ((SingletonComponentType) type) </td></tr><tr><td class="line2"> .setObject(new MyComponentImpl(5)) <span class="comment">// Inject a pojo object</span><span> </span></td></tr><tr><td class="line1"> .create();<span class="comment">// Create an instance</span><span> </span></td></tr></tbody></table></div><textarea style="display: none;" name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">PrimitiveComponentType type = new SingletonComponentType() |
| .setBundleContext(context) |
| .setClassName(org.example.service.impl.MyComponentImpl.class.getName()) |
| .addDependency(new Dependency().setField("myFoo")) |
| .setValidateMethod("start"); |
| |
| ((SingletonComponentType) type) |
| .setObject(new MyComponentImpl(5)) // Inject a pojo object |
| .create();// Create an instance</textarea> |
| <script class="javascript"> |
| if(!window.newcodemacro_initialised) |
| { |
| window.newcodemacro_initialised = true; |
| window.oldonloadmethod = window.onload; |
| window.onload = function(){ |
| dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| if(window.oldonloadmethod) |
| { |
| window.oldonloadmethod(); |
| } |
| } |
| } |
| |
| </script> |
| </div> |
| |
| |
| <p>The type created with the singleton component type are set to <tt>private</tt> by default. Instead of calling the <tt>start</tt> method, you have to call one of the <tt>create</tt> methods to start the type and create the instance.</p> |
| |
| <p>You can also set the contained POJO object by using the <tt>setObject</tt> method. The given object MUST be compatible with the component implementation class.</p> |
| |
| <h2><a name="apache-felix-ipojo-api-Usingexternalhandlers"></a>Using external handlers</h2> |
| <p>iPOJO is extensible... So, it makes sense that the API is also |
| extensible. So component type provides a method allowing to add |
| external handler configuration:</p> |
| <div class="code"> |
| <div class="dp-highlighter"><table class="dp-c" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="line1"><span></span><span class="keyword">return</span><span> new PrimitiveComponentType() </span></td></tr><tr><td class="line2"> .setBundleContext(context) </td></tr><tr><td class="line1"> .setClassName(HostImpl.<span class="keyword">class</span><span>.getName()) </span></td></tr><tr><td class="line2"> .addHandler(new Whiteboard() </td></tr><tr><td class="line1"> .onArrival(<span class="string">"arrival"</span><span>) </span></td></tr><tr><td class="line2"> .onDeparture(<span class="string">"departure"</span><span>) </span></td></tr><tr><td class="line1"> .setFilter(<span class="string">"(foo=foo)"</span><span>) </span></td></tr><tr><td class="line2"> ); </td></tr></tbody></table></div><textarea style="display: none;" name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">return new PrimitiveComponentType() |
| .setBundleContext(context) |
| .setClassName(HostImpl.class.getName()) |
| .addHandler(new Whiteboard() |
| .onArrival("arrival") |
| .onDeparture("departure") |
| .setFilter("(foo=foo)") |
| );</textarea> |
| <script class="javascript"> |
| if(!window.newcodemacro_initialised) |
| { |
| window.newcodemacro_initialised = true; |
| window.oldonloadmethod = window.onload; |
| window.onload = function(){ |
| dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| if(window.oldonloadmethod) |
| { |
| window.oldonloadmethod(); |
| } |
| } |
| } |
| |
| </script> |
| </div> |
| |
| |
| <p>The <tt>addHandler</tt> method allows you to add any handler description. A handler description is an object of a class implementing <tt>org.apache.felix.ipojo.api.HandlerConfiguration</tt>. Handler provider willing to support the API have to provide this class. For example, the example above uses <tt>Whiteboard</tt> that is the Whiteboard pattern handler description. This class is very simple, and is shown below:</p> |
| <div class="code"> |
| <div class="dp-highlighter"><table class="dp-c" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="line1"><span></span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> Whiteboard </span><span class="keyword">implements</span><span> HandlerConfiguration { </span></td></tr><tr><td class="line2"> </td></tr><tr><td class="line1"> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">final</span><span> String NAME = </span><span class="string">"wbp"</span><span>; </span></td></tr><tr><td class="line2"> </td></tr><tr><td class="line1"> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">final</span><span> String NAMESPACE = </span><span class="string">"org.apache.felix.ipojo.whiteboard"</span><span>; </span></td></tr><tr><td class="line2"> </td></tr><tr><td class="line1"> <span class="keyword">private</span><span> String arrival; </span></td></tr><tr><td class="line2"> </td></tr><tr><td class="line1"> <span class="keyword">private</span><span> String departure; </span></td></tr><tr><td class="line2"> </td></tr><tr><td class="line1"> <span class="keyword">private</span><span> String modification; </span></td></tr><tr><td class="line2"> </td></tr><tr><td class="line1"> <span class="keyword">private</span><span> String filter; </span></td></tr><tr><td class="line2"> </td></tr><tr><td class="line1"> <span class="keyword">public</span><span> Whiteboard onArrival(String method) { </span></td></tr><tr><td class="line2"> arrival = method; </td></tr><tr><td class="line1"> <span class="keyword">return</span><span> </span><span class="keyword">this</span><span>; </span></td></tr><tr><td class="line2"> } </td></tr><tr><td class="line1"> </td></tr><tr><td class="line2"> <span class="keyword">public</span><span> Whiteboard onDeparture(String method) { </span></td></tr><tr><td class="line1"> departure = method; </td></tr><tr><td class="line2"> <span class="keyword">return</span><span> </span><span class="keyword">this</span><span>; </span></td></tr><tr><td class="line1"> } </td></tr><tr><td class="line2"> </td></tr><tr><td class="line1"> <span class="keyword">public</span><span> Whiteboard onModification(String method) { </span></td></tr><tr><td class="line2"> modification = method; </td></tr><tr><td class="line1"> <span class="keyword">return</span><span> </span><span class="keyword">this</span><span>; </span></td></tr><tr><td class="line2"> } </td></tr><tr><td class="line1"> </td></tr><tr><td class="line2"> <span class="keyword">public</span><span> Whiteboard setFilter(String fil) { </span></td></tr><tr><td class="line1"> filter = fil; </td></tr><tr><td class="line2"> <span class="keyword">return</span><span> </span><span class="keyword">this</span><span>; </span></td></tr><tr><td class="line1"> } </td></tr><tr><td class="line2"> </td></tr><tr><td class="line1"> <span class="keyword">public</span><span> Element getElement() { </span></td></tr><tr><td class="line2"> ensureValidity(); </td></tr><tr><td class="line1"> <span class="comment">// Create the root element.</span><span> </span></td></tr><tr><td class="line2"> Element element = new Element(NAME, NAMESPACE); </td></tr><tr><td class="line1"> <span class="comment">// Mandatory attributes</span><span> </span></td></tr><tr><td class="line2"> element.addAttribute(new Attribute(<span class="string">"onArrival"</span><span>, arrival)); </span></td></tr><tr><td class="line1"> element.addAttribute(new Attribute(<span class="string">"onDeparture"</span><span>, departure)); </span></td></tr><tr><td class="line2"> element.addAttribute(new Attribute(<span class="string">"filter"</span><span>, filter)); </span></td></tr><tr><td class="line1"> </td></tr><tr><td class="line2"> <span class="comment">// Optional attribute</span><span> </span></td></tr><tr><td class="line1"> <span class="keyword">if</span><span> (modification != </span><span class="keyword">null</span><span>) { </span></td></tr><tr><td class="line2"> element.addAttribute(new Attribute(<span class="string">"onModification"</span><span>, modification)); </span></td></tr><tr><td class="line1"> } </td></tr><tr><td class="line2"> </td></tr><tr><td class="line1"> <span class="keyword">return</span><span> element; </span></td></tr><tr><td class="line2"> } </td></tr><tr><td class="line1"> </td></tr><tr><td class="line2"> <span class="keyword">private</span><span> </span><span class="keyword">void</span><span> ensureValidity() { </span></td></tr><tr><td class="line1"> <span class="keyword">if</span><span> (arrival == </span><span class="keyword">null</span><span>) { </span></td></tr><tr><td class="line2"> <span class="keyword">throw</span><span> new IllegalStateException(</span><span class="string">"The whiteboard pattern configuration must have a onArrival method"</span><span>); </span></td></tr><tr><td class="line1"> } </td></tr><tr><td class="line2"> <span class="keyword">if</span><span> (departure == </span><span class="keyword">null</span><span>) { </span></td></tr><tr><td class="line1"> <span class="keyword">throw</span><span> new IllegalStateException(</span><span class="string">"The whiteboard pattern configuration must have a onDeparture method"</span><span>); </span></td></tr><tr><td class="line2"> } </td></tr><tr><td class="line1"> <span class="keyword">if</span><span> (filter == </span><span class="keyword">null</span><span>) { </span></td></tr><tr><td class="line2"> <span class="keyword">throw</span><span> new IllegalStateException(</span><span class="string">"The whiteboard pattern configuration must have a filter"</span><span>); </span></td></tr><tr><td class="line1"> } </td></tr><tr><td class="line2"> </td></tr><tr><td class="line1"> } </td></tr></tbody></table></div><textarea style="display: none;" name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">public class Whiteboard implements HandlerConfiguration { |
| |
| public static final String NAME = "wbp"; |
| |
| public static final String NAMESPACE = "org.apache.felix.ipojo.whiteboard"; |
| |
| private String arrival; |
| |
| private String departure; |
| |
| private String modification; |
| |
| private String filter; |
| |
| public Whiteboard onArrival(String method) { |
| arrival = method; |
| return this; |
| } |
| |
| public Whiteboard onDeparture(String method) { |
| departure = method; |
| return this; |
| } |
| |
| public Whiteboard onModification(String method) { |
| modification = method; |
| return this; |
| } |
| |
| public Whiteboard setFilter(String fil) { |
| filter = fil; |
| return this; |
| } |
| |
| public Element getElement() { |
| ensureValidity(); |
| // Create the root element. |
| Element element = new Element(NAME, NAMESPACE); |
| // Mandatory attributes |
| element.addAttribute(new Attribute("onArrival", arrival)); |
| element.addAttribute(new Attribute("onDeparture", departure)); |
| element.addAttribute(new Attribute("filter", filter)); |
| |
| // Optional attribute |
| if (modification != null) { |
| element.addAttribute(new Attribute("onModification", modification)); |
| } |
| |
| return element; |
| } |
| |
| private void ensureValidity() { |
| if (arrival == null) { |
| throw new IllegalStateException("The whiteboard pattern configuration must have a onArrival method"); |
| } |
| if (departure == null) { |
| throw new IllegalStateException("The whiteboard pattern configuration must have a onDeparture method"); |
| } |
| if (filter == null) { |
| throw new IllegalStateException("The whiteboard pattern configuration must have a filter"); |
| } |
| |
| }</textarea> |
| <script class="javascript"> |
| if(!window.newcodemacro_initialised) |
| { |
| window.newcodemacro_initialised = true; |
| window.oldonloadmethod = window.onload; |
| window.onload = function(){ |
| dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| if(window.oldonloadmethod) |
| { |
| window.oldonloadmethod(); |
| } |
| } |
| } |
| |
| </script> |
| </div> |
| |
| |
| <p>The only required method is <tt>getElement</tt> returning the |
| Element-Attribute structure representing the handler configuration |
| (this uses the internal iPOJO data format). If the metadata cannot be |
| generated, the class throws IllegalStateExceptions.</p> |
| |
| <h2><a name="apache-felix-ipojo-api-CreatingcompositionwiththeAPI"></a>Creating composition with the API</h2> |
| <p>The API also allows you to create iPOJO compositions in a pretty simple way. So you can create compositions:</p> |
| <ul> |
| <li>containing internal instances</li> |
| <li>importing services</li> |
| <li>instantiating sub-services</li> |
| <li>exporting services</li> |
| <li>providing services (by delegation)</li> |
| </ul> |
| |
| |
| <p>Here are some examples:</p> |
| <div class="code"> |
| <div class="dp-highlighter"><table class="dp-c" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="line1"><span>PrimitiveComponentType prov = createAProvider(); </span><span class="comment">// Create a primitive type</span><span> </span></td></tr><tr><td class="line2"> PrimitiveComponentType cons = createAConsumer(); <span class="comment">// Create another primitive type</span><span> </span></td></tr><tr><td class="line1"> </td></tr><tr><td class="line2"> CompositeComponentType type = new CompositeComponentType() </td></tr><tr><td class="line1"> .setBundleContext(context) </td></tr><tr><td class="line2"> .setComponentTypeName(<span class="string">"comp1"</span><span>) </span></td></tr><tr><td class="line1"> .addInstance(new Instance(prov.getFactory().getName())) <span class="comment">// Create an instance in the composite</span><span> </span></td></tr><tr><td class="line2"> .addInstance(new Instance(cons.getFactory().getName())); </td></tr><tr><td class="line1"> </td></tr><tr><td class="line2"> ComponentInstance ci = type.createInstance(); </td></tr></tbody></table></div><textarea style="display: none;" name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">PrimitiveComponentType prov = createAProvider(); // Create a primitive type |
| PrimitiveComponentType cons = createAConsumer(); // Create another primitive type |
| |
| CompositeComponentType type = new CompositeComponentType() |
| .setBundleContext(context) |
| .setComponentTypeName("comp1") |
| .addInstance(new Instance(prov.getFactory().getName())) // Create an instance in the composite |
| .addInstance(new Instance(cons.getFactory().getName())); |
| |
| ComponentInstance ci = type.createInstance();</textarea> |
| <script class="javascript"> |
| if(!window.newcodemacro_initialised) |
| { |
| window.newcodemacro_initialised = true; |
| window.oldonloadmethod = window.onload; |
| window.onload = function(){ |
| dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| if(window.oldonloadmethod) |
| { |
| window.oldonloadmethod(); |
| } |
| } |
| } |
| |
| </script> |
| </div> |
| |
| <div class="code"> |
| <div class="dp-highlighter"><table class="dp-c" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="line1"><span>CompositeComponentType type = new CompositeComponentType() </span></td></tr><tr><td class="line2"> .setBundleContext(context) </td></tr><tr><td class="line1"> .setComponentTypeName(<span class="string">"comp3"</span><span>) </span></td></tr><tr><td class="line2"> .addSubService(new InstantiatedService() <span class="comment">// Importation</span><span> </span></td></tr><tr><td class="line1"> .setSpecification(Foo.<span class="keyword">class</span><span>.getName()) </span></td></tr><tr><td class="line2"> .setOptional(<span class="keyword">true</span><span>)); </span></td></tr></tbody></table></div><textarea style="display: none;" name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">CompositeComponentType type = new CompositeComponentType() |
| .setBundleContext(context) |
| .setComponentTypeName("comp3") |
| .addSubService(new InstantiatedService() // Importation |
| .setSpecification(Foo.class.getName()) |
| .setOptional(true));</textarea> |
| <script class="javascript"> |
| if(!window.newcodemacro_initialised) |
| { |
| window.newcodemacro_initialised = true; |
| window.oldonloadmethod = window.onload; |
| window.onload = function(){ |
| dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| if(window.oldonloadmethod) |
| { |
| window.oldonloadmethod(); |
| } |
| } |
| } |
| |
| </script> |
| </div> |
| |
| <div class="code"> |
| <div class="dp-highlighter"><table class="dp-c" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="line1"><span>CompositeComponentType type = new CompositeComponentType() </span></td></tr><tr><td class="line2"> .setBundleContext(context) </td></tr><tr><td class="line1"> .setComponentTypeName(<span class="string">"comp2"</span><span>) </span></td></tr><tr><td class="line2"> .addSubService(new InstantiatedService() <span class="comment">// Instantiated service</span><span> </span></td></tr><tr><td class="line1"> .setSpecification(Foo.<span class="keyword">class</span><span>.getName())) </span></td></tr><tr><td class="line2"> .addInstance(new Instance(cons.getFactory().getName())); </td></tr></tbody></table></div><textarea style="display: none;" name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">CompositeComponentType type = new CompositeComponentType() |
| .setBundleContext(context) |
| .setComponentTypeName("comp2") |
| .addSubService(new InstantiatedService() // Instantiated service |
| .setSpecification(Foo.class.getName())) |
| .addInstance(new Instance(cons.getFactory().getName()));</textarea> |
| <script class="javascript"> |
| if(!window.newcodemacro_initialised) |
| { |
| window.newcodemacro_initialised = true; |
| window.oldonloadmethod = window.onload; |
| window.onload = function(){ |
| dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| if(window.oldonloadmethod) |
| { |
| window.oldonloadmethod(); |
| } |
| } |
| } |
| |
| </script> |
| </div> |
| |
| <div class="code"> |
| <div class="dp-highlighter"><table class="dp-c" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="line1"><span>CompositeComponentType type = new CompositeComponentType() </span></td></tr><tr><td class="line2"> .setBundleContext(context) </td></tr><tr><td class="line1"> .setComponentTypeName(<span class="string">"compExport"</span><span>) </span></td></tr><tr><td class="line2"> .addSubService(new InstantiatedService().setSpecification(Foo.<span class="keyword">class</span><span>.getName())) </span></td></tr><tr><td class="line1"> .addService(new ExportedService() </td></tr><tr><td class="line2"> .setSpecification(Foo.<span class="keyword">class</span><span>.getName())); </span><span class="comment">// Exports a service</span><span> </span></td></tr></tbody></table></div><textarea style="display: none;" name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">CompositeComponentType type = new CompositeComponentType() |
| .setBundleContext(context) |
| .setComponentTypeName("compExport") |
| .addSubService(new InstantiatedService().setSpecification(Foo.class.getName())) |
| .addService(new ExportedService() |
| .setSpecification(Foo.class.getName())); // Exports a service</textarea> |
| <script class="javascript"> |
| if(!window.newcodemacro_initialised) |
| { |
| window.newcodemacro_initialised = true; |
| window.oldonloadmethod = window.onload; |
| window.onload = function(){ |
| dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| if(window.oldonloadmethod) |
| { |
| window.oldonloadmethod(); |
| } |
| } |
| } |
| |
| </script> |
| </div> |
| |
| <p><br clear="all"></p> |
| |
| </div> |
| <img src="apache-felix-ipojo-api_files/footer.png" class="footer"> |
| </div> |
| |
| <script type="text/javascript"> |
| var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); |
| document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); |
| </script><script src="apache-felix-ipojo-api_files/ga.js" type="text/javascript"></script> |
| <script type="text/javascript"> |
| try{ |
| var pageTracker = _gat._getTracker("UA-1518442-4"); |
| pageTracker._trackPageview(); |
| } catch(err) {} |
| </script> |
| |
| </div> |
| </body><div FirebugVersion="1.4.0" style="display: none;" id="_firebugConsole"></div></html> |