felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From build...@apache.org
Subject svn commit: r943598 - in /websites/staging/felix/trunk/content: ./ documentation/subprojects/apache-felix-dependency-manager/reference/components.html
Date Thu, 12 Mar 2015 23:42:55 GMT
Author: buildbot
Date: Thu Mar 12 23:42:55 2015
New Revision: 943598

Log:
Staging update by buildbot for felix

Modified:
    websites/staging/felix/trunk/content/   (props changed)
    websites/staging/felix/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/components.html

Propchange: websites/staging/felix/trunk/content/
------------------------------------------------------------------------------
--- cms:source-revision (original)
+++ cms:source-revision Thu Mar 12 23:42:55 2015
@@ -1 +1 @@
-1666333
+1666335

Modified: websites/staging/felix/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/components.html
==============================================================================
--- websites/staging/felix/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/components.html
(original)
+++ websites/staging/felix/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/components.html
Thu Mar 12 23:42:55 2015
@@ -213,40 +213,102 @@ that is internally implemented using thr
     <span class="kd">private</span> <span class="kd">final</span>
<span class="n">ProviderParticipant2</span> <span class="n">m_participant2</span>
<span class="o">=</span> <span class="k">new</span> <span class="n">ProviderParticipant2</span><span
class="o">();</span>
     <span class="kd">private</span> <span class="kd">volatile</span>
<span class="n">LogService</span> <span class="n">m_log</span><span
class="o">;</span> <span class="c1">// injected</span>
 
-    <span class="nd">@Composition</span>
-    <span class="n">Object</span><span class="o">[]</span> <span
class="nf">getComposition</span><span class="o">()</span> <span class="o">{</span>
-        <span class="k">return</span> <span class="k">new</span>
<span class="n">Object</span><span class="o">[]</span> <span class="o">{</span>
<span class="k">this</span><span class="o">,</span> <span class="n">m_participant1</span><span
class="o">,</span> <span class="n">m_participant2</span> <span class="o">};</span>
-    <span class="o">}</span>
+    <span class="nd">@CompositioncompositionMngr</span><span class="o">,</span>
<span class="s">&quot;getComposition</span>
+<span class="s">    Object[] getComposition() {</span>
+<span class="s">        return new Object[] { this, m_participant1, m_participant2
};</span>
+<span class="s">    }</span>
+
+<span class="s">    @Start</span>
+<span class="s">    void start() {</span>
+<span class="s">        m_log.log(LogService.LOG_INFO, &quot;</span><span
class="n">ProviderImpl</span><span class="o">.</span><span class="na">start</span><span
class="o">():</span> <span class="n">participants</span><span class="o">=</span><span
class="s">&quot; + m_participant1 + &quot;</span><span class="o">,</span><span
class="s">&quot; + m_participant2</span>
+<span class="s">            + &quot;</span><span class="o">,</span>
<span class="n">conf</span><span class="o">=</span><span class="s">&quot;
+ m_conf);</span>
+<span class="s">    }      </span>
+<span class="s">}</span>
+
+<span class="s">public class ProviderParticipant1 {</span>
+<span class="s">    private volatile LogService m_log; // also injected, since we are
part of the composition</span>
+
+<span class="s">    void start() {</span>
+<span class="s">        m_log.log(LogService.LOG_INFO, &quot;</span><span
class="n">ProviderParticipant1</span><span class="o">.</span><span
class="na">start</span><span class="o">()</span><span class="s">&quot;);</span>
+<span class="s">    }</span>
+<span class="s">}</span>
 
-    <span class="nd">@Start</span>
-    <span class="kt">void</span> <span class="nf">start</span><span
class="o">()</span> <span class="o">{</span>
-        <span class="n">m_log</span><span class="o">.</span><span
class="na">log</span><span class="o">(</span><span class="n">LogService</span><span
class="o">.</span><span class="na">LOG_INFO</span><span class="o">,</span>
<span class="s">&quot;ProviderImpl.start(): participants=&quot;</span>
<span class="o">+</span> <span class="n">m_participant1</span> <span
class="o">+</span> <span class="s">&quot;,&quot;</span> <span
class="o">+</span> <span class="n">m_participant2</span>
-            <span class="o">+</span> <span class="s">&quot;, conf=&quot;</span>
<span class="o">+</span> <span class="n">m_conf</span><span class="o">);</span>
-    <span class="o">}</span>      
+<span class="s">public class ProviderParticipant2 {</span>
+<span class="s">    private volatile LogService m_log; // also injected, since we are
part of the composition</span>
+
+<span class="s">    void start() {</span>
+<span class="s">        m_log.log(LogService.LOG_INFO, &quot;</span><span
class="n">ProviderParticipant2</span><span class="o">.</span><span
class="na">start</span><span class="o">()</span><span class="err">&quot;</span><span
class="o">);</span>
+    <span class="o">}</span>
 <span class="o">}</span>
+</pre></div>
 
-<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">ProviderParticipant1</span> <span class="o">{</span>
-    <span class="kd">private</span> <span class="kd">volatile</span>
<span class="n">LogService</span> <span class="n">m_log</span><span
class="o">;</span> <span class="c1">// also injected, since we are part of
the composition</span>
 
-    <span class="kt">void</span> <span class="nf">start</span><span
class="o">()</span> <span class="o">{</span>
-        <span class="n">m_log</span><span class="o">.</span><span
class="na">log</span><span class="o">(</span><span class="n">LogService</span><span
class="o">.</span><span class="na">LOG_INFO</span><span class="o">,</span>
<span class="s">&quot;ProviderParticipant1.start()&quot;</span><span
class="o">);</span>
+<h2 id="factories">Factories</h2>
+<p>Out of the box, there already is support for lazy instantiation, meaning that the
dependency manager can create component instances for you when their required dependencies
are resolved. However, sometimes creating a single instance using a default constructor is
not enough. In those cases, you can tell the dependency manager to delegate the creation process
to a factory.</p>
+<p>Interestingly, you can also mix the usage of a Factory object and a Composition
of objects returned by the Factory.
+The following is the same example as in the previous section (Composition), but using a Factory
approach in order to instantiate a composition of objects: 
+The "ProviderFactory" is first injected with a Configuration that can be possibly be used
to create
+and configure all the other objects that are part of the composition; each object will also
injected with
+the depenencies defined in the Activator.</p>
+<div class="codehilite"><pre><span class="kd">public</span> <span
class="kd">class</span> <span class="nc">ProviderFactory</span> <span
class="o">{</span>
+    <span class="kd">private</span> <span class="n">ProviderParticipant1</span>
<span class="n">m_participant1</span><span class="o">;</span>
+    <span class="kd">private</span> <span class="n">ProviderParticipant2</span>
<span class="n">m_participant2</span><span class="o">;</span>
+    <span class="kd">private</span> <span class="n">ProviderImpl</span>
<span class="n">m_providerImpl</span><span class="o">;</span>
+    <span class="kd">private</span> <span class="n">Dictionary</span><span
class="o">&lt;</span><span class="n">String</span><span class="o">,</span>
<span class="n">String</span><span class="o">&gt;</span> <span
class="n">m_conf</span><span class="o">;</span>
+
+    <span class="kd">public</span> <span class="kt">void</span> <span
class="nf">updated</span><span class="o">(</span><span class="n">Dictionary</span><span
class="o">&lt;</span><span class="n">String</span><span class="o">,</span>
<span class="n">String</span><span class="o">&gt;</span> <span
class="n">conf</span><span class="o">)</span> <span class="kd">throws</span>
<span class="n">Exception</span> <span class="o">{</span>
+        <span class="c1">// validate configuration and throw an exception if the properties
are invalid</span>
+        <span class="n">m_conf</span> <span class="o">=</span> <span
class="n">conf</span><span class="o">;</span>
     <span class="o">}</span>
-<span class="o">}</span>
 
-<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">ProviderParticipant2</span> <span class="o">{</span>
-    <span class="kd">private</span> <span class="kd">volatile</span>
<span class="n">LogService</span> <span class="n">m_log</span><span
class="o">;</span> <span class="c1">// also injected, since we are part of
the composition</span>
+    <span class="cm">/**</span>
+<span class="cm">     * Builds the composition of objects used to implement the &quot;Provider&quot;
service.</span>
+<span class="cm">     * The Configuration injected by Config Admin will be used to
configure the components</span>
+<span class="cm">     * @return The &quot;main&quot; object providing the &quot;Provider&quot;
service.</span>
+<span class="cm">     */</span>
+    <span class="n">Object</span> <span class="nf">create</span><span
class="o">()</span> <span class="o">{</span>
+        <span class="c1">// Here, we can instantiate our object composition based on
our injected configuration ...</span>
+
+        <span class="k">if</span> <span class="o">(</span><span
class="s">&quot;true&quot;</span><span class="o">.</span><span
class="na">equals</span><span class="o">(</span><span class="n">m_conf</span><span
class="o">.</span><span class="na">get</span><span class="o">(</span><span
class="s">&quot;some.parameter&quot;</span><span class="o">))</span>
<span class="o">{</span>
+            <span class="n">m_participant1</span> <span class="o">=</span>
<span class="k">new</span> <span class="n">ProviderParticipant1</span><span
class="o">();</span> <span class="c1">// depenencies and lifecycle callbacks
will also be applied</span>
+            <span class="n">m_participant2</span> <span class="o">=</span>
<span class="k">new</span> <span class="n">ProviderParticipant2</span><span
class="o">();</span> <span class="c1">// depenencies and lifecycle callbacks
will also be applied</span>
+        <span class="o">}</span> <span class="k">else</span> <span
class="o">{</span>
+            <span class="c1">// Compose with some other objects ...</span>
+            <span class="n">m_participant1</span> <span class="o">=</span>
<span class="k">new</span> <span class="n">ProviderParticipant3</span><span
class="o">();</span> <span class="c1">// depenencies and lifecycle callbacks
will also be applied</span>
+            <span class="n">m_participant2</span> <span class="o">=</span>
<span class="k">new</span> <span class="n">ProviderParticipant4</span><span
class="o">();</span> <span class="c1">// depenencies and lifecycle callbacks
will also be applied</span>
+        <span class="o">}</span>
 
-    <span class="kt">void</span> <span class="nf">start</span><span
class="o">()</span> <span class="o">{</span>
-        <span class="n">m_log</span><span class="o">.</span><span
class="na">log</span><span class="o">(</span><span class="n">LogService</span><span
class="o">.</span><span class="na">LOG_INFO</span><span class="o">,</span>
<span class="s">&quot;ProviderParticipant2.start()&quot;</span><span
class="o">);</span>
+        <span class="n">m_providerImpl</span> <span class="o">=</span>
<span class="k">new</span> <span class="n">ProviderImpl</span><span
class="o">(</span><span class="n">m_participant1</span><span class="o">,</span>
<span class="n">m_participant2</span><span class="o">);</span>
+        <span class="k">return</span> <span class="n">m_providerImpl</span><span
class="o">;</span> <span class="c1">// Main object implementing the Provider
service</span>
+    <span class="o">}</span>
+
+    <span class="cm">/**</span>
+<span class="cm">     * Returns the list of objects that are part of the composition
for the Provider implementation.</span>
+<span class="cm">     */</span>
+    <span class="n">Object</span><span class="o">[]</span> <span
class="nf">getComposition</span><span class="o">()</span> <span class="o">{</span>
+        <span class="k">return</span> <span class="k">new</span>
<span class="n">Object</span><span class="o">[]</span> <span class="o">{</span>
<span class="n">m_providerImpl</span><span class="o">,</span> <span
class="n">m_participant1</span><span class="o">,</span> <span class="n">m_participant2</span>
<span class="o">};</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+
+<span class="o">:::</span><span class="n">java</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">Activator</span> <span class="kd">extends</span> <span
class="n">DependencyActivatorBase</span> <span class="o">{</span>
+    <span class="kd">public</span> <span class="kt">void</span> <span
class="nf">init</span><span class="o">(</span><span class="n">BundleContext</span>
<span class="n">ctx</span><span class="o">,</span> <span class="n">DependencyManager</span>
<span class="n">m</span><span class="o">)</span> <span class="kd">throws</span>
<span class="n">Exception</span> <span class="o">{</span>
+        <span class="n">ProviderFactory</span> <span class="n">factory</span>
<span class="o">=</span> <span class="k">new</span> <span class="n">ProviderFactory</span><span
class="o">();</span>
+        <span class="n">m</span><span class="o">.</span><span
class="na">add</span><span class="o">(</span><span class="n">createComponent</span><span
class="o">()</span>
+            <span class="o">.</span><span class="na">setFactory</span><span
class="o">(</span><span class="n">factory</span><span class="o">,</span>
<span class="s">&quot;create&quot;</span><span class="o">)</span>
+            <span class="o">.</span><span class="na">setComposition</span><span
class="o">(</span><span class="n">factory</span><span class="o">,</span>
<span class="s">&quot;getComposition&quot;</span><span class="o">)</span>
+            <span class="o">.</span><span class="na">add</span><span
class="o">(</span><span class="n">createConfigurationDependency</span><span
class="o">()</span>
+                <span class="o">.</span><span class="na">setPid</span><span
class="o">(</span><span class="s">&quot;some.pid&quot;</span><span
class="o">)</span>
+                <span class="o">.</span><span class="na">setCallback</span><span
class="o">(</span><span class="n">factory</span><span class="o">,</span>
<span class="s">&quot;updated&quot;</span><span class="o">))</span>
<span class="c1">// will invoke &quot;updated&quot; on the factory instance</span>
+            <span class="o">.</span><span class="na">add</span><span
class="o">(</span><span class="n">createServiceDependency</span><span
class="o">().</span><span class="na">setService</span><span class="o">(</span><span
class="n">LogService</span><span class="o">.</span><span class="na">class</span><span
class="o">).</span><span class="na">setRequired</span><span class="o">(</span><span
class="kc">true</span><span class="o">)));</span>
     <span class="o">}</span>
 <span class="o">}</span>
 </pre></div>
 
 
-<h2 id="factories">Factories</h2>
-<p>Out of the box, there already is support for lazy instantiation, meaning that the
dependency manager can create component instances for you when their required dependencies
are resolved. However, sometimes creating a single instance using a default constructor is
not enough. In those cases, you can tell the dependency manager to delegate the creation process
to a factory.</p>
+<p>You can refer to this <a href="https://svn.apache.org/repos/asf/felix/trunk/dependencymanager/org.apache.felix.dependencymanager.samples/src/org/apache/felix/dependencymanager/samples/compositefactory/">sample
code</a>, which is part of the source distribution.</p>
       <div class="timestamp" style="margin-top: 30px; font-size: 80%; text-align: right;">
-        Rev. 1666333 by pderop on Thu, 12 Mar 2015 23:13:00 +0000
+        Rev. 1666335 by pderop on Thu, 12 Mar 2015 23:42:41 +0000
       </div>
       <div class="trademarkFooter"> 
         Apache Felix, Felix, Apache, the Apache feather logo, and the Apache Felix project



Mime
View raw message