| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| <!-- NewPage --> |
| <html lang="en"> |
| <head> |
| <!-- Generated by javadoc (1.8.0_172) on Mon Oct 22 18:37:56 CEST 2018 --> |
| <title>Init</title> |
| <meta name="date" content="2018-10-22"> |
| <link rel="stylesheet" type="text/css" href="../../../../../../stylesheet.css" title="Style"> |
| <script type="text/javascript" src="../../../../../../script.js"></script> |
| </head> |
| <body> |
| <script type="text/javascript"><!-- |
| try { |
| if (location.href.indexOf('is-external=true') == -1) { |
| parent.document.title="Init"; |
| } |
| } |
| catch(err) { |
| } |
| //--> |
| </script> |
| <noscript> |
| <div>JavaScript is disabled on your browser.</div> |
| </noscript> |
| <!-- ========= START OF TOP NAVBAR ======= --> |
| <div class="topNav"><a name="navbar.top"> |
| <!-- --> |
| </a> |
| <div class="skipNav"><a href="#skip.navbar.top" title="Skip navigation links">Skip navigation links</a></div> |
| <a name="navbar.top.firstrow"> |
| <!-- --> |
| </a> |
| <ul class="navList" title="Navigation"> |
| <li><a href="../../../../../../org/apache/felix/dm/annotation/api/package-summary.html">Package</a></li> |
| <li class="navBarCell1Rev">Class</li> |
| <li><a href="package-tree.html">Tree</a></li> |
| <li><a href="../../../../../../deprecated-list.html">Deprecated</a></li> |
| <li><a href="../../../../../../index-all.html">Index</a></li> |
| <li><a href="../../../../../../help-doc.html">Help</a></li> |
| </ul> |
| </div> |
| <div class="subNav"> |
| <ul class="navList"> |
| <li><a href="../../../../../../org/apache/felix/dm/annotation/api/Destroy.html" title="annotation in org.apache.felix.dm.annotation.api"><span class="typeNameLink">Prev Class</span></a></li> |
| <li><a href="../../../../../../org/apache/felix/dm/annotation/api/Inject.html" title="annotation in org.apache.felix.dm.annotation.api"><span class="typeNameLink">Next Class</span></a></li> |
| </ul> |
| <ul class="navList"> |
| <li><a href="../../../../../../index.html?org/apache/felix/dm/annotation/api/Init.html" target="_top">Frames</a></li> |
| <li><a href="Init.html" target="_top">No Frames</a></li> |
| </ul> |
| <ul class="navList" id="allclasses_navbar_top"> |
| <li><a href="../../../../../../allclasses-noframe.html">All Classes</a></li> |
| </ul> |
| <div> |
| <script type="text/javascript"><!-- |
| allClassesLink = document.getElementById("allclasses_navbar_top"); |
| if(window==top) { |
| allClassesLink.style.display = "block"; |
| } |
| else { |
| allClassesLink.style.display = "none"; |
| } |
| //--> |
| </script> |
| </div> |
| <div> |
| <ul class="subNavList"> |
| <li>Summary: </li> |
| <li>Field | </li> |
| <li>Required | </li> |
| <li>Optional</li> |
| </ul> |
| <ul class="subNavList"> |
| <li>Detail: </li> |
| <li>Field | </li> |
| <li>Element</li> |
| </ul> |
| </div> |
| <a name="skip.navbar.top"> |
| <!-- --> |
| </a></div> |
| <!-- ========= END OF TOP NAVBAR ========= --> |
| <!-- ======== START OF CLASS DATA ======== --> |
| <div class="header"> |
| <div class="subTitle">org.apache.felix.dm.annotation.api</div> |
| <h2 title="Annotation Type Init" class="title">Annotation Type Init</h2> |
| </div> |
| <div class="contentContainer"> |
| <div class="description"> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <hr> |
| <br> |
| <pre>@Retention(value=CLASS) |
| @Target(value=METHOD) |
| public @interface <span class="memberNameLabel">Init</span></pre> |
| <div class="block">Annotates a method used to configure dynamic dependencies. |
| When this method is invoked, all required dependencies (except the ones declared with a <code>name</code> |
| attribute) are already injected, and optional dependencies on class fields |
| are also already injected (possibly with NullObjects). |
| |
| The purpose of the @Init method is to either declare more dynamic dependencies using the DM API, or to |
| return a Map used to dynamically configure dependencies that are annotated using a <code>name</code> attribute. |
| |
| After the init method returns, the added or configured dependencies are then tracked, and when all |
| dependencies are injected, then the start method (annotated with @Start) is invoked. |
| |
| The method annotated with @Init may have the following signatures, |
| <ol> |
| <li>void init(Component component)</li> |
| <li>void init()</li> |
| <li>Map<String, String> init(Component component)</li> |
| <li>Map<String, String> init()</li> |
| </ol> |
| |
| When the init method defines a Component argument, it can then be used by the method to add more dependencie using the Dependency Manager API. |
| |
| <h3>Usage Examples</h3> |
| In this sample, the "PersistenceImpl" component dynamically configures the "storage" dependency from the "init" method. |
| The dependency "required" flag and filter string are derived from an xml configuration that is already injected before the init |
| method. |
| |
| <blockquote> |
| <pre> |
| @Component |
| public class PersistenceImpl implements Persistence { |
| // Injected before init. |
| @ConfigurationDependency |
| void updated(Dictionary conf) { |
| if (conf != null) { |
| _xmlConfiguration = parseXmlConfiguration(conf.get("xmlConfiguration")); |
| } |
| } |
| |
| // Parsed xml configuration, where we'll get our storage service filter and required dependency flag. |
| XmlConfiguration _xmlConfiguration; |
| |
| // Dynamically configure the dependency declared with a "storage" name. |
| @Init |
| Map<String, String> init() { |
| Map<String, String> props = new HashMap<>(); |
| props.put("storage.required", Boolean.toString(_xmlConfiguration.isStorageRequired())) |
| props.put("storage.filter", "(type=" + _xmlConfiguration.getStorageType() + ")"); |
| return props; |
| } |
| |
| // Injected after init (dependency filter is defined dynamically from our init method). |
| @ServiceDependency(name="storage") |
| Storage storage; |
| |
| // All dependencies injected, including dynamic dependencies defined from init method. |
| @Start |
| void start() { |
| log.log(LogService.LOG_WARNING, "start"); |
| } |
| |
| @Override |
| void store(String key, String value) { |
| storage.store(key, value); |
| } |
| } |
| </pre> |
| </blockquote> |
| |
| Same example as above, but this time the dependency is added from the init method using the Dependency Manager API: |
| |
| <blockquote> |
| <pre> |
| @Component |
| public class PersistenceImpl implements Persistence { |
| // Injected before init. |
| @ConfigurationDependency |
| void updated(Dictionary conf) { |
| if (conf != null) { |
| _xmlConfiguration = parseXmlConfiguration(conf.get("xmlConfiguration")); |
| } |
| } |
| |
| // Parsed xml configuration, where we'll get our storage service filter and required dependency flag. |
| XmlConfiguration _xmlConfiguration; |
| |
| // Dynamically configure the dependency declared with a "storage" name. |
| @Init |
| void init(org.apache.felix.dm.Comppnent myComponent) { |
| boolean required = _xmlConfiguration.isStorageRequired(); |
| String filter = _xmlConfiguration.getStorageType(); |
| DependencyManager dm = myComponent.getDependencyManager(); |
| myComponent.add(dm.createServiceDependency().setService(Storage.class, filter).setRequired(required)); |
| } |
| |
| // Injected after init, later, when the dependency added from the init() method is satisfied |
| volatile Storage storage; |
| |
| // All dependencies injected, including dynamic dependencies defined from init method. |
| @Start |
| void start() { |
| log.log(LogService.LOG_WARNING, "start"); |
| } |
| |
| @Override |
| void store(String key, String value) { |
| storage.store(key, value); |
| } |
| } |
| </pre> |
| </blockquote> |
| |
| Same example as above, but this time the dependency is added from the init method using the Dependency Manager Lambda: |
| |
| <blockquote> |
| <pre> |
| import static org.apache.felix.dm.lambda.DependencyManagerActivator.component; |
| |
| @Component |
| public class PersistenceImpl implements Persistence { |
| // Injected before init. |
| @ConfigurationDependency |
| void updated(Dictionary conf) { |
| if (conf != null) { |
| _xmlConfiguration = parseXmlConfiguration(conf.get("xmlConfiguration")); |
| } |
| } |
| |
| // Parsed xml configuration, where we'll get our storage service filter and required dependency flag. |
| XmlConfiguration _xmlConfiguration; |
| |
| // Dynamically configure the dependency declared with a "storage" name. |
| @Init |
| void init(org.apache.felix.dm.Comppnent myComponent) { |
| boolean required = _xmlConfiguration.isStorageRequired(); |
| String filter = _xmlConfiguration.getStorageType(); |
| component(myComponent, comp -> comp.withSvc(Storage.class, filter, required)); |
| } |
| |
| // Injected after init, later, when the dependency added from the init() method is satisfied |
| volatile Storage storage; |
| |
| // All dependencies injected, including dynamic dependencies defined from init method. |
| @Start |
| void start() { |
| log.log(LogService.LOG_WARNING, "start"); |
| } |
| |
| @Override |
| void store(String key, String value) { |
| storage.store(key, value); |
| } |
| } |
| </pre> |
| </blockquote></div> |
| </li> |
| </ul> |
| </div> |
| </div> |
| <!-- ========= END OF CLASS DATA ========= --> |
| <!-- ======= START OF BOTTOM NAVBAR ====== --> |
| <div class="bottomNav"><a name="navbar.bottom"> |
| <!-- --> |
| </a> |
| <div class="skipNav"><a href="#skip.navbar.bottom" title="Skip navigation links">Skip navigation links</a></div> |
| <a name="navbar.bottom.firstrow"> |
| <!-- --> |
| </a> |
| <ul class="navList" title="Navigation"> |
| <li><a href="../../../../../../org/apache/felix/dm/annotation/api/package-summary.html">Package</a></li> |
| <li class="navBarCell1Rev">Class</li> |
| <li><a href="package-tree.html">Tree</a></li> |
| <li><a href="../../../../../../deprecated-list.html">Deprecated</a></li> |
| <li><a href="../../../../../../index-all.html">Index</a></li> |
| <li><a href="../../../../../../help-doc.html">Help</a></li> |
| </ul> |
| </div> |
| <div class="subNav"> |
| <ul class="navList"> |
| <li><a href="../../../../../../org/apache/felix/dm/annotation/api/Destroy.html" title="annotation in org.apache.felix.dm.annotation.api"><span class="typeNameLink">Prev Class</span></a></li> |
| <li><a href="../../../../../../org/apache/felix/dm/annotation/api/Inject.html" title="annotation in org.apache.felix.dm.annotation.api"><span class="typeNameLink">Next Class</span></a></li> |
| </ul> |
| <ul class="navList"> |
| <li><a href="../../../../../../index.html?org/apache/felix/dm/annotation/api/Init.html" target="_top">Frames</a></li> |
| <li><a href="Init.html" target="_top">No Frames</a></li> |
| </ul> |
| <ul class="navList" id="allclasses_navbar_bottom"> |
| <li><a href="../../../../../../allclasses-noframe.html">All Classes</a></li> |
| </ul> |
| <div> |
| <script type="text/javascript"><!-- |
| allClassesLink = document.getElementById("allclasses_navbar_bottom"); |
| if(window==top) { |
| allClassesLink.style.display = "block"; |
| } |
| else { |
| allClassesLink.style.display = "none"; |
| } |
| //--> |
| </script> |
| </div> |
| <div> |
| <ul class="subNavList"> |
| <li>Summary: </li> |
| <li>Field | </li> |
| <li>Required | </li> |
| <li>Optional</li> |
| </ul> |
| <ul class="subNavList"> |
| <li>Detail: </li> |
| <li>Field | </li> |
| <li>Element</li> |
| </ul> |
| </div> |
| <a name="skip.navbar.bottom"> |
| <!-- --> |
| </a></div> |
| <!-- ======== END OF BOTTOM NAVBAR ======= --> |
| </body> |
| </html> |