Return-Path: X-Original-To: apmail-felix-commits-archive@www.apache.org Delivered-To: apmail-felix-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id E1539184B8 for ; Wed, 3 Feb 2016 13:42:29 +0000 (UTC) Received: (qmail 99745 invoked by uid 500); 3 Feb 2016 13:42:20 -0000 Delivered-To: apmail-felix-commits-archive@felix.apache.org Received: (qmail 99700 invoked by uid 500); 3 Feb 2016 13:42:20 -0000 Mailing-List: contact commits-help@felix.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@felix.apache.org Delivered-To: mailing list commits@felix.apache.org Received: (qmail 99691 invoked by uid 99); 3 Feb 2016 13:42:20 -0000 Received: from Unknown (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 03 Feb 2016 13:42:20 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id CED8BC01A8 for ; Wed, 3 Feb 2016 13:42:19 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.255 X-Spam-Level: * X-Spam-Status: No, score=1.255 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RP_MATCHES_RCVD=-0.545] autolearn=disabled Received: from mx1-us-east.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id jVS_-gHx6m6S for ; Wed, 3 Feb 2016 13:42:12 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-us-east.apache.org (ASF Mail Server at mx1-us-east.apache.org) with ESMTP id 20379428ED for ; Wed, 3 Feb 2016 13:42:12 +0000 (UTC) Received: from svn01-us-west.apache.org (svn.apache.org [10.41.0.6]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 96144E0428 for ; Wed, 3 Feb 2016 13:42:11 +0000 (UTC) Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id 956A63A0C11 for ; Wed, 3 Feb 2016 13:42:11 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r979376 - in /websites/staging/felix/trunk/content: ./ documentation/subprojects/apache-felix-dependency-manager/guides/dm-lambda.html Date: Wed, 03 Feb 2016 13:42:11 -0000 To: commits@felix.apache.org From: buildbot@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20160203134211.956A63A0C11@svn01-us-west.apache.org> Author: buildbot Date: Wed Feb 3 13:42:11 2016 New Revision: 979376 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/guides/dm-lambda.html Propchange: websites/staging/felix/trunk/content/ ------------------------------------------------------------------------------ --- cms:source-revision (original) +++ cms:source-revision Wed Feb 3 13:42:11 2016 @@ -1 +1 @@ -1728264 +1728309 Modified: websites/staging/felix/trunk/content/documentation/subprojects/apache-felix-dependency-manager/guides/dm-lambda.html ============================================================================== --- websites/staging/felix/trunk/content/documentation/subprojects/apache-felix-dependency-manager/guides/dm-lambda.html (original) +++ websites/staging/felix/trunk/content/documentation/subprojects/apache-felix-dependency-manager/guides/dm-lambda.html Wed Feb 3 13:42:11 2016 @@ -109,11 +109,13 @@ the DependencyManager class automaticall

You can first instantiate builders using some of the convenient factory methods available from the DependencyManagerActivator class, which is the new base class for dm-lambda activators:

import org.apache.felix.dm.lambda.DependencyManagerActivator;
+import org.apache.felix.dm.Component;
 
-public class Activator extends DependencyActivatorBase {
+public class Activator extends DependencyManagerActivator {
     @Override
-    public void init(BundleContext ctx, DependencyManager dm) throws Exception {
+    public void activate() throws Exception {
         Component comp = component().impl(Hello.class).build();
+        DependencyManager dm = getDM();
         dm.add(comp);
     }
 }
@@ -126,9 +128,12 @@ So, in order to reduce the code size, yo
 Consumer<ComponentBuilder> parameter.
 So, the lambda has just to invoke the chain of necessary methods from the builder, without having to call build and add the returned Component to the dm object.

The following is the same as above, using a consumer<ComponentBuilder> lambda expression:

-
public class Activator extends DependencyActivatorBase {
+
import org.apache.felix.dm.lambda.DependencyManagerActivator;
+import org.apache.felix.dm.lambda.ComponentBuilder;
+
+public class Activator extends DependencyManagerActivator {
     @Override
-    public void init(BundleContext ctx, DependencyManager dm) throws Exception {
+    public void activate() throws Exception {
         component((ComponentBuilder comp) -> comp.impl(Hello.class));
     }
 }
@@ -136,9 +141,11 @@ So, the lambda has just to invoke the ch
 
 
 

And here is a more concise version where the type of the lambda parameter is not declared:

-
public class Activator extends DependencyActivatorBase {
+
import org.apache.felix.dm.lambda.DependencyManagerActivator;
+
+public class Activator extends DependencyManagerActivator {
     @Override
-    public void init(BundleContext ctx, DependencyManager dm) throws Exception {
+    public void activate() throws Exception {
         component(comp -> comp.impl(Hello.class));
     }
 }
@@ -148,9 +155,12 @@ So, the lambda has just to invoke the ch
 

Adding service dependencies

Service Dependencies, unlike in the original DM API, are required by default, and you can add a dependency using the withSrv methods available from the ComponentBuilder interface. Such method accepts a Consumer<ServiceDependencyBuilder> lambda expression, which may then configure the dependency using a chain of method calls (filter/callbacks,autoconfig, etc ...):

-
public class Activator extends DependencyActivatorBase {
+
import org.apache.felix.dm.lambda.DependencyManagerActivator;
+import org.apache.felix.dm.lambda.ServiceDependencyBuilder;
+
+public class Activator extends DependencyManagerActivator {
     @Override
-    public void init(BundleContext ctx, DependencyManager dm) throws Exception {
+    public void activate() throws Exception {
         component(comp -> comp.impl(Hello.class)
             .withSrv(LogService.class, (ServiceDependencyBuilder srv) -> srv.filter("(vendor=apache)")));
     }
@@ -159,9 +169,11 @@ Such method accepts a Consumer<
 
 
 

The above example adds a service dependency on a LogService with a service filter. Here is a more concise version where the type of the srv lambda parameter is not declared:

-
public class Activator extends DependencyActivatorBase {
+
import org.apache.felix.dm.lambda.DependencyManagerActivator;
+
+public class Activator extends DependencyManagerActivator {
     @Override
-    public void init(BundleContext ctx, DependencyManager dm) throws Exception {
+    public void activate() throws Exception {
         component(comp -> comp.impl(Hello.class).withSrv(LogService.class, srv -> srv.filter("(vendor=apache)")));
     }
 }
@@ -169,9 +181,11 @@ Such method accepts a Consumer<
 
 
 

If you depend on multiple required services (without filters), you can declare the services in one shot like this:

-
public class Activator extends DependencyActivatorBase {
+
import org.apache.felix.dm.lambda.DependencyManagerActivator;
+
+public class Activator extends DependencyManagerActivator {
     @Override
-    public void init(BundleContext ctx, DependencyManager dm) throws Exception {
+    public void activate() throws Exception {
         // using a varargs of service dependencies ...
         component(comp -> comp.impl(Hello.class).withSrv(LogService.class, EventAdmin.class)); 
     }
@@ -182,9 +196,11 @@ Such method accepts a Consumer<
 

Defining Service Dependency Component's callbacks

By default, service dependencies are auto injected in class fields (you can configure the name of the class field where the dependency should be injected). But like in the current DM API, you can specify callbacks on the component implementation class using the "cb" ServiceDependencyBuilder method:

-
public class Activator extends DependencyActivatorBase {
+
import org.apache.felix.dm.lambda.DependencyManagerActivator;
+
+public class Activator extends DependencyManagerActivator {
     @Override
-    public void init(BundleContext ctx, DependencyManager dm) throws Exception {
+    public void activate() throws Exception {
         component(comp -> comp.impl(Hello.class).withSrv(LogService.class, srv -> srv.cb("setLog")));
     }
 }
@@ -220,9 +236,11 @@ But like in the current DM API, you can
 
 
 

Now you can also use a more type-safe callback using a Java 8 method reference:

-
public class Activator extends DependencyActivatorBase {
+
import org.apache.felix.dm.lambda.DependencyManagerActivator;
+
+public class Activator extends DependencyManagerActivator {
     @Override
-    public void init(BundleContext ctx, DependencyManager dm) throws Exception {
+    public void activate() throws Exception {
         component(comp -> comp.impl(Hello.class).withSrv(LogService.class, srv -> srv.cb(Hello::setLog)));
     }
 }
@@ -230,9 +248,11 @@ But like in the current DM API, you can
 
 
 

or:

-
public class Activator extends DependencyActivatorBase {
+
import org.apache.felix.dm.lambda.DependencyManagerActivator;
+
+public class Activator extends DependencyManagerActivator {
     @Override
-    public void init(BundleContext ctx, DependencyManager dm) throws Exception {
+    public void activate() throws Exception {
         component(comp -> comp.impl(Hello.class).withSrv(LogService.class, srv -> srv.cb(Hello::setLog, Hello::unsetLog)));
     }
 }
@@ -243,9 +263,11 @@ But like in the current DM API, you can
 

Sometimes, you want to inject the dependency to a seperate object that is not part of the component implementation classes. In this case, you can use the "cbi" method (which stands for "callback instance").

For example, the following example injects a dependency in a DependencyHandler instance:

-
public class Activator extends DependencyActivatorBase {
+
import org.apache.felix.dm.lambda.DependencyManagerActivator;
+
+public class Activator extends DependencyManagerActivator {
     @Override
-    public void init(BundleContext ctx, DependencyManager dm) throws Exception {
+    public void activate() throws Exception {
         DependencyHandler depHandler = new DependencyHandler();
         component(comp -> comp.impl(Hello.class).withSrv(LogService.class, srv -> srv.cbi(depHandler, "setLog")));
     }
@@ -254,9 +276,11 @@ In this case, you can use the "cbi
 
 
 

or using method reference:

-
public class Activator extends DependencyActivatorBase {
+
import org.apache.felix.dm.lambda.DependencyManagerActivator;
+
+public class Activator extends DependencyManagerActivator {
     @Override
-    public void init(BundleContext ctx, DependencyManager dm) throws Exception {
+    public void activate() throws Exception {
         DependencyHandler depHandler = new DependencyHandler();
         component(comp -> comp.impl(Hello.class).withSrv(LogService.class, srv -> srv.cbi(depHandler::setLog)));
     }
@@ -265,9 +289,11 @@ In this case, you can use the "cbi
 
 
 

You can chain multiple callbacks:

-
public class Activator extends DependencyActivatorBase {
+
import org.apache.felix.dm.lambda.DependencyManagerActivator;
+
+public class Activator extends DependencyManagerActivator {
     @Override
-    public void init(BundleContext ctx, DependencyManager dm) throws Exception {
+    public void activate() throws Exception {
         DependencyHandler depHandler = new DependencyHandler();
         component(comp -> comp.impl(Hello.class).withSrv(LogService.class, srv -> srv.cb(Hello::setLog).cbi(depHandler::setLog)));
     }
@@ -278,9 +304,11 @@ In this case, you can use the "cbi
 

Providing a service

When a component provides a service with some properties, so far it was necessary to create a Dictionary and pass it to the Component.setInterface() method.

Now you can pass properties directly to the provides method as varargs of properties (a suite of key-value properties):

-
public class Activator extends DependencyActivatorBase {
+
import org.apache.felix.dm.lambda.DependencyManagerActivator;
+
+public class Activator extends DependencyManagerActivator {
     @Override
-    public void init(BundleContext ctx, DependencyManager dm) throws Exception {
+    public void activate() throws Exception {
         component(comp -> comp.impl(Hello.class).provides(HelloService.class, "p1", "v1", "p2", 123));
     }
 }
@@ -289,9 +317,11 @@ In this case, you can use the "cbi
 
 

or if you build your program using the -parameters option, you can also use the "FluentProperty" lambda that allows to declare service properties as a suite of "key -> value" lambdas, like this:

-
public class Activator extends DependencyActivatorBase {
+
import org.apache.felix.dm.lambda.DependencyManagerActivator;
+
+public class Activator extends DependencyManagerActivator {
     @Override
-    public void init(BundleContext ctx, DependencyManager dm) throws Exception {
+    public void activate() throws Exception {
         component(comp -> comp.impl(Hello.class).provides(HelloService.class, p1 -> "v1", p2 -> 123));
     }
 }
@@ -300,11 +330,13 @@ service properties as a suite of "
 
 

Managing components outside of Activators.

You can manage Components outside of the Activator by using some static factory methods from the DependencyManagerActivator class.

-

For example, considere a use case where you want to retrieve some informations from some already injected services, and you then want to dynamically add more dependencies from your +

For example, consider a use case where you want to retrieve some informations from some already injected services, and you then want to dynamically add more dependencies from your init component callback. First let's look at the Activator:

-
public class Activator extends DependencyActivatorBase {
+
import org.apache.felix.dm.lambda.DependencyManagerActivator;
+
+public class Activator extends DependencyManagerActivator {
     @Override
-    public void init(BundleContext ctx, DependencyManager dm) throws Exception {
+    public void activate() throws Exception {
         component(comp -> comp.impl(Pojo.class).withCnf("pojo.pid"));
     }
 }
@@ -314,6 +346,7 @@ service properties as a suite of "
 

Here, we define a Configuration dependency with a "pojo.pid" configuration pid. So, now, the Pojo will then for example be able to parse an xml from the configuration, and depending on what it has parsed, it will possibly add more dependencies, like this:

import static org.apache.felix.dm.lambda.DependencyManagerActivator.*;
+import org.apache.felix.dm.Component;
 
 public class Pojo {
     void updated(Dictionary conf) throws Exception {
@@ -330,7 +363,12 @@ what it has parsed, it will possibly add
 
 
 

The available variety of factory methods allows you to also create some DM objects and add them manually, like:

-
public class Pojo {
+
import static org.apache.felix.dm.lambda.DependencyManagerActivator.*;
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.ServiceDependency;
+import org.apache.felix.dm.DependencyManager;
+
+public class Pojo {
     void updated(Dictionary conf) throws Exception {
         parseXml(conf.get("some.xml.configuration"));
     }
@@ -347,7 +385,10 @@ what it has parsed, it will possibly add
 
 
 

And an example where you create a new DM component from the code:

-
public class Pojo {
+
import static org.apache.felix.dm.lambda.DependencyManagerActivator.*;
+import org.apache.felix.dm.DependencyManager;
+
+public class Pojo {
     volatile DependencyManager m_dm;
 
     void createComponent() {
@@ -372,9 +413,11 @@ client, the method returns to you a And once the result will be completed, you will then be called in your start() callback, and at this point, the Tracked services will then
 be injected (using DM, optional service callbacks are always invoked after the start() callback, never before).

So, the Activator looks like this:

-
public class Activator extends DependencyActivatorBase {
+
import org.apache.felix.dm.lambda.DependencyManagerActivator;
+
+public class Activator extends DependencyManagerActivator {
     @Override
-    public void init(BundleContext ctx, DependencyManager dm) throws Exception {
+    public void activate() throws Exception {
         component(comp -> comp.impl(Pojo.class).provides(PojoService)
            .withCnf("foo.pid").withSrv(HttpClient.class)
            .withSrv(Tracked.class, srv -> srv.optional().cb(Pojo::bindTracked));
@@ -388,6 +431,7 @@ for the result of the CompletableF
 in the setPage callback, then the start() callback will be called, and finally, any registered Tracked services will be
 injected in the "bindTracked" method:

import static org.apache.felix.dm.lambda.DependencyManagerActivator.*;
+import org.apache.felix.dm.Component;
 
 public class Pojo implements PojoService {
     HttpClient m_httpClient; // injected.
@@ -551,7 +595,7 @@ and the service is registered.

Caution: if you are using a corporate http proxy, you have to fix the Activator in order to configure the ip addr and port number of your http proxy.

- Rev. 1728264 by pderop on Wed, 3 Feb 2016 07:44:32 +0000 + Rev. 1728309 by pderop on Wed, 3 Feb 2016 13:41:34 +0000
Apache Felix, Felix, Apache, the Apache feather logo, and the Apache Felix project