Added comments. Cleaned hello sample.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1727908 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dm/lambda/samples/hello/Activator.java b/dependencymanager/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dm/lambda/samples/hello/Activator.java
index f2b8dea..8dfd611 100644
--- a/dependencymanager/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dm/lambda/samples/hello/Activator.java
+++ b/dependencymanager/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dm/lambda/samples/hello/Activator.java
@@ -33,22 +33,22 @@
public void activate() throws Exception {
out.println("type \"log info\" to see the logs emitted by this test.");
- // Creates a Service Provider
- component(comp -> comp
- .impl(ServiceProviderImpl.class)
- .provides(ServiceProvider.class, property1 -> "value1", property2 -> 123) // property names are deduced from lambda parameter names
+ // Creates a Service Provider (property names are deduced from lambda parameter names).
+ // (service dependencies are required by default)
+ component(comp -> comp.impl(ServiceProviderImpl.class)
+ .provides(ServiceProvider.class, p1 -> "v1", p2 -> 123)
.withSrv(LogService.class));
-
+
// Creates a Service Consumer. we depend on LogService, EventAdmin and on our ServiceProvider.
// (LogService and EventAdmin are declared in one single method call).
-
- component(comp -> comp
- .impl(ServiceConsumer.class)
+ component(comp -> comp.impl(ServiceConsumer.class)
.withSrv(LogService.class, EventAdmin.class)
- .withSrv(ServiceProvider.class, srv -> srv.filter("(property1=value1)"))
+ .withSrv(ServiceProvider.class, srv -> srv.filter("(p1=v1)"))
.withCnf(ServiceConsumer.class));
// Creates a component that populates some properties in the Configuration Admin.
- component(comp -> comp.impl(Configurator.class).withSrv(ConfigurationAdmin.class));
+ // Here, we inject the CM (Configuration Admin) service dependency using a method reference:
+ component(comp -> comp.impl(Configurator.class)
+ .withSrv(ConfigurationAdmin.class, srv -> srv.cb(Configurator::bind)));
}
}
diff --git a/dependencymanager/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dm/lambda/samples/hello/Configurator.java b/dependencymanager/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dm/lambda/samples/hello/Configurator.java
index 544c7cb..648a92f 100644
--- a/dependencymanager/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dm/lambda/samples/hello/Configurator.java
+++ b/dependencymanager/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dm/lambda/samples/hello/Configurator.java
@@ -8,7 +8,11 @@
import org.osgi.service.cm.ConfigurationAdmin;
public class Configurator {
- volatile ConfigurationAdmin m_cm; // injected by reflection.
+ ConfigurationAdmin m_cm;
+
+ void bind(ConfigurationAdmin cm) {
+ m_cm = cm;
+ }
void start() throws IOException {
// Configure the ServiceConsumer component
diff --git a/dependencymanager/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dm/lambda/samples/hello/README b/dependencymanager/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dm/lambda/samples/hello/README
index d762a3c..4797d79 100644
--- a/dependencymanager/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dm/lambda/samples/hello/README
+++ b/dependencymanager/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dm/lambda/samples/hello/README
@@ -21,3 +21,9 @@
g! log info|grep hello.annot
+and you should see the following logs:
+
+2016.02.01 10:12:02 INFO - Bundle: org.apache.felix.dependencymanager.lambda.samples.hello - ServiceProviderImpl.hello
+2016.02.01 10:12:02 INFO - Bundle: org.apache.felix.dependencymanager.lambda.samples.hello - ServiceConsumer.start: calling service.hello()
+
+
diff --git a/dependencymanager/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dm/lambda/samples/hello/ServiceProviderImpl.java b/dependencymanager/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dm/lambda/samples/hello/ServiceProviderImpl.java
index b0134c0..2d1fbba 100644
--- a/dependencymanager/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dm/lambda/samples/hello/ServiceProviderImpl.java
+++ b/dependencymanager/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dm/lambda/samples/hello/ServiceProviderImpl.java
@@ -26,10 +26,24 @@
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
*/
public class ServiceProviderImpl implements ServiceProvider {
+ /**
+ * Our log service, injected from Activator.
+ * If field is only accessed by callbacks, then you do not need to declare it as volatile, because the DM thread model
+ * ensures "safe publications" of objects when lifecycle or dependency callbacks are invoked.
+ * Here we declare the field using volatile because our hello method may be invoked from any thread, and declaring the field
+ * as volatile will ensure safe publication if the log service is replaced at runtime.
+ */
volatile LogService log;
void start() {
- // default lifecycle start calback (all required dependencies have been injected when start is called.
+ // Default lifecycle start calback (all required dependencies have been injected when start is called,
+ // and all optional dependency callbacks will be invoked after the start method. This allows to easily
+ // implement the whiteboard pattern: you are first injected with required dependencies, then you can initialize
+ // your component from the start callback, and then optional dependency callbacks are invoked.
+
+ // Notice that all callbacks are serially executed and you don't need to synchronize your callbacks and
+ // you can perform any manual service registrations (using BundleContext.registerService) whithout dealing
+ // with synchronization, because no locks are held by DM when callbacks are invoked.
}
@Override