Clement Escoffier | 373a57d | 2009-07-21 12:06:46 +0000 | [diff] [blame] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 2 | <html><head> |
| 3 | |
| 4 | |
| 5 | |
| 6 | <title>Apache Felix - apache-felix-ipojo-api</title> |
| 7 | <link rel="stylesheet" href="apache-felix-ipojo-api_files/site.css" type="text/css" media="all"> |
| 8 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| 9 | </head><body> |
| 10 | <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> |
| 11 | <div class="menu"> |
| 12 | <ul> |
| 13 | <li><a href="http://felix.apache.org/site/news.html" title="news">news</a></li> |
| 14 | <li><a href="http://felix.apache.org/site/license.html" title="license">license</a></li> |
| 15 | <li><a href="http://felix.apache.org/site/downloads.cgi" rel="nofollow">downloads</a></li> |
| 16 | <li><a href="http://felix.apache.org/site/documentation.html" title="documentation">documentation</a></li> |
| 17 | <li><a href="http://felix.apache.org/site/mailinglists.html" title="mailinglists">mailing lists</a></li> |
| 18 | <li><a href="http://felix.apache.org/site/contributing.html" title="Contributing">contributing</a></li> |
| 19 | <li><a href="http://www.apache.org/" rel="nofollow">asf</a></li> |
| 20 | <li><a href="http://www.apache.org/foundation/sponsorship.html" rel="nofollow">sponsorship</a></li> |
| 21 | <li><a href="http://www.apache.org/foundation/thanks.html" rel="nofollow">sponsors</a> |
| 22 | <!-- ApacheCon Ad --> |
| 23 | <iframe src="apache-felix-ipojo-api_files/button.html" style="border-width: 0pt; float: left;" scrolling="no" frameborder="0" height="135" width="135"></iframe> |
| 24 | <p style="height: 100px;"> |
| 25 | <!-- ApacheCon Ad --> |
| 26 | </p></li></ul> </div> |
| 27 | <div class="main"> |
| 28 | <style type="text/css"> |
| 29 | @import url(http://people.apache.org/~clement/ipojo/site/superfish.css); |
| 30 | </style> |
| 31 | |
| 32 | <style type="text/css"> |
| 33 | @import url(http://people.apache.org/~clement/ipojo/site/style.css); |
| 34 | </style> |
| 35 | |
| 36 | <p> |
| 37 | <script class="javascript" src="apache-felix-ipojo-api_files/shCore.js"></script> |
| 38 | <script class="javascript" src="apache-felix-ipojo-api_files/shBrushCSharp.js"></script> |
| 39 | <script class="javascript" src="apache-felix-ipojo-api_files/shBrushPhp.js"></script> |
| 40 | <script class="javascript" src="apache-felix-ipojo-api_files/shBrushJScript.js"></script> |
| 41 | <script class="javascript" src="apache-felix-ipojo-api_files/shBrushVb.js"></script> |
| 42 | <script class="javascript" src="apache-felix-ipojo-api_files/shBrushSql.js"></script> |
| 43 | <script class="javascript" src="apache-felix-ipojo-api_files/shBrushXml.js"></script> |
| 44 | <script class="javascript" src="apache-felix-ipojo-api_files/shBrushShell.js"></script> |
| 45 | <script class="javascript" src="apache-felix-ipojo-api_files/shBrushDelphi.js"></script> |
| 46 | <script class="javascript" src="apache-felix-ipojo-api_files/shBrushPython.js"></script> |
| 47 | <script class="javascript" src="apache-felix-ipojo-api_files/shBrushJava.js"></script> |
| 48 | |
| 49 | <script type="text/javascript" src="apache-felix-ipojo-api_files/jquery-1.js"></script> |
| 50 | <script type="text/javascript" src="apache-felix-ipojo-api_files/hoverIntent.js"></script> |
| 51 | <script type="text/javascript" src="apache-felix-ipojo-api_files/superfish.js"></script> |
| 52 | <script type="text/javascript" src="apache-felix-ipojo-api_files/supersubs.js"></script> |
| 53 | |
| 54 | <script type="text/javascript"> |
| 55 | |
| 56 | $(document).ready(function(){ |
| 57 | $("ul.sf-menu").supersubs({ |
| 58 | minWidth: 14, // minimum width of sub-menus in em units |
| 59 | maxWidth: 30, // maximum width of sub-menus in em units |
| 60 | extraWidth: 1 // extra width can ensure lines don't sometimes turn over |
| 61 | // due to slight rounding differences and font-family |
| 62 | }).superfish(); // call supersubs first, then superfish, so that subs are |
| 63 | // not display:none when measuring. Call before initialising |
| 64 | // containing tabs for same reason. |
| 65 | }); |
| 66 | |
| 67 | </script> |
| 68 | </p><div class="main"> |
| 69 | <div class="page-header"> |
| 70 | <img src="apache-felix-ipojo-api_files/header.png" class="header"> |
| 71 | <a href="http://ipojo.org/"><img src="apache-felix-ipojo-api_files/ipojo.png" class="header-logo" width="225"></a> |
| 72 | <ul class="sf-menu sf-js-enabled sf-shadow" id="ipojo-menu"> |
| 73 | <li class="current"> |
| 74 | <!-- Menu Overview --> |
| 75 | <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> |
| 76 | <ul style="float: none; width: 14em; display: none; visibility: hidden;"> |
| 77 | <li style="white-space: normal; float: left; width: 100%;"> |
| 78 | <a style="float: none; width: auto;" href="http://felix.apache.org/site/apache-felix-ipojo.html" title="Apache Felix iPOJO">Home</a> |
| 79 | </li> |
| 80 | <li style="white-space: normal; float: left; width: 100%;"> |
| 81 | <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> |
| 82 | </li> |
| 83 | <li style="white-space: normal; float: left; width: 100%;"> |
| 84 | <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> |
| 85 | </li> |
| 86 | <li style="white-space: normal; float: left; width: 100%;"> |
| 87 | <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> |
| 88 | </li> |
| 89 | </ul> |
| 90 | </li> |
| 91 | |
| 92 | <li class=""> |
| 93 | <!-- Menu download --> |
| 94 | </li><li> |
| 95 | <a href="http://felix.apache.org/site/download.html" title="Download">Download </a> |
| 96 | </li> |
| 97 | |
| 98 | <li class=""> |
| 99 | <!-- Menu Documentation --> |
| 100 | <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> |
| 101 | <ul style="float: none; width: 14em; display: none; visibility: hidden;"> |
| 102 | <!-- sub- menu : getting started --> |
| 103 | <li style="white-space: normal; float: left; width: 100%;" class=""> |
| 104 | <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> |
| 105 | <ul style="left: 14em; float: none; width: 14em; display: none; visibility: hidden;"> |
| 106 | <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> |
| 107 | <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> |
| 108 | <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> |
| 109 | <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> |
| 110 | <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> |
| 111 | <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> |
| 112 | </ul> |
| 113 | </li> <!-- end of getting started --> |
| 114 | <!-- sub menu : Describing Components --> |
| 115 | <li style="white-space: normal; float: left; width: 100%;" class=""> |
| 116 | <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> |
| 117 | <ul style="left: 14em; float: none; width: 14em; display: none; visibility: hidden;"> |
| 118 | <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> |
| 119 | <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> |
| 120 | <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> |
| 121 | <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> |
| 122 | <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> |
| 123 | <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> |
| 124 | <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> |
| 125 | <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> |
| 126 | <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> |
| 127 | <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> |
| 128 | <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> |
| 129 | </ul> |
| 130 | </li> <!-- End of describing components --> |
| 131 | <!-- sub- menu : User Guide --> |
| 132 | <li style="white-space: normal; float: left; width: 100%;" class=""> |
| 133 | <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> |
| 134 | <ul style="left: 14em; float: none; width: 14em; display: none; visibility: hidden;"> |
| 135 | <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> |
| 136 | <li style="white-space: normal; float: left; width: 100%;"><a style="float: none; width: auto;" href="" title="apache-felix-ipojo-api">API</a></li> |
| 137 | <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> |
| 138 | <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> |
| 139 | <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> |
| 140 | <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> |
| 141 | </ul> |
| 142 | </li> <!-- end of user guide --> |
| 143 | <!-- sub- menu : Dev Guide --> |
| 144 | <li class="" style="white-space: normal; float: left; width: 100%;"> |
| 145 | <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> |
| 146 | <ul style="left: 14em; float: none; width: 14em; display: none; visibility: hidden;"> |
| 147 | <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> |
| 148 | <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> |
| 149 | <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> |
| 150 | <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> |
| 151 | </ul> |
| 152 | </li> <!-- End of Dev guide --> |
| 153 | </ul> |
| 154 | </li> <!-- End of doc --> |
| 155 | <!-- Menu 4 : Tools --> |
| 156 | <li class=""> |
| 157 | <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> |
| 158 | <ul style="float: none; width: 14em; display: none; visibility: hidden;"> |
| 159 | <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> |
| 160 | <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> |
| 161 | <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> |
| 162 | <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> |
| 163 | <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> |
| 164 | <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> |
| 165 | </ul> |
| 166 | </li><!-- End of tools --> |
| 167 | <!-- Menu 5 : Misc --> |
| 168 | <li class=""> |
| 169 | <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> |
| 170 | <ul style="float: none; width: 14em; display: none; visibility: hidden;"> |
| 171 | <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> |
| 172 | <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> |
| 173 | <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> |
| 174 | <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> |
| 175 | <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> |
| 176 | </ul> |
| 177 | </li><!-- End of misc --> |
| 178 | </ul> <!-- End of the menu --> |
| 179 | </div> <!-- Page header --> |
| 180 | |
| 181 | |
| 182 | <div class="content"> |
| 183 | |
| 184 | <h1><a name="apache-felix-ipojo-api-iPOJOAPI"></a>iPOJO API</h1> |
| 185 | |
| 186 | <p><em>The iPOJO API provides a third way to describe iPOJO components. |
| 187 | With the API, you can dynamically create new components types and |
| 188 | create instances from them. Your component types are described with an |
| 189 | API. To use the API, deploy and start the iPOJO-API bundle and the |
| 190 | iPOJO core bundle.</em></p> |
| 191 | |
| 192 | <div class="toc"> |
| 193 | <ul> |
| 194 | <li><a href="#apache-felix-ipojo-api-Asimpleexample">A simple example</a></li> |
| 195 | <li><a href="#apache-felix-ipojo-api-PrimitiveComponentTypebasics">Primitive Component Type basics</a></li> |
| 196 | <li><a href="#apache-felix-ipojo-api-ImmediateComponent%252CValidateandInvalidateMethod">Immediate Component, Validate and Invalidate Method</a></li> |
| 197 | <li><a href="#apache-felix-ipojo-api-Declaringservices">Declaring services</a></li> |
| 198 | <li><a href="#apache-felix-ipojo-api-ServiceDependencies">Service Dependencies</a></li> |
| 199 | <li><a href="#apache-felix-ipojo-api-Properties">Properties</a></li> |
| 200 | <li><a href="#apache-felix-ipojo-api-TemporalDependencies">Temporal Dependencies</a></li> |
| 201 | <li><a href="#apache-felix-ipojo-api-Instancecreation">Instance creation</a></li> |
| 202 | <li><a href="#apache-felix-ipojo-api-ManagedServiceandPropagation">Managed Service and Propagation</a></li> |
| 203 | <li><a href="#apache-felix-ipojo-api-ManagingiPOJOFactory">Managing iPOJO Factory</a></li> |
| 204 | <li><a href="#apache-felix-ipojo-api-AccesstotheintrospectionAPI">Access to the introspection API</a></li> |
| 205 | <li><a href="#apache-felix-ipojo-api-SingletonComponentType">Singleton Component Type</a></li> |
| 206 | <li><a href="#apache-felix-ipojo-api-Usingexternalhandlers">Using external handlers</a></li> |
| 207 | <li><a href="#apache-felix-ipojo-api-CreatingcompositionwiththeAPI">Creating composition with the API</a></li> |
| 208 | </ul></div> |
| 209 | |
| 210 | |
| 211 | <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> |
| 212 | |
| 213 | <h2><a name="apache-felix-ipojo-api-Asimpleexample"></a>A simple example</h2> |
| 214 | |
| 215 | <p>Let's imagine a simple component providing a service Foo. The following code is the implementation class of our component:</p> |
| 216 | <div class="code"> |
| 217 | <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; |
| 218 | |
| 219 | import org.example.service.Foo; |
| 220 | |
| 221 | public class FooImpl implements Foo { |
| 222 | |
| 223 | public void doSomething() { |
| 224 | // Do something... |
| 225 | } |
| 226 | |
| 227 | }</textarea> |
| 228 | <script class="javascript"> |
| 229 | if(!window.newcodemacro_initialised) |
| 230 | { |
| 231 | window.newcodemacro_initialised = true; |
| 232 | window.oldonloadmethod = window.onload; |
| 233 | window.onload = function(){ |
| 234 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 235 | if(window.oldonloadmethod) |
| 236 | { |
| 237 | window.oldonloadmethod(); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | </script> |
| 243 | </div> |
| 244 | |
| 245 | |
| 246 | <p>To create the component type and an instance of the component type |
| 247 | just create a class, get the bundle context (either from a |
| 248 | Bundle-Activator, or from an iPOJO component), and write the following |
| 249 | code:</p> |
| 250 | <div class="code"> |
| 251 | <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() |
| 252 | .setBundleContext(context) |
| 253 | .setClassName(FooImpl.class.getName()) |
| 254 | .addService(new Service()) // Provide the Foo service |
| 255 | .createInstance(); // Create the instance</textarea> |
| 256 | <script class="javascript"> |
| 257 | if(!window.newcodemacro_initialised) |
| 258 | { |
| 259 | window.newcodemacro_initialised = true; |
| 260 | window.oldonloadmethod = window.onload; |
| 261 | window.onload = function(){ |
| 262 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 263 | if(window.oldonloadmethod) |
| 264 | { |
| 265 | window.oldonloadmethod(); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | </script> |
| 271 | </div> |
| 272 | |
| 273 | |
| 274 | <p>So, now let's imagine another component using this service. The |
| 275 | following code is the implementation class of this component type:</p> |
| 276 | <div class="code"> |
| 277 | <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; |
| 278 | |
| 279 | import org.example.service.Foo; |
| 280 | |
| 281 | public class MyComponentImpl { |
| 282 | private Foo myFoo; |
| 283 | |
| 284 | public void start() { |
| 285 | myFoo.doSomething(); |
| 286 | } |
| 287 | |
| 288 | }</textarea> |
| 289 | <script class="javascript"> |
| 290 | if(!window.newcodemacro_initialised) |
| 291 | { |
| 292 | window.newcodemacro_initialised = true; |
| 293 | window.oldonloadmethod = window.onload; |
| 294 | window.onload = function(){ |
| 295 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 296 | if(window.oldonloadmethod) |
| 297 | { |
| 298 | window.oldonloadmethod(); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | </script> |
| 304 | </div> |
| 305 | |
| 306 | |
| 307 | <p>It is a regular iPOJO component expecting to get a Foo service in the <tt>myFoo</tt> |
| 308 | field. It also has a method executed when the instance is valid using |
| 309 | the Foo service. To describe this component, just write the following |
| 310 | lines:</p> |
| 311 | <div class="code"> |
| 312 | <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() |
| 313 | .setBundleContext(context) |
| 314 | .setClassName(MyComponentImpl.class.getName()) |
| 315 | .addDependency(new Dependency().setField("myFoo")) |
| 316 | .setValidateMethod("start") |
| 317 | .createInstance();</textarea> |
| 318 | <script class="javascript"> |
| 319 | if(!window.newcodemacro_initialised) |
| 320 | { |
| 321 | window.newcodemacro_initialised = true; |
| 322 | window.oldonloadmethod = window.onload; |
| 323 | window.onload = function(){ |
| 324 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 325 | if(window.oldonloadmethod) |
| 326 | { |
| 327 | window.oldonloadmethod(); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | </script> |
| 333 | </div> |
| 334 | |
| 335 | |
| 336 | <h2><a name="apache-felix-ipojo-api-PrimitiveComponentTypebasics"></a>Primitive Component Type basics</h2> |
| 337 | |
| 338 | <p>When you create a new Primitive component type (so, using a Java |
| 339 | class as implementation), you must set the bundle context and the class |
| 340 | name. Everything else is optional. </p> |
| 341 | |
| 342 | <p>Note about inner classes: iPOJO is based on a bytecode manipulation. |
| 343 | The API embeds the manipulator. So, when you initialize the component |
| 344 | type, the specified class name is manipulated (if not already |
| 345 | manipulated). So, as this force using a customized classloader, inner |
| 346 | classes cannot be manipulated. So, inner class injection is not |
| 347 | supported with the API.</p> |
| 348 | |
| 349 | <h2><a name="apache-felix-ipojo-api-ImmediateComponent,ValidateandInvalidateMethod"></a>Immediate Component, Validate and Invalidate Method</h2> |
| 350 | <p>To set the component type as immediate, just call the <tt>setImmediate(immediate)</tt> method on the primitive component type object. <br> |
| 351 | 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> |
| 352 | |
| 353 | <h2><a name="apache-felix-ipojo-api-Declaringservices"></a>Declaring services</h2> |
| 354 | <p>To declare that a component provides a service, add a new service to your primitive component type.<br> |
| 355 | The Service object can be configured. By default, it exposed every implemented interface (regular iPOJO behavior). So, you can:</p> |
| 356 | <ul> |
| 357 | <li>Add service property</li> |
| 358 | <li>Set the creation strategy</li> |
| 359 | <li>Set the exposed service specifications</li> |
| 360 | </ul> |
| 361 | |
| 362 | |
| 363 | <div class="code"> |
| 364 | <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() |
| 365 | .setBundleContext(context) |
| 366 | .setClassName(org.example.service.impl.MyComponentImpl.class.getName()) |
| 367 | .addService(new Service() |
| 368 | .addProperty(new ServiceProperty() |
| 369 | .setField("myServiceProperty") |
| 370 | .setName("sample.myProperty")) |
| 371 | .setCreationStrategy(Service.INSTANCE_STRATEGY)) |
| 372 | .createInstance();</textarea> |
| 373 | <script class="javascript"> |
| 374 | if(!window.newcodemacro_initialised) |
| 375 | { |
| 376 | window.newcodemacro_initialised = true; |
| 377 | window.oldonloadmethod = window.onload; |
| 378 | window.onload = function(){ |
| 379 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 380 | if(window.oldonloadmethod) |
| 381 | { |
| 382 | window.oldonloadmethod(); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | </script> |
| 388 | </div> |
| 389 | |
| 390 | |
| 391 | <h2><a name="apache-felix-ipojo-api-ServiceDependencies"></a>Service Dependencies</h2> |
| 392 | <p>To declare a service dependency, create and add a Dependency object. |
| 393 | The dependency object offers all the iPOJO service dependency features. |
| 394 | You can set the injected field and/or bind/unbind methods. Here is an |
| 395 | example:</p> |
| 396 | <div class="code"> |
| 397 | <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() |
| 398 | .setBundleContext(context) |
| 399 | .setClassName(org.example.service.impl.MyComponentImpl.class.getName()) |
| 400 | .addDependency( |
| 401 | new Dependency().setField("myFoo") |
| 402 | .setOptional(true) |
| 403 | .setDefaultImplementation("org.sample.FooDefaultImplementation") |
| 404 | ) |
| 405 | .createInstance();</textarea> |
| 406 | <script class="javascript"> |
| 407 | if(!window.newcodemacro_initialised) |
| 408 | { |
| 409 | window.newcodemacro_initialised = true; |
| 410 | window.oldonloadmethod = window.onload; |
| 411 | window.onload = function(){ |
| 412 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 413 | if(window.oldonloadmethod) |
| 414 | { |
| 415 | window.oldonloadmethod(); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | </script> |
| 421 | </div> |
| 422 | |
| 423 | |
| 424 | <h2><a name="apache-felix-ipojo-api-Properties"></a>Properties</h2> |
| 425 | <p>Thanks to the <tt>addProperty</tt> method, you can create component properties. Here is some example of properties:</p> |
| 426 | <div class="code"> |
| 427 | <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() |
| 428 | .setBundleContext(context) |
| 429 | .setClassName(MyComponentImpl.class.getName()) |
| 430 | .addProperty(new Property() |
| 431 | .setField("myProperty") |
| 432 | .setValue("default-value") |
| 433 | ) |
| 434 | .addProperty(new Property() |
| 435 | .setMethod("setMethod") |
| 436 | .setName("prop") |
| 437 | ) |
| 438 | .createInstance();</textarea> |
| 439 | <script class="javascript"> |
| 440 | if(!window.newcodemacro_initialised) |
| 441 | { |
| 442 | window.newcodemacro_initialised = true; |
| 443 | window.oldonloadmethod = window.onload; |
| 444 | window.onload = function(){ |
| 445 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 446 | if(window.oldonloadmethod) |
| 447 | { |
| 448 | window.oldonloadmethod(); |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | </script> |
| 454 | </div> |
| 455 | |
| 456 | |
| 457 | <h2><a name="apache-felix-ipojo-api-TemporalDependencies"></a>Temporal Dependencies</h2> |
| 458 | <p>Temporal dependencies are also supported: </p> |
| 459 | <div class="code"> |
| 460 | <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() |
| 461 | .setBundleContext(context) |
| 462 | .setClassName(MyComponentImpl.class.getName()) |
| 463 | .addDependency( |
| 464 | new TemporalDependency().setField("myFoo") |
| 465 | .setOnTimeoutPolicy(TemporalDependency.NULLABLE) |
| 466 | .setTimeout(3000) |
| 467 | .setProxy(true) |
| 468 | ) |
| 469 | .createInstance();</textarea> |
| 470 | <script class="javascript"> |
| 471 | if(!window.newcodemacro_initialised) |
| 472 | { |
| 473 | window.newcodemacro_initialised = true; |
| 474 | window.oldonloadmethod = window.onload; |
| 475 | window.onload = function(){ |
| 476 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 477 | if(window.oldonloadmethod) |
| 478 | { |
| 479 | window.oldonloadmethod(); |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | </script> |
| 485 | </div> |
| 486 | |
| 487 | |
| 488 | <h2><a name="apache-felix-ipojo-api-Instancecreation"></a>Instance creation</h2> |
| 489 | <p>The API allows you to create instances from the component type you described. Three differents methods.<br> |
| 490 | 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> |
| 491 | allows setting the instance configuration. All those methods returns |
| 492 | the ComponentInstance object allowing to manage the instance (stop, |
| 493 | start, dispose). </p> |
| 494 | |
| 495 | <h2><a name="apache-felix-ipojo-api-ManagedServiceandPropagation"></a>Managed Service and Propagation</h2> |
| 496 | <p>You can enable/disable the property propagation thanks to the <tt>setPropagation</tt> method on the PrimitiveComponentType object. <br> |
| 497 | You can also set the the managed service PID with the <tt>setManagedServicePID</tt> |
| 498 | method. This method should be only use to give a default value of for |
| 499 | singleton component. In all other case, the managed service pid has to |
| 500 | be provided inside the instance configuration.</p> |
| 501 | |
| 502 | <h2><a name="apache-felix-ipojo-api-ManagingiPOJOFactory"></a>Managing iPOJO Factory</h2> |
| 503 | <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> |
| 504 | |
| 505 | <p>Then, you can access to the Factory object by calling the <tt>getFactory</tt> method.</p> |
| 506 | |
| 507 | <h2><a name="apache-felix-ipojo-api-AccesstotheintrospectionAPI"></a>Access to the introspection API</h2> |
| 508 | <p>The API provides bridge to get access to the iPOJO introspection |
| 509 | API. The introspection API allows reconfiguring at runtime an instance |
| 510 | (properties, service dependencies...). From Service and Dependency, |
| 511 | Property and ServiceProperty objects, call the <tt>getXXXDescription</tt> |
| 512 | method. You must give the instance that you want to introspect in |
| 513 | argument. If the lookup success, you get an object allowing |
| 514 | reconfiguring the service, dependency or property. </p> |
| 515 | |
| 516 | <h2><a name="apache-felix-ipojo-api-SingletonComponentType"></a>Singleton Component Type</h2> |
| 517 | <p>If you are sure to create only one instance of your component type, |
| 518 | you can use the singleton component type class. This is a kind of |
| 519 | primitive component type, but when you start it (with the <tt>create</tt> method), it will automatically create an instance.</p> |
| 520 | <div class="code"> |
| 521 | <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() |
| 522 | .setBundleContext(context) |
| 523 | .setClassName(org.example.service.impl.MyComponentImpl.class.getName()) |
| 524 | .addDependency(new Dependency().setField("myFoo")) |
| 525 | .setValidateMethod("start"); |
| 526 | |
| 527 | ((SingletonComponentType) type) |
| 528 | .setObject(new MyComponentImpl(5)) // Inject a pojo object |
| 529 | .create();// Create an instance</textarea> |
| 530 | <script class="javascript"> |
| 531 | if(!window.newcodemacro_initialised) |
| 532 | { |
| 533 | window.newcodemacro_initialised = true; |
| 534 | window.oldonloadmethod = window.onload; |
| 535 | window.onload = function(){ |
| 536 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 537 | if(window.oldonloadmethod) |
| 538 | { |
| 539 | window.oldonloadmethod(); |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | </script> |
| 545 | </div> |
| 546 | |
| 547 | |
| 548 | <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> |
| 549 | |
| 550 | <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> |
| 551 | |
| 552 | <h2><a name="apache-felix-ipojo-api-Usingexternalhandlers"></a>Using external handlers</h2> |
| 553 | <p>iPOJO is extensible... So, it makes sense that the API is also |
| 554 | extensible. So component type provides a method allowing to add |
| 555 | external handler configuration:</p> |
| 556 | <div class="code"> |
| 557 | <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() |
| 558 | .setBundleContext(context) |
| 559 | .setClassName(HostImpl.class.getName()) |
| 560 | .addHandler(new Whiteboard() |
| 561 | .onArrival("arrival") |
| 562 | .onDeparture("departure") |
| 563 | .setFilter("(foo=foo)") |
| 564 | );</textarea> |
| 565 | <script class="javascript"> |
| 566 | if(!window.newcodemacro_initialised) |
| 567 | { |
| 568 | window.newcodemacro_initialised = true; |
| 569 | window.oldonloadmethod = window.onload; |
| 570 | window.onload = function(){ |
| 571 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 572 | if(window.oldonloadmethod) |
| 573 | { |
| 574 | window.oldonloadmethod(); |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | </script> |
| 580 | </div> |
| 581 | |
| 582 | |
| 583 | <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> |
| 584 | <div class="code"> |
| 585 | <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 { |
| 586 | |
| 587 | public static final String NAME = "wbp"; |
| 588 | |
| 589 | public static final String NAMESPACE = "org.apache.felix.ipojo.whiteboard"; |
| 590 | |
| 591 | private String arrival; |
| 592 | |
| 593 | private String departure; |
| 594 | |
| 595 | private String modification; |
| 596 | |
| 597 | private String filter; |
| 598 | |
| 599 | public Whiteboard onArrival(String method) { |
| 600 | arrival = method; |
| 601 | return this; |
| 602 | } |
| 603 | |
| 604 | public Whiteboard onDeparture(String method) { |
| 605 | departure = method; |
| 606 | return this; |
| 607 | } |
| 608 | |
| 609 | public Whiteboard onModification(String method) { |
| 610 | modification = method; |
| 611 | return this; |
| 612 | } |
| 613 | |
| 614 | public Whiteboard setFilter(String fil) { |
| 615 | filter = fil; |
| 616 | return this; |
| 617 | } |
| 618 | |
| 619 | public Element getElement() { |
| 620 | ensureValidity(); |
| 621 | // Create the root element. |
| 622 | Element element = new Element(NAME, NAMESPACE); |
| 623 | // Mandatory attributes |
| 624 | element.addAttribute(new Attribute("onArrival", arrival)); |
| 625 | element.addAttribute(new Attribute("onDeparture", departure)); |
| 626 | element.addAttribute(new Attribute("filter", filter)); |
| 627 | |
| 628 | // Optional attribute |
| 629 | if (modification != null) { |
| 630 | element.addAttribute(new Attribute("onModification", modification)); |
| 631 | } |
| 632 | |
| 633 | return element; |
| 634 | } |
| 635 | |
| 636 | private void ensureValidity() { |
| 637 | if (arrival == null) { |
| 638 | throw new IllegalStateException("The whiteboard pattern configuration must have a onArrival method"); |
| 639 | } |
| 640 | if (departure == null) { |
| 641 | throw new IllegalStateException("The whiteboard pattern configuration must have a onDeparture method"); |
| 642 | } |
| 643 | if (filter == null) { |
| 644 | throw new IllegalStateException("The whiteboard pattern configuration must have a filter"); |
| 645 | } |
| 646 | |
| 647 | }</textarea> |
| 648 | <script class="javascript"> |
| 649 | if(!window.newcodemacro_initialised) |
| 650 | { |
| 651 | window.newcodemacro_initialised = true; |
| 652 | window.oldonloadmethod = window.onload; |
| 653 | window.onload = function(){ |
| 654 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 655 | if(window.oldonloadmethod) |
| 656 | { |
| 657 | window.oldonloadmethod(); |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | </script> |
| 663 | </div> |
| 664 | |
| 665 | |
| 666 | <p>The only required method is <tt>getElement</tt> returning the |
| 667 | Element-Attribute structure representing the handler configuration |
| 668 | (this uses the internal iPOJO data format). If the metadata cannot be |
| 669 | generated, the class throws IllegalStateExceptions.</p> |
| 670 | |
| 671 | <h2><a name="apache-felix-ipojo-api-CreatingcompositionwiththeAPI"></a>Creating composition with the API</h2> |
| 672 | <p>The API also allows you to create iPOJO compositions in a pretty simple way. So you can create compositions:</p> |
| 673 | <ul> |
| 674 | <li>containing internal instances</li> |
| 675 | <li>importing services</li> |
| 676 | <li>instantiating sub-services</li> |
| 677 | <li>exporting services</li> |
| 678 | <li>providing services (by delegation)</li> |
| 679 | </ul> |
| 680 | |
| 681 | |
| 682 | <p>Here are some examples:</p> |
| 683 | <div class="code"> |
| 684 | <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 |
| 685 | PrimitiveComponentType cons = createAConsumer(); // Create another primitive type |
| 686 | |
| 687 | CompositeComponentType type = new CompositeComponentType() |
| 688 | .setBundleContext(context) |
| 689 | .setComponentTypeName("comp1") |
| 690 | .addInstance(new Instance(prov.getFactory().getName())) // Create an instance in the composite |
| 691 | .addInstance(new Instance(cons.getFactory().getName())); |
| 692 | |
| 693 | ComponentInstance ci = type.createInstance();</textarea> |
| 694 | <script class="javascript"> |
| 695 | if(!window.newcodemacro_initialised) |
| 696 | { |
| 697 | window.newcodemacro_initialised = true; |
| 698 | window.oldonloadmethod = window.onload; |
| 699 | window.onload = function(){ |
| 700 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 701 | if(window.oldonloadmethod) |
| 702 | { |
| 703 | window.oldonloadmethod(); |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | </script> |
| 709 | </div> |
| 710 | |
| 711 | <div class="code"> |
| 712 | <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() |
| 713 | .setBundleContext(context) |
| 714 | .setComponentTypeName("comp3") |
| 715 | .addSubService(new InstantiatedService() // Importation |
| 716 | .setSpecification(Foo.class.getName()) |
| 717 | .setOptional(true));</textarea> |
| 718 | <script class="javascript"> |
| 719 | if(!window.newcodemacro_initialised) |
| 720 | { |
| 721 | window.newcodemacro_initialised = true; |
| 722 | window.oldonloadmethod = window.onload; |
| 723 | window.onload = function(){ |
| 724 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 725 | if(window.oldonloadmethod) |
| 726 | { |
| 727 | window.oldonloadmethod(); |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | </script> |
| 733 | </div> |
| 734 | |
| 735 | <div class="code"> |
| 736 | <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() |
| 737 | .setBundleContext(context) |
| 738 | .setComponentTypeName("comp2") |
| 739 | .addSubService(new InstantiatedService() // Instantiated service |
| 740 | .setSpecification(Foo.class.getName())) |
| 741 | .addInstance(new Instance(cons.getFactory().getName()));</textarea> |
| 742 | <script class="javascript"> |
| 743 | if(!window.newcodemacro_initialised) |
| 744 | { |
| 745 | window.newcodemacro_initialised = true; |
| 746 | window.oldonloadmethod = window.onload; |
| 747 | window.onload = function(){ |
| 748 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 749 | if(window.oldonloadmethod) |
| 750 | { |
| 751 | window.oldonloadmethod(); |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | </script> |
| 757 | </div> |
| 758 | |
| 759 | <div class="code"> |
| 760 | <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() |
| 761 | .setBundleContext(context) |
| 762 | .setComponentTypeName("compExport") |
| 763 | .addSubService(new InstantiatedService().setSpecification(Foo.class.getName())) |
| 764 | .addService(new ExportedService() |
| 765 | .setSpecification(Foo.class.getName())); // Exports a service</textarea> |
| 766 | <script class="javascript"> |
| 767 | if(!window.newcodemacro_initialised) |
| 768 | { |
| 769 | window.newcodemacro_initialised = true; |
| 770 | window.oldonloadmethod = window.onload; |
| 771 | window.onload = function(){ |
| 772 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 773 | if(window.oldonloadmethod) |
| 774 | { |
| 775 | window.oldonloadmethod(); |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | </script> |
| 781 | </div> |
| 782 | |
| 783 | <p><br clear="all"></p> |
| 784 | |
| 785 | </div> |
| 786 | <img src="apache-felix-ipojo-api_files/footer.png" class="footer"> |
| 787 | </div> |
| 788 | |
| 789 | <script type="text/javascript"> |
| 790 | var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); |
| 791 | document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); |
| 792 | </script><script src="apache-felix-ipojo-api_files/ga.js" type="text/javascript"></script> |
| 793 | <script type="text/javascript"> |
| 794 | try{ |
| 795 | var pageTracker = _gat._getTracker("UA-1518442-4"); |
| 796 | pageTracker._trackPageview(); |
| 797 | } catch(err) {} |
| 798 | </script> |
| 799 | |
| 800 | </div> |
| 801 | </body><div FirebugVersion="1.4.0" style="display: none;" id="_firebugConsole"></div></html> |