Author: pderop
Date: Thu Mar 12 23:13:00 2015
New Revision: 1666333

URL: http://svn.apache.org/r1666333
Log:
Added more documentation about Service Compositions.

Modified:
    
felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/components.mdtext

Modified: 
felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/components.mdtext
URL: 
http://svn.apache.org/viewvc/felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/components.mdtext?rev=1666333&r1=1666332&r2=1666333&view=diff
==============================================================================
--- 
felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/components.mdtext
 (original)
+++ 
felix/site/trunk/content/documentation/subprojects/apache-felix-dependency-manager/reference/components.mdtext
 Thu Mar 12 23:13:00 2015
@@ -94,7 +94,104 @@ Components in the context of the depende
 
 ## Composition
 
-When implementing more complex components, you often find yourself using more 
than one instance. However, several of these instances might want to have 
dependencies injected. In such cases you need to tell the dependency manager 
which instances to consider. This has to be a fixed set of instances however.
+When implementing more complex components, you often find yourself using more 
than one 
+instance. However, several of these instances might want to have dependencies 
injected. 
+In such cases you need to tell the dependency manager which instances to 
consider. 
+This has to be a fixed set of instances however.
+
+We now describe how to declare a service composition using the Api, and the 
Annotations:
+
+### Composition / API
+
+When using the DependencyManager API, you can use the 
*Component.setComposition* method to declare a special callback in your 
component that 
+returns the list of object that are part of the component, and all 
dependencies and lifecycle callbacks will be invoked
+on the objects returned by the method. Let's take an example, with a 
*ProviderImpl* top-level service implementation
+that is internally implemented using three Pojos: *ProviderImpl*, 
*ProviderParticipant1*, and 
+*ProviderParticipant2*:
+
+    :::java
+    public class ProviderImpl implements Provider {
+        private final ProviderParticipant1 m_participant1 = new 
ProviderParticipant1();
+        private final ProviderParticipant2 m_participant2 = new 
ProviderParticipant2();
+        private volatile LogService m_log; // injected
+
+        Object[] getComposition() {
+            return new Object[] { this, m_participant1, m_participant2 };
+        }
+
+        void start() {
+            m_log.log(LogService.LOG_INFO, "ProviderImpl.start(): 
participants=" + m_participant1 + "," + m_participant2
+                + ", conf=" + m_conf);
+        }      
+    }
+
+    public class ProviderParticipant1 {
+        private volatile LogService m_log; // also injected since we are part 
of the composition
+
+        void start() {
+            m_log.log(LogService.LOG_INFO, "ProviderParticipant1.start()");
+        }
+    }
+
+    public class ProviderParticipant2 {
+        private volatile LogService m_log; // also injected since we are part 
of the composition
+
+        void start() {
+            m_log.log(LogService.LOG_INFO, "ProviderParticipant2.start()");
+        }
+    }
+
+And here is the Activator, which uses the *setComposition* method:
+
+    :::java
+    public class Activator extends DependencyActivatorBase {
+        public void init(BundleContext ctx, DependencyManager m) throws 
Exception {
+            m.add(createComponent()
+                .setImplementation(ProviderImpl.class)
+                .setComposition("getComposition")
+                
.add(createServiceDependency().setService(LogService.class).setRequired(true)));
+        }
+    }
+
+### Composition / Annotations
+
+Here is the same example as above, but using Annotations:
+
+    :::java
+    @Component
+    public class ProviderImpl implements Provider {
+        private final ProviderParticipant1 m_participant1 = new 
ProviderParticipant1();
+        private final ProviderParticipant2 m_participant2 = new 
ProviderParticipant2();
+        private volatile LogService m_log; // injected
+
+        @Composition
+        Object[] getComposition() {
+            return new Object[] { this, m_participant1, m_participant2 };
+        }
+
+        @Start
+        void start() {
+            m_log.log(LogService.LOG_INFO, "ProviderImpl.start(): 
participants=" + m_participant1 + "," + m_participant2
+                + ", conf=" + m_conf);
+        }      
+    }
+
+    public class ProviderParticipant1 {
+        private volatile LogService m_log; // also injected, since we are part 
of the composition
+
+        void start() {
+            m_log.log(LogService.LOG_INFO, "ProviderParticipant1.start()");
+        }
+    }
+
+    public class ProviderParticipant2 {
+        private volatile LogService m_log; // also injected, since we are part 
of the composition
+
+        void start() {
+            m_log.log(LogService.LOG_INFO, "ProviderParticipant2.start()");
+        }
+    }
+
 
 ## Factories
 


Reply via email to