Modified: felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/future/Activator.java URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/future/Activator.java?rev=1724333&r1=1724332&r2=1724333&view=diff ============================================================================== --- felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/future/Activator.java (original) +++ felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/future/Activator.java Tue Jan 12 22:45:36 2016 @@ -19,35 +19,44 @@ package org.apache.felix.dependencymanager.lambda.samples.future; import static java.lang.System.out; +import static org.apache.felix.dm.builder.lambda.CB.ADD; + +import java.util.List; +import java.util.concurrent.CompletableFuture; import org.apache.felix.dm.builder.lambda.DependencyActivatorBase; import org.osgi.service.log.LogService; /** - * Defines two components: FelixSite which provides some informations about the Felix Web site, - * and Test, which depends in the FelixSite service. - * - * The FelixSite component will asynchronously download the Felix web page, and uses a "CompletableFuture" - * dependency, in order to block the activation of the FelixSite service, until the web page is downloaded and parsed. - * - * The download is done using some CompletableFuture asynchronous tasks. + * This examples show how to use the new "Future" dependency available from the dependencymanager-lambda library. + * The FelixLinksImpl component provides the list of available hrefs found from the Felix web site. + * The page is downloaded asynchronously using a CompletableFuture, and the component of the FelixLinksImpl + * will wait for the future to be completed before start. * * @author <a href="mailto:[email protected]">Felix Project Team</a> */ public class Activator extends DependencyActivatorBase { + @Override public void init() throws Exception { out.println("type \"log info\" to see the logs emitted by this test."); - - // Define the FelixSiteInfo component that provides some informations about the Felix web site. + + // Creates a future that asynchronously download Felix web site. + String felix = "http://felix.apache.org/"; + CompletableFuture<List<String>> futureLinks = CompletableFuture.supplyAsync(() -> FelixLinksImpl.download(felix)) + .thenApply(FelixLinksImpl::parseLinks); + + // Create the FelixLink service, it will be started once our previous future has completed. component(comp -> comp - .provides(SiteInfo.class) - .factory(() -> new SiteInfoImpl("http://felix.apache.org/")) - .withService(LogService.class, srv -> srv.onAdd(SiteInfoImpl::bind))); - - // Define the FelixSite component that depends on the FelixSiteInfo service + .impl(FelixLinksImpl.class) + .provides(FelixLinks.class) + .withService(LogService.class, srv -> srv.cb(ADD, FelixLinksImpl::bind)) + .withFuture(futureLinks, future -> future.cb(FelixLinksImpl::setLinks))); + + // Define a component that just displays the links found from the Felix web site. + // It depends on a log service and on the FelixLink service, which are both injected in class fields. component(comp -> comp - .impl(DisplaySite.class) - .withService(LogService.class).withService(SiteInfo.class)); + .impl(DisplayFelixLinks.class) + .withService(LogService.class, FelixLinks.class)); } }
Added: felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/future/DisplayFelixLinks.java URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/future/DisplayFelixLinks.java?rev=1724333&view=auto ============================================================================== --- felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/future/DisplayFelixLinks.java (added) +++ felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/future/DisplayFelixLinks.java Tue Jan 12 22:45:36 2016 @@ -0,0 +1,16 @@ +package org.apache.felix.dependencymanager.lambda.samples.future; + +import org.osgi.service.log.LogService; + +/** + * Displays all links found from Felix web site, using the FelixLinks service. + */ +public class DisplayFelixLinks { + volatile FelixLinks m_siteInfo; + volatile LogService m_log; + + void start() { + m_log.log(LogService.LOG_INFO, "DisplaySite.start(): links available from the Felix web site: " + m_siteInfo.getLinks()); + } + +} Added: felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/future/FelixLinks.java URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/future/FelixLinks.java?rev=1724333&view=auto ============================================================================== --- felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/future/FelixLinks.java (added) +++ felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/future/FelixLinks.java Tue Jan 12 22:45:36 2016 @@ -0,0 +1,10 @@ +package org.apache.felix.dependencymanager.lambda.samples.future; + +import java.util.List; + +/** + * Service that displays all links found from the Felix web site. + */ +public interface FelixLinks { + List<String> getLinks(); +} Added: felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/future/FelixLinksImpl.java URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/future/FelixLinksImpl.java?rev=1724333&view=auto ============================================================================== --- felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/future/FelixLinksImpl.java (added) +++ felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/future/FelixLinksImpl.java Tue Jan 12 22:45:36 2016 @@ -0,0 +1,63 @@ +package org.apache.felix.dependencymanager.lambda.samples.future; + +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.osgi.service.log.LogService; + +/** + * Provides all hrefs found from Felix web site. + */ +public class FelixLinksImpl implements FelixLinks { + private LogService m_log; + final static String HREF_PATTERN = "<a\\s+href\\s*=\\s*(\"[^\"]*\"|[^\\s>]*)\\s*>"; + List<String> m_links; // web page hrefs (links). + + void bind(LogService log) { + m_log = log; + } + + // Called when our future has completed. + void setLinks(List<String> links) { + m_links = links; + } + + // once our future has completed, our component is started. + void start() { + m_log.log(LogService.LOG_INFO, "Service starting: number of links found from Felix web site: " + m_links.size()); + } + + @Override + public List<String> getLinks() { + return m_links; + } + + public static String download(String url) { + try (Scanner in = new Scanner(new URL(url).openStream())) { + StringBuilder builder = new StringBuilder(); + while (in.hasNextLine()) { + builder.append(in.nextLine()); + builder.append("\n"); + } + return builder.toString(); + } catch (IOException ex) { + RuntimeException rex = new RuntimeException(); + rex.initCause(ex); + throw rex; + } + } + + public static List<String> parseLinks(String content) { + Pattern pattern = Pattern.compile(HREF_PATTERN, Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(content); + List<String> result = new ArrayList<>(); + while (matcher.find()) + result.add(matcher.group(1)); + return result; + } +} Modified: felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/hello/Activator.java URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/hello/Activator.java?rev=1724333&r1=1724332&r2=1724333&view=diff ============================================================================== --- felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/hello/Activator.java (original) +++ felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/hello/Activator.java Tue Jan 12 22:45:36 2016 @@ -19,6 +19,7 @@ package org.apache.felix.dependencymanager.lambda.samples.hello; import static java.lang.System.out; +import static org.apache.felix.dm.builder.lambda.CB.ADD; import org.apache.felix.dm.builder.lambda.DependencyActivatorBase; import org.osgi.service.cm.ConfigurationAdmin; @@ -33,18 +34,19 @@ public class Activator extends Dependenc out.println("type \"log info\" to see the logs emitted by this test."); component(comp -> comp - .provides(ServiceProvider.class) - .onStart(ServiceProviderImpl::activate) - //.properties("foo", "bar", "gabu", "zo") // foo=bar, gabu=zo - .properties(foo -> "bar", gabu -> "zo") // property names are deduced from lambda parameter name .impl(ServiceProviderImpl.class) - .withService(LogService.class, srv -> srv.onAdd(ServiceProviderImpl::bind))); + .provides(ServiceProvider.class) + .properties(foo -> "bar", gabu -> "zo") // property names are deduced from lambda parameter names + .start(ServiceProviderImpl::activate) + .withService(LogService.class, srv -> srv.cb(ADD, ServiceProviderImpl::bind))); + // service can also be injected using reflection: + // .withService(LogService.class, srv -> srv.cb("bind"))); component(comp -> comp .impl(ServiceConsumer.class) .withService(LogService.class) .withService(ServiceProvider.class, srv -> srv.filter("(foo=bar)")) - .withConfiguration(conf -> conf.pid(ServiceConsumer.class).onUpdate(ServiceConsumer::updated))); + .withConfiguration(conf -> conf.pid(ServiceConsumer.class).cb(ServiceConsumer::updated))); component(comp -> comp.impl(Configurator.class).withService(ConfigurationAdmin.class)); } Modified: felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/rx/completable/Activator.java URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/rx/completable/Activator.java?rev=1724333&r1=1724332&r2=1724333&view=diff ============================================================================== --- felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/rx/completable/Activator.java (original) +++ felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/rx/completable/Activator.java Tue Jan 12 22:45:36 2016 @@ -19,6 +19,7 @@ package org.apache.felix.dependencymanager.lambda.samples.rx.completable; import static java.lang.System.out; +import static org.apache.felix.dm.builder.lambda.CB.ADD; import org.apache.felix.dm.builder.lambda.DependencyActivatorBase; import org.osgi.service.log.LogService; @@ -40,14 +41,15 @@ public class Activator extends Dependenc out.println("type \"log info\" to see the logs emitted by this test."); // Define the FelixSiteInfo component that provides some informations about the Felix web site. + // (see the SiteInfoImpl::init method, which asynchronously download Felix). component(comp -> comp .provides(SiteInfo.class) .factory(() -> new SiteInfoImpl("http://felix.apache.org/")) - .withService(LogService.class, srv -> srv.onAdd(SiteInfoImpl::bind))); + .withService(LogService.class, srv -> srv.cb(ADD, SiteInfoImpl::bind))); // Define the FelixSite component that depends on the FelixSiteInfo service component(comp -> comp - .impl(DisplaySite.class) - .withService(LogService.class).withService(SiteInfo.class)); + .impl(DisplaySite.class) + .withService(LogService.class, SiteInfo.class)); } } Modified: felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/rx/completable/SiteInfoImpl.java URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/rx/completable/SiteInfoImpl.java?rev=1724333&r1=1724332&r2=1724333&view=diff ============================================================================== --- felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/rx/completable/SiteInfoImpl.java (original) +++ felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/rx/completable/SiteInfoImpl.java Tue Jan 12 22:45:36 2016 @@ -44,11 +44,8 @@ public class SiteInfoImpl implements Sit // that is used as a Dependency Manager "Future" dependency, in order to block the activation of // our component until the page has been downloaded and parsed. - Observable<List<String>> links = downloadSite(m_url) - .map(this::getSiteLinks); - - component(c, comp -> comp - .withFuture(toFuture(links), futuredep -> futuredep.thenAccept(this::setLinks))); + Observable<List<String>> links = downloadSite(m_url).map(this::getSiteLinks); + component(c, comp -> comp.withFuture(toFuture(links), futuredep -> futuredep.cb(this::setLinks))); } // Called when our future has completed. Modified: felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/rx/observable/ObservableComponent.java URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/rx/observable/ObservableComponent.java?rev=1724333&r1=1724332&r2=1724333&view=diff ============================================================================== --- felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/rx/observable/ObservableComponent.java (original) +++ felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda.samples/src/org/apache/felix/dependencymanager/lambda/samples/rx/observable/ObservableComponent.java Tue Jan 12 22:45:36 2016 @@ -79,7 +79,7 @@ public class ObservableComponent { publisher.onSubscribe(EmptySubscription.INSTANCE); component(m_dm, builder -> builder .factory(() -> new DocumentViewer(title)) - .onStart(publisher::onNext)); + .start(publisher::onNext)); }); } } Modified: felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/BundleDependencyBuilder.java URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/BundleDependencyBuilder.java?rev=1724333&r1=1724332&r2=1724333&view=diff ============================================================================== --- felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/BundleDependencyBuilder.java (original) +++ felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/BundleDependencyBuilder.java Tue Jan 12 22:45:36 2016 @@ -4,220 +4,49 @@ import java.util.Dictionary; import java.util.function.Supplier; import org.apache.felix.dm.BundleDependency; -import org.apache.felix.dm.Component; -import org.apache.felix.dm.builder.lambda.Functions.Consumer; -import org.apache.felix.dm.builder.lambda.Functions.Consumer2; -import org.apache.felix.dm.builder.lambda.Functions.SerializableLambda; +import org.apache.felix.dm.builder.lambda.Functions.CbBundle; +import org.apache.felix.dm.builder.lambda.Functions.CbComponentBundle; +import org.apache.felix.dm.builder.lambda.Functions.CbTypeBundle; +import org.apache.felix.dm.builder.lambda.Functions.CbTypeComponentBundle; import org.osgi.framework.Bundle; /** - * Defines a dependency on a bundle. + * Builder for a DependencyManager <code>BundleDependency</code>. + * TODO: add javadocs * * @author <a href="mailto:[email protected]">Felix Project Team</a> */ -public interface BundleDependencyBuilder extends DependencyBuilder<BundleDependency> { - /** - * Defines a method reference on a component class method. The callback method takes Bundle parameter. - * @param <I> the type of the component instance on which this method reference is applied - */ - @FunctionalInterface - public interface InstanceBundle<I> extends SerializableLambda { - /** - * Signature of the callback method. - * @param instance the component instance on which the callback has to be called - * @param bundle to inject - */ - void call(I instance, Bundle bundle); - } - - /** - * Defines a method reference on a component instance. The callback method takes Component and a Bundle in parameters. - * @param <I> the type of the component instance on which this method reference is applied - */ - @FunctionalInterface - public interface InstanceComponentBundle<I> extends SerializableLambda { - /** - * Signature of the callback method. - * @param instance the component instance on which the callback has to be called - * @param component the component on which this dependency has been added. - * @param bundle the bundle to inject. - */ - void call(I instance, Component component, Bundle bundle); - } - - /** - * Sets the add callbacks for this dependency. The callback can be used as hooks whenever a dependency is added. - * When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming - * you don't need it in this case. - * - * @param callback the method to call when a bundle was added - * @return the bundle dependency - */ - public BundleDependencyBuilder onAdd(String callback); - - /** - * Sets the change callbacks for this dependency. The callback can be used as hooks whenever a dependency is changed. - * When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming - * you don't need it in this case. - * - * @param callback the method to call when a bundle was changed - * @return the bundle dependency - */ - public BundleDependencyBuilder onChange(String callback); +public interface BundleDependencyBuilder<T> extends DependencyBuilder<BundleDependency> { - /** - * Sets the remove callbacks for this dependency. The callback can be used as hooks whenever a dependency is removed. - * When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming - * you don't need it in this case. - * - * @param callback the method to call when a bundle was removed - * @return the bundle dependency - */ - public BundleDependencyBuilder onRemove(String callback); - - /** - * Sets the callbacks for this dependency. These callbacks can be used as hooks whenever a dependency is added or - * removed. When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming - * you don't need it in this case. - * - * @param added the method to call when a bundle was added - * @param removed the method to call when a bundle was removed - * @return the bundle dependency - */ - public BundleDependencyBuilder callbacks(String added, String removed); + BundleDependencyBuilder<T> cbInst(Object callbackInstance); - /** - * Sets the callbacks for this dependency. These callbacks can be used as hooks whenever a dependency is added, changed or - * removed. When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming - * you don't need it in this case. - * - * @param added the method to call when a bundle was added - * @param changed the method to call when a bundle was changed - * @param removed the method to call when a bundle was removed - * @return the bundle dependency - */ - public BundleDependencyBuilder callbacks(String added, String changed, String removed); - - /** - * Sets the callbacks for this dependency. These callbacks can be used as hooks whenever a dependency is added, changed or - * removed. They are called on the instance you provide. When you specify callbacks, the auto configuration feature is - * automatically turned off, because we're assuming you don't need it in this case. - * - * @param instance the instance to call the callbacks on - * @param added the method to call when a bundle was added - * @param changed the method to call when a bundle was changed - * @param removed the method to call when a bundle was removed - * @return the bundle dependency builder - */ - public BundleDependencyBuilder callbacks(Object instance, String added, String changed, String removed); - - /** - * Sets the add callbacks instance for this dependency. These callbacks can be used as hooks whenever a dependency is added. When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming - * you don't need it in this case. - * - * @param added the method to call when a bundle was added - * @return the bundle dependency - */ - public BundleDependencyBuilder onAdd(Consumer<Bundle> add); - - /** - * Sets the add callbacks instance for this dependency. These callbacks can be used as hooks whenever a dependency is added. When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming - * you don't need it in this case. - * - * @param added the method to call when a bundle was added - * @return the bundle dependency - */ - public <I> BundleDependencyBuilder onAdd(InstanceBundle<I> add); - - /** - * Sets the add callbacks instance for this dependency. These callbacks can be used as hooks whenever a dependency is added. When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming - * you don't need it in this case. - * - * @param added the method to call when a bundle was added. the callback is invoked with the component on which this dependency is added, and the added bundle. - * @return the bundle dependency - */ - public BundleDependencyBuilder onAdd(Consumer2<Component, Bundle> add); - - /** - * Sets the add callbacks instance for this dependency. These callbacks can be used as hooks whenever a dependency is added. When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming - * you don't need it in this case. - * - * @param added the method to call on one of the component instances when a bundle was added. the callback is invoked with the component on which this dependency is added, and the added bundle. - * @return the bundle dependency - */ - public <I> BundleDependencyBuilder onAdd(InstanceComponentBundle<I> add); - - /** - * Sets the change callbacks instance for this dependency. These callbacks can be used as hooks whenever a dependency is changed. When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming - * you don't need it in this case. - * - * @param added the method to call when a bundle was changed - * @return the bundle dependency - */ - public BundleDependencyBuilder onChange(Consumer<Bundle> add); - - /** - * Sets the changed callbacks instance for this dependency. These callbacks can be used as hooks whenever a dependency is changed. When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming - * you don't need it in this case. - * - * @param change the method to call when a bundle was changed - * @return the bundle dependency - */ - public <I> BundleDependencyBuilder onChange(InstanceBundle<I> change); - - /** - * Sets the changed callbacks instance for this dependency. These callbacks can be used as hooks whenever a dependency is changed. When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming - * you don't need it in this case. - * - * @param change the method to call when a bundle was changed. the callback is invoked with the component on which this dependency is added, and the added bundle. - * @return the bundle dependency - */ - public BundleDependencyBuilder onChange(Consumer2<Component, Bundle> change); - - /** - * Sets the changed callbacks instance for this dependency. These callbacks can be used as hooks whenever a dependency is changed. When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming - * you don't need it in this case. - * - * @param change the method to call on one of the component instances when a bundle was changed. the callback is invoked with the component on which this dependency is added, and the added bundle. - * @return the bundle dependency - */ - public <I> BundleDependencyBuilder onChange(InstanceComponentBundle<I> change); + BundleDependencyBuilder<T> cb(String add); + BundleDependencyBuilder<T> cb(String add, String remove); + BundleDependencyBuilder<T> cb(String add, String change, String remove); + + BundleDependencyBuilder<T> cb(CbTypeBundle<T> add); + BundleDependencyBuilder<T> cb(CbTypeBundle<T> add, CbTypeBundle<T> remove); + BundleDependencyBuilder<T> cb(CbTypeBundle<T> add, CbTypeBundle<T> change, CbTypeBundle<T> remove); - /** - * Sets the remove callbacks instance for this dependency. These callbacks can be used as hooks whenever a dependency is removed. When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming - * you don't need it in this case. - * - * @param remove the method to call when a bundle was remove - * @return the bundle dependency - */ - public BundleDependencyBuilder onRemove(Consumer<Bundle> remove); + BundleDependencyBuilder<T> cb(CbTypeComponentBundle<T> add); + BundleDependencyBuilder<T> cb(CbTypeComponentBundle<T> add, CbTypeComponentBundle<T> remove); + BundleDependencyBuilder<T> cb(CbTypeComponentBundle<T> add, CbTypeComponentBundle<T> change, CbTypeComponentBundle<T> remove); - /** - * Sets the remove callbacks instance for this dependency. These callbacks can be used as hooks whenever a dependency is removed. When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming - * you don't need it in this case. - * - * @param remove the method to call when a bundle was removed - * @return the bundle dependency - */ - public <I> BundleDependencyBuilder onRemove(InstanceBundle<I> remove); + BundleDependencyBuilder<T> cb(CbBundle add); + BundleDependencyBuilder<T> cb(CbBundle add, CbBundle remove); + BundleDependencyBuilder<T> cb(CbBundle add, CbBundle change, CbBundle remove); - /** - * Sets the remove callbacks instance for this dependency. These callbacks can be used as hooks whenever a dependency is remove. When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming - * you don't need it in this case. - * - * @param removed the method to call when a bundle was removed. the callback is invoked with the component on which this dependency is removed, and the added bundle. - * @return the bundle dependency - */ - public BundleDependencyBuilder onRemove(Consumer2<Component, Bundle> remove); + BundleDependencyBuilder<T> cb(CbComponentBundle add); + BundleDependencyBuilder<T> cb(CbComponentBundle add, CbComponentBundle remove); + BundleDependencyBuilder<T> cb(CbComponentBundle add, CbComponentBundle change, CbComponentBundle remove); - /** - * Sets the remove callbacks instance for this dependency. These callbacks can be used as hooks whenever a dependency is removed. When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming - * you don't need it in this case. - * - * @param remove the method to call on one of the component instances when a bundle was removed. the callback is invoked with the component on which this dependency is added, and the added bundle. - * @return the bundle dependency - */ - public <I> BundleDependencyBuilder onRemove(InstanceComponentBundle<I> remove); + <U> BundleDependencyBuilder<T> compositeCb(CbTypeBundle<U> add); + <U> BundleDependencyBuilder<T> compositeCb(CbTypeBundle<U> add, CbTypeBundle<U> remove); + <U> BundleDependencyBuilder<T> compositeCb(CbTypeBundle<U> add, CbTypeBundle<U> change, CbTypeBundle<U> remove); + + <U> BundleDependencyBuilder<T> compositeCb(CbTypeComponentBundle<U> add); + <U> BundleDependencyBuilder<T> compositeCb(CbTypeComponentBundle<U> add, CbTypeComponentBundle<U> remove); + <U> BundleDependencyBuilder<T> compositeCb(CbTypeComponentBundle<U> add, CbTypeComponentBundle<U> change, CbTypeComponentBundle<U> remove); /** * Enables auto configuration for this dependency. This means the component implementation (composition) will be @@ -226,7 +55,7 @@ public interface BundleDependencyBuilder * @param autoConfig <code>true</code> to enable auto configuration * @return the bundle dependency builder */ - public BundleDependencyBuilder autoConfig(boolean autoConfig); + public BundleDependencyBuilder<T> autoConfig(boolean autoConfig); /** * Enables auto configuration for this dependency. This means the component implementation (composition) will be @@ -234,7 +63,7 @@ public interface BundleDependencyBuilder * * @return the bundle dependency builder */ - public BundleDependencyBuilder autoConfig(); + public BundleDependencyBuilder<T> autoConfig(); /** * Sets the dependency to be required. @@ -242,7 +71,7 @@ public interface BundleDependencyBuilder * @param required <code>true</code> if this bundle dependency is required * @return the bundle dependency builder */ - public BundleDependencyBuilder required(boolean required); + public BundleDependencyBuilder<T> required(boolean required); /** * Sets the dependency to be required. @@ -250,7 +79,7 @@ public interface BundleDependencyBuilder * @param required <code>true</code> if this bundle dependency is required * @return the bundle dependency builder */ - public BundleDependencyBuilder required(); + public BundleDependencyBuilder<T> required(); /** * Sets the bundle to depend on directly. @@ -258,7 +87,7 @@ public interface BundleDependencyBuilder * @param bundle the bundle to depend on * @return the bundle dependency builder */ - public BundleDependencyBuilder bundle(Bundle bundle); + public BundleDependencyBuilder<T> bundle(Bundle bundle); /** * Sets the filter condition to depend on. Filters are matched against the full manifest of a bundle. @@ -267,7 +96,7 @@ public interface BundleDependencyBuilder * @return the bundle dependency builder * @throws IllegalArgumentException if the filter is invalid */ - public BundleDependencyBuilder filter(String filter) throws IllegalArgumentException; + public BundleDependencyBuilder<T> filter(String filter) throws IllegalArgumentException; /** * Sets the bundle state mask to depend on. The OSGi BundleTracker explains this mask in more detail, but @@ -276,7 +105,7 @@ public interface BundleDependencyBuilder * @param mask the mask to use * @return the bundle dependency builder */ - public BundleDependencyBuilder mask(int mask); + public BundleDependencyBuilder<T> mask(int mask); /** * Sets property propagation. If set to <code>true</code> any bundle manifest properties will be added @@ -285,7 +114,7 @@ public interface BundleDependencyBuilder * @param propagate <code>true</code> to propagate the bundle manifest properties * @return the bundle dependency builder */ - public BundleDependencyBuilder propagate(boolean propagate); + public BundleDependencyBuilder<T> propagate(boolean propagate); /** * Sets property propagation. any bundle manifest properties will be added @@ -293,7 +122,7 @@ public interface BundleDependencyBuilder * * @return the bundle dependency builder */ - public BundleDependencyBuilder propagate(); + public BundleDependencyBuilder<T> propagate(); /** * Sets an Object instance and a callback method used to propagate some properties to the provided service properties. @@ -305,7 +134,7 @@ public interface BundleDependencyBuilder * @param method the method to invoke for retrieving the properties to be propagated to the service properties. * @return this service dependency. builder */ - public BundleDependencyBuilder propagate(Object instance, String method); + public BundleDependencyBuilder<T> propagate(Object instance, String method); /** * Sets an Object instance and a callback method used to propagate some properties to the provided service properties. @@ -316,5 +145,6 @@ public interface BundleDependencyBuilder * @param instance the Object instance which is used to retrieve propagated service properties * @return this service dependency. builder */ - public BundleDependencyBuilder propagate(Supplier<Dictionary<?, ?>> instance); + public BundleDependencyBuilder<T> propagate(Supplier<Dictionary<?, ?>> instance); + } Added: felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/CB.java URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/CB.java?rev=1724333&view=auto ============================================================================== --- felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/CB.java (added) +++ felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/CB.java Tue Jan 12 22:45:36 2016 @@ -0,0 +1,21 @@ +package org.apache.felix.dm.builder.lambda; + +/** + * Defines various types of dependency callbacks. + */ +public enum CB { + /** + * Service is added. + */ + ADD, + + /** + * Service properties updated + */ + CHG, + + /** + * Service removed. + */ + REM +} Modified: felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/ComponentBuilder.java URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/ComponentBuilder.java?rev=1724333&r1=1724332&r2=1724333&view=diff ============================================================================== --- felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/ComponentBuilder.java (original) +++ felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/ComponentBuilder.java Tue Jan 12 22:45:36 2016 @@ -1,14 +1,8 @@ package org.apache.felix.dm.builder.lambda; -import java.util.Dictionary; -import java.util.concurrent.CompletableFuture; import java.util.function.Function; import java.util.function.Supplier; -import org.apache.felix.dm.Component; -import org.apache.felix.dm.builder.lambda.Functions.Consumer; -import org.apache.felix.dm.builder.lambda.Functions.FluentProperties; - /** * Builds a DependencyManager Component using java8 constructs. This interface provides builder methods used to configure general Component * parameters, like:<p> @@ -28,10 +22,10 @@ import org.apache.felix.dm.builder.lambd * public class Activator extends DependencyActivatorBase { * public void init() throws Exception { * component(comp -> comp - * .provides(Service.class) * .factory(ServiceImplFactory::new, ServiceImplFactory::create) + * .provides(Service.class) * .withService(LogService.class, srv -> srv.required().onAdd(ServiceImpl::set)) - * .onStart(ServiceImpl::activate)); + * .start(ServiceImpl::activate)); * } * } * }</pre> @@ -41,29 +35,27 @@ import org.apache.felix.dm.builder.lambd * * TODO: add support for reflection for lifecycle callbacks (like in Dependency Manager). */ -public interface ComponentBuilder<B extends ComponentBuilder<B>> { +public interface ComponentBuilder<T> extends ComponentBuilderBase<T, ComponentBuilder<T>> { /** - * Configures the services registered by this component in the OSGgi registry - * @param ifaces the OSGi service(s) components. + * Configures the component implementation. Can be a class name, or a component implementation object. + * @param impl the component implementation (a class, or an Object). * @return this builder */ - B provides(Class<?> ... ifaces); + <U> ComponentBuilder<U> impl(U impl); /** - * Configures the component implementation. Can be a classname, or a component implementation object. - * @param impl the component implementation (a class, or an Object). - * @return this builder + * TODO */ - B impl(Object impl); - + <U> ComponentBuilder<U> impl(Class<U> implClass); + /** - * Configures a factory that can be used to create this component implemention. + * Configures a factory that can be used to create this component implementation. * Example: "factory(ComponentImpl::new)", or "factory(() -> new ComponentImpl())". * - * @param create the factory used to create the component implemenation. + * @param create the factory used to create the component implementation. * @return this builder */ - <T> B factory(Supplier<T> create); + <U> ComponentBuilder<U> factory(Supplier<U> create); /** * Configures a factory used to create this component implementation using a Factory object and a method in the Factory object. @@ -75,8 +67,8 @@ public interface ComponentBuilder<B exte * @param create the method reference on the Factory method that is used to create the Component implementation * @return this builder */ - <T> B factory(Supplier<T> factory, Function<T, Object> create); - + <U, V> ComponentBuilder<V> factory(Supplier<U> factory, Function<U, V> create); + /** * Configures a factory used to create this component implementation using a Factory object and a "getComponent" factory method. * the Factory method may then return multiple objects that will be part of this component implementation. @@ -91,8 +83,8 @@ public interface ComponentBuilder<B exte * @param getComposition * @return this builder */ - <T> B factory(Supplier<T> factory, Supplier<Object[]> getComposition); - + <U> ComponentBuilder<U> factory(Supplier<U> factory, Supplier<Object[]> getComposition); + /** * Configures a factory that also returns a composition of objects for this component implemenation. * @@ -108,221 +100,5 @@ public interface ComponentBuilder<B exte * @param getComposition the Factory method used to return the list of objects that are also part of the component implementation. * @return this builder */ - <T> B factory(Supplier<T> factory, Function<T, Object> create, Function<T, Object[]> getComposition); - - /** - * Sets the component's service properties - * @param properties the component's service properties - * @return this builder - */ - B properties(Dictionary<?,?> properties); - - /** - * Sets the components's service properties using varargs. The number of parameters must be even, representing a list of pair property key-value. - * @param properties a varargs representing a list of key-value pairs. - * - * Example: properties("param1", "value1", "service.ranking", 3) - * @return this builder - */ - B properties(String name, Object value, Object ... rest); - - /** - * Sets the components's service properties using List of lamda properties. - * When you use this method, you must compile your source code using the "-parameters" option, and the "arg0" parameter - * name is now allowed. - * - * Example: properties(param1 -> "value1, param2 -> 2); - * @return this builder - */ - B properties(FluentProperties ... properties); - - /** - * Adds a required/autoconfig service dependency. - * @param service the dependency that is required and that will be injected in any field with the same dependency type. - * @return this builder - */ - <T> B withService(Class<T> service); - - /** - * Adds a service dependency. - * @param service the service - * @param consumer the lambda for building the service dependency - * @return this builder. - */ - <T> B withService(Class<T> service, Consumer<ServiceDependencyBuilder<T>> consumer); - - /** - * Adds a configuration dependency. - * @param consumer the lambda used to build the configuration dependency. - * @return this builder. - */ - B withConfiguration(Consumer<ConfigurationDependencyBuilder> consumer); - - /** - * Adds a bundle dependency. - * @param consumer the lambda used to build the bundle dependency. - * @return this builder. - */ - B withBundle(Consumer<BundleDependencyBuilder> consumer); - - /** - * TODO - * @param future - * @return - */ - <T> B withFuture(CompletableFuture<T> future, Consumer<FutureDependencyBuilder<T>> consumer); - - /** - * TODO comment - * - * @param callback - * @return - */ - B onInit(Runnable callback); - - /** - * Sets a reference on an instance method that is called when the component is initialized. - * - * Example: onInit(instance::init) - * - * @param callback a callback that will be called when the component is initialized - * @return this builder - */ - <T> B onInit(Functions.Consumer<T> callback); - - /** - * Sets a reference on a component implementation method that is called when the component is initialized. - * - * Example: onInit(MyComponentImpl::activate) - * - * @param callback a method reference called when the component is started - * @return this builder - */ - <T> B onInit(Functions.Consumer2<T, Component> callback); - - /** - * TODO comment - * - * @param callback - * @return - */ - B onStart(Runnable callback); - - /** - * Sets a reference on an instance method that is called when the component is started. - * - * Example: onStart(instance::start) - * - * @param callback a callback that will be called when the component is started - * @return this builder - */ - <T> B onStart(Functions.Consumer<T> callback); - - /** - * Sets a reference on a component implementation method that is called when the component is started. - * - * Example: onStart(MyComponentImpl::started) - * - * @param callback a method reference called when the component is started - * @return this builder - */ - <T> B onStart(Functions.Consumer2<T, Component> callback); - - /** - * TODO comment - * - * @param callback - * @return - */ - B onStop(Runnable callback); - - /** - * Sets a reference on an instance method that is called when the component is stopped. - * - * Example: onStop(instance::stop) - * - * @param callback a callback that will be called when the component is stopped - * @return this builder - */ - <T> B onStop(Functions.Consumer<T> callback); - - /** - * Sets a reference on a component implementation method that is called when the component is started. - * - * Example: onStart(MyComponentImpl::started) - * - * @param callback a method reference called when the component is started - * @return this builder - */ - <T> B onStop(Functions.Consumer2<T, Component> callback); - - /** - * TODO comment - * - * @param callback - * @return - */ - B onDestroy(Runnable callback); - - /** - * Sets a reference on an instance method that is called when the component is destroyed. - * - * Example: onDestroy(instance::destroy) - * - * @param callback a callback that will be called when the component is destroyed - * @return this builder - */ - <T> B onDestroy(Functions.Consumer<T> callback); - - /** - * Sets a reference on a component implementation method that is called when the component is destroyed. - * - * Example: onDestroy(MyComponentImpl::destroy) - * - * @param callback a method reference called when the component is destroyed - * @return this builder - */ - <T> B onDestroy(Functions.Consumer2<T, Component> callback); - - /** - * Configures OSGi object (BundleContext, Component, etc ...) that will be injected in any field having the same OSGi object type. - * @param clazz the OSGi object type (BundleContext, Component, DependencyManager). - * @param autoConfig true if the OSGi object has to be injected, false if not - * @return this builder - */ - <T> B autoInject(Class<T> clazz, boolean autoConfig); - - /** - * Configures OSGi object (BundleContext, Component, etc ...) that will be injected in a given field. - * @param clazz the OSGi object type (BundleContext, Component, DependencyManager). - * @param field the field that will be injected with the OSGI object - * @return this builder - */ - <T> B autoInject(Class<T> clazz, String field); - - /** - * Activates debug mode - * @param label the debug label - * @return this builder - */ - B debug(String label); - - /** - * Automatically adds this component to its DependencyManager object - * @param autoAdd true for automatically adding this component to the DependencyManager object, false if not - * @return this builder - */ - B autoAdd(boolean autoAdd); - - /** - * Is this component automatically added to its DependencyManager object ? - * @return this builder - */ - boolean autoAdd(); - - /** - * Builds the real DependencyManager Component from this Component. - * @return the real DependencyManager Component. - */ - Component build(); + <U, V> ComponentBuilder<V> factory(Supplier<U> factory, Function<U, V> create, Function<U, Object[]> getComposition); } Added: felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/ComponentBuilderBase.java URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/ComponentBuilderBase.java?rev=1724333&view=auto ============================================================================== --- felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/ComponentBuilderBase.java (added) +++ felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/ComponentBuilderBase.java Tue Jan 12 22:45:36 2016 @@ -0,0 +1,164 @@ +package org.apache.felix.dm.builder.lambda; + +import java.util.Dictionary; +import java.util.concurrent.CompletableFuture; +import java.util.function.BiConsumer; +import java.util.function.Consumer; + +import org.apache.felix.dm.Component; +import org.apache.felix.dm.builder.lambda.Functions.CbComponent; +import org.apache.felix.dm.builder.lambda.Functions.FluentProperties; + +/** + * Base class for all kinds of component builders. + * + * @param <T> the type of the component implementation class + * @param <B> the builder that extends this class. + */ +public interface ComponentBuilderBase<T, B extends ComponentBuilderBase<T, B>> { + /** + * Configures the services registered by this component in the OSGgi registry + * @param ifaces the OSGi service(s) components. + * @return this builder + */ + B provides(Class<?> ... ifaces); + + /** + * Configures the services registered by this component in the OSGgi registry + * @param ifaces the OSGi service(s) components. + * @return this builder + */ + B provides(String ... ifaces); + + /** + * Sets the component's service properties + * @param properties the component's service properties + * @return this builder + */ + B properties(Dictionary<?,?> properties); + + /** + * Sets the components's service properties using varargs. The number of parameters must be even, representing a list of pair property key-value. + * @param properties a varargs representing a list of key-value pairs. + * + * Example: properties("param1", "value1", "service.ranking", 3) + * @return this builder + */ + B properties(String name, Object value, Object ... rest); + + /** + * Sets the components's service properties using List of lamda properties. + * When you use this method, you must compile your source code using the "-parameters" option, and the "arg0" parameter + * name is now allowed. + * + * Example: properties(param1 -> "value1, param2 -> 2); + * @return this builder + */ + B properties(FluentProperties ... properties); + + /** + * Adds a required/autoconfig service dependency. + * @param service the dependency that is required and that will be injected in any field with the same dependency type. + * @return this builder + */ + B withService(Class<?> service, Class<?> ... services); + + /** + * Adds a service dependency. + * @param service the service + * @param consumer the lambda for building the service dependency + * @return this builder. + */ + <U> B withService(Class<U> service, Consumer<ServiceDependencyBuilder<U>> consumer); + + /** + * Adds a configuration dependency. + * @param consumer the lambda used to build the configuration dependency. + * @return this builder. + */ + B withConfiguration(Consumer<ConfigurationDependencyBuilder<T>> consumer); + + /** + * Adds a bundle dependency. + * @param consumer the lambda used to build the bundle dependency. + * @return this builder. + */ + B withBundle(Consumer<BundleDependencyBuilder<T>> consumer); + + /** + * TODO + * @param future + * @return + */ + <V> B withFuture(CompletableFuture<V> future, Consumer<FutureDependencyBuilder<T, V>> consumer); + + B init(Consumer<T> callback); + B init(BiConsumer<T, Component> callback); + <U> B init(Class<U> type, Consumer<U> callback); + <U> B init(Class<U> type, BiConsumer<U, Component> callback); + B init(Runnable callback); + B init(CbComponent callback); + + B start(Consumer<T> callback); + B start(BiConsumer<T, Component> callback); + <U> B start(Class<U> type, Consumer<U> callback); + <U> B start(Class<U> type, BiConsumer<U, Component> callback); + B start(Runnable callback); + B start(CbComponent callback); + + B stop(Consumer<T> callback); + B stop(BiConsumer<T, Component> callback); + <U> B stop(Class<U> type, Consumer<U> callback); + <U> B stop(Class<U> type, BiConsumer<U, Component> callback); + B stop(Runnable callback); + B stop(CbComponent callback); + + B destroy(Consumer<T> callback); + B destroy(BiConsumer<T, Component> callback); + <U> B destroy(Class<U> type, Consumer<U> callback); + <U> B destroy(Class<U> type, BiConsumer<U, Component> callback); + B destroy(Runnable callback); + B destroy(CbComponent callback); + + /** + * Configures OSGi object (BundleContext, Component, etc ...) that will be injected in any field having the same OSGi object type. + * @param clazz the OSGi object type (BundleContext, Component, DependencyManager). + * @param autoConfig true if the OSGi object has to be injected, false if not + * @return this builder + */ + <U> B autoInject(Class<U> clazz, boolean autoConfig); + + /** + * Configures OSGi object (BundleContext, Component, etc ...) that will be injected in a given field. + * @param clazz the OSGi object type (BundleContext, Component, DependencyManager). + * @param field the field that will be injected with the OSGI object + * @return this builder + */ + <U> B autoInject(Class<U> clazz, String field); + + /** + * Activates debug mode + * @param label the debug label + * @return this builder + */ + B debug(String label); + + /** + * Automatically adds this component to its DependencyManager object + * @param autoAdd true for automatically adding this component to the DependencyManager object, false if not + * @return this builder + */ + B autoAdd(boolean autoAdd); + + /** + * Is this component automatically added to its DependencyManager object ? + * @return this builder + */ + B autoAdd(); + + /** + * Builds the real DependencyManager Component from this Component. + * @return the real DependencyManager Component. + */ + Component build(); +} Modified: felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/ConfigurationDependencyBuilder.java URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/ConfigurationDependencyBuilder.java?rev=1724333&r1=1724332&r2=1724333&view=diff ============================================================================== --- felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/ConfigurationDependencyBuilder.java (original) +++ felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/ConfigurationDependencyBuilder.java Tue Jan 12 22:45:36 2016 @@ -1,10 +1,10 @@ package org.apache.felix.dm.builder.lambda; -import static org.apache.felix.dm.builder.lambda.Functions.*; - -import java.util.Dictionary; - import org.apache.felix.dm.ConfigurationDependency; +import org.apache.felix.dm.builder.lambda.Functions.CbComponentDictionary; +import org.apache.felix.dm.builder.lambda.Functions.CbDictionary; +import org.apache.felix.dm.builder.lambda.Functions.CbTypeComponentDictionary; +import org.apache.felix.dm.builder.lambda.Functions.CbTypeDictionary; /** * Defines a builder for DependencyManager Configuration Dependency. @@ -22,11 +22,20 @@ import org.apache.felix.dm.Configuration * * TODO: javadoc */ -public interface ConfigurationDependencyBuilder extends DependencyBuilder<ConfigurationDependency> { - ConfigurationDependencyBuilder pid(String pid); - ConfigurationDependencyBuilder pid(Class<?> pidClass); - ConfigurationDependencyBuilder propagate(); - ConfigurationDependencyBuilder propagate(boolean propagate); - <T> ConfigurationDependencyBuilder onUpdate(Consumer2<T, Dictionary<String, Object>> updated); - ConfigurationDependencyBuilder onUpdate(Consumer<Dictionary<String, Object>> updated); +public interface ConfigurationDependencyBuilder<T> extends DependencyBuilder<ConfigurationDependency> { + + ConfigurationDependencyBuilder<T> pid(String pid); + ConfigurationDependencyBuilder<T> pid(Class<?> pidClass); + ConfigurationDependencyBuilder<T> propagate(); + ConfigurationDependencyBuilder<T> propagate(boolean propagate); + + ConfigurationDependencyBuilder<T> cb(String updateMethod); + ConfigurationDependencyBuilder<T> cb(Object callbackInstance, String updateMethod); + + ConfigurationDependencyBuilder<T> cb(CbTypeDictionary<T> callback); + ConfigurationDependencyBuilder<T> cb(CbDictionary updated); + ConfigurationDependencyBuilder<T> cb(CbComponentDictionary updated); + + <U> ConfigurationDependencyBuilder<T> compositeCb(CbTypeDictionary<U> callback); + <U> ConfigurationDependencyBuilder<T> compositeCb(CbTypeComponentDictionary<U> callback); } Modified: felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/DependencyActivatorBase.java URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/DependencyActivatorBase.java?rev=1724333&r1=1724332&r2=1724333&view=diff ============================================================================== --- felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/DependencyActivatorBase.java (original) +++ felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/DependencyActivatorBase.java Tue Jan 12 22:45:36 2016 @@ -4,10 +4,10 @@ import java.util.function.Consumer; import org.apache.felix.dm.Component; import org.apache.felix.dm.DependencyManager; -import org.apache.felix.dm.builder.lambda.impl.AdapterBuilderImpl; -import org.apache.felix.dm.builder.lambda.impl.AspectBuilderImpl; import org.apache.felix.dm.builder.lambda.impl.ComponentBuilderImpl; import org.apache.felix.dm.builder.lambda.impl.FactoryPidAdapterBuilderImpl; +import org.apache.felix.dm.builder.lambda.impl.ServiceAdapterBuilderImpl; +import org.apache.felix.dm.builder.lambda.impl.ServiceAspectBuilderImpl; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; @@ -79,7 +79,7 @@ public abstract class DependencyActivato * The component is auto-added to the DependencyManager, unless the lambda calls the ComponentBuilder.autoAdd(false) method. * @return a newly built DM component. */ - protected Component component(Consumer<ComponentBuilder<? extends ComponentBuilder<?>>> consumer) { + protected <T> Component component(Consumer<ComponentBuilder<T>> consumer) { return component(m_manager, consumer); } @@ -89,7 +89,7 @@ public abstract class DependencyActivato * The component is auto-added to the DependencyManager, unless the lambda calls the AspectBuilder.autoAdd(false) method. * @return a newly built DM component. */ - protected <T> Component aspect(Class<T> aspect, Consumer<AspectBuilder<T>> consumer) { + protected <T> Component aspect(Class<T> aspect, Consumer<ServiceAspectBuilder<T, ?>> consumer) { return aspect(m_manager, aspect, consumer); } @@ -99,7 +99,7 @@ public abstract class DependencyActivato * The component is auto-added to the DependencyManager, unless the lambda calls the AdapterBuilder.autoAdd(false) method. * @return a newly built DM component. */ - protected <T> Component adapter(Class<T> adaptee, Consumer<AdapterBuilder<T>> consumer) { + protected <T> Component adapter(Class<T> adaptee, Consumer<ServiceAdapterBuilder<T, ?>> consumer) { return adapter(m_manager, adaptee, consumer); } @@ -109,7 +109,7 @@ public abstract class DependencyActivato * The component is auto-added to the DependencyManager, unless the lambda calls the FactoryPidAdapterBuilder.autoAdd(false) method. * @return a newly built DM component. */ - protected Component factoryPidAdapter(Consumer<FactoryPidAdapterBuilder> consumer) { + protected <T> Component factoryPidAdapter(Consumer<FactoryPidAdapterBuilder<T>> consumer) { return factoryPidAdapter(m_manager, consumer); } @@ -123,11 +123,11 @@ public abstract class DependencyActivato * * @return the built DM component. */ - public static Component component(DependencyManager dm, Consumer<ComponentBuilder<? extends ComponentBuilder<?>>> consumer) { - ComponentBuilder<?> componentBuilder = new ComponentBuilderImpl(dm); + public static <T> Component component(DependencyManager dm, Consumer<ComponentBuilder<T>> consumer) { + ComponentBuilder<T> componentBuilder = new ComponentBuilderImpl<>(dm); consumer.accept(componentBuilder); Component comp = componentBuilder.build(); - if (componentBuilder.autoAdd()) { + if (((ComponentBuilderImpl<T>) componentBuilder).isAutoAdd()) { dm.add(comp); } return comp; @@ -138,8 +138,8 @@ public abstract class DependencyActivato * @param comp an existing DM component * @param consumer the lambda that will be used to update the component */ - public static void component(Component comp, Consumer<ComponentBuilder<?>> consumer) { - ComponentBuilder<?> componentBuilder = new ComponentBuilderImpl(comp, true /* update component */); + public static <T> void component(Component comp, Consumer<ComponentBuilder<T>> consumer) { + ComponentBuilder<T> componentBuilder = new ComponentBuilderImpl<>(comp, true /* update component */); consumer.accept(componentBuilder); componentBuilder.build(); } @@ -152,11 +152,11 @@ public abstract class DependencyActivato * @return a new DM aspect component. The aspect component is auto-added into the dm object, unless the lambda calls * the AspectBuilder.autoAdd(false) method. */ - public static <T> Component aspect(DependencyManager dm, Class<T> aspect, Consumer<AspectBuilder<T>> consumer) { - AspectBuilder<T> aspectBuilder = new AspectBuilderImpl<>(dm, aspect); + public static <T> Component aspect(DependencyManager dm, Class<T> aspect, Consumer<ServiceAspectBuilder<T, ?>> consumer) { + ServiceAspectBuilderImpl<T, ?> aspectBuilder = new ServiceAspectBuilderImpl<>(dm, aspect); consumer.accept(aspectBuilder); Component comp = aspectBuilder.build(); - if (aspectBuilder.autoAdd()) { + if (aspectBuilder.isAutoAdd()) { dm.add(comp); } return comp; @@ -170,11 +170,11 @@ public abstract class DependencyActivato * @return a new DM adapter component. The adapter component is auto-added into the dm object, unless the lambda calls * the AspectBuilder.autoAdd(false) method is called. */ - public static <T> Component adapter(DependencyManager dm, Class<T> adaptee, Consumer<AdapterBuilder<T>> consumer) { - AdapterBuilder<T> adapterBuilder = new AdapterBuilderImpl<>(dm, adaptee); + public static <T> Component adapter(DependencyManager dm, Class<T> adaptee, Consumer<ServiceAdapterBuilder<T, ?>> consumer) { + ServiceAdapterBuilderImpl<T, ?> adapterBuilder = new ServiceAdapterBuilderImpl<>(dm, adaptee); consumer.accept(adapterBuilder); Component comp = adapterBuilder.build(); - if (adapterBuilder.autoAdd()) { + if (adapterBuilder.isAutoAdd()) { dm.add(comp); } return comp; @@ -187,11 +187,11 @@ public abstract class DependencyActivato * @return a new DM factory configuration adapter component. The adapter component is auto-added into the dm object, unless the lambda calls * the FactoryPidAdapterBuilder.autoAdd(false) method is called */ - public static Component factoryPidAdapter(DependencyManager dm, Consumer<FactoryPidAdapterBuilder> consumer) { - FactoryPidAdapterBuilder factoryPidAdapter = new FactoryPidAdapterBuilderImpl(dm); + public static <T> Component factoryPidAdapter(DependencyManager dm, Consumer<FactoryPidAdapterBuilder<T>> consumer) { + FactoryPidAdapterBuilderImpl<T> factoryPidAdapter = new FactoryPidAdapterBuilderImpl<>(dm); consumer.accept(factoryPidAdapter); Component comp = factoryPidAdapter.build(); - if (factoryPidAdapter.autoAdd()) { + if (factoryPidAdapter.isAutoAdd()) { dm.add(comp); } return comp; Modified: felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/FactoryPidAdapterBuilder.java URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/FactoryPidAdapterBuilder.java?rev=1724333&r1=1724332&r2=1724333&view=diff ============================================================================== --- felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/FactoryPidAdapterBuilder.java (original) +++ felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/FactoryPidAdapterBuilder.java Tue Jan 12 22:45:36 2016 @@ -1,8 +1,12 @@ package org.apache.felix.dm.builder.lambda; -import java.util.Dictionary; +import java.util.function.Function; +import java.util.function.Supplier; -import org.apache.felix.dm.builder.lambda.Functions.Consumer2; +import org.apache.felix.dm.builder.lambda.Functions.CbComponentDictionary; +import org.apache.felix.dm.builder.lambda.Functions.CbDictionary; +import org.apache.felix.dm.builder.lambda.Functions.CbTypeComponentDictionary; +import org.apache.felix.dm.builder.lambda.Functions.CbTypeDictionary; /** * Defines the interface for a Dependency Manager Factory Configuration Adapter. @@ -21,10 +25,86 @@ import org.apache.felix.dm.builder.lambd * * TODO: javadoc */ -public interface FactoryPidAdapterBuilder extends ComponentBuilder<FactoryPidAdapterBuilder> { - FactoryPidAdapterBuilder factoryPid(String pid); - FactoryPidAdapterBuilder factoryPid(Class<?> pidClass); - FactoryPidAdapterBuilder propagate(); - FactoryPidAdapterBuilder propagate(boolean propagate); - <T> FactoryPidAdapterBuilder onUpdate(Consumer2<T, Dictionary<String, Object>> callback); +public interface FactoryPidAdapterBuilder<T> extends ComponentBuilderBase<T, FactoryPidAdapterBuilder<T>> { + /** + * Configures the component implementation. Can be a class name, or a component implementation object. + * @param impl the component implementation (a class, or an Object). + * @return this builder + */ + <U> FactoryPidAdapterBuilder<U> impl(U impl); + + /** + * TODO + */ + <U> FactoryPidAdapterBuilder<U> impl(Class<U> implClass); + + /** + * Configures a factory that can be used to create this component implementation. + * Example: "factory(ComponentImpl::new)", or "factory(() -> new ComponentImpl())". + * + * @param create the factory used to create the component implementation. + * @return this builder + */ + <U> FactoryPidAdapterBuilder<U> factory(Supplier<U> create); + + /** + * Configures a factory used to create this component implementation using a Factory object and a method in the Factory object. + * Example: + * + * factory(Factory::new, Factory::create) + * + * @param factory the function used to create the Factory itself + * @param create the method reference on the Factory method that is used to create the Component implementation + * @return this builder + */ + <U, V> FactoryPidAdapterBuilder<V> factory(Supplier<U> factory, Function<U, V> create); + + /** + * Configures a factory used to create this component implementation using a Factory object and a "getComponent" factory method. + * the Factory method may then return multiple objects that will be part of this component implementation. + * + * Example: + * + * CompositionManager mngr = new CompositionManager(); + * ... + * factory(mngr::create, mngr::getComposition) + * + * @param factory + * @param getComposition + * @return this builder + */ + <U> FactoryPidAdapterBuilder<U> factory(Supplier<U> factory, Supplier<Object[]> getComposition); + + /** + * Configures a factory that also returns a composition of objects for this component implemenation. + * + * Example: + * + * factory(CompositionManager::new, CompositionManager::create, CompositionManager::getComposition). + * + * Here, the CompositionManager will act as a factory (the create method will return the component implementation object), and the + * CompositionManager.getComposition() method will return all the objects that are also part of the component implementation. + * + * @param factory the function used to create the Factory itself + * @param create the Factory method used to create the main component implementation object + * @param getComposition the Factory method used to return the list of objects that are also part of the component implementation. + * @return this builder + */ + <U, V> FactoryPidAdapterBuilder<V> factory(Supplier<U> factory, Function<U, V> create, Function<U, Object[]> getComposition); + + FactoryPidAdapterBuilder<T> factoryPid(String pid); + FactoryPidAdapterBuilder<T> factoryPid(Class<?> pidClass); + FactoryPidAdapterBuilder<T> propagate(); + FactoryPidAdapterBuilder<T> propagate(boolean propagate); + + FactoryPidAdapterBuilder<T> cb(String updateMethod); + FactoryPidAdapterBuilder<T> cb(Object callbackInstance, String updateMethod); + + FactoryPidAdapterBuilder<T> cb(CbTypeDictionary<T> callback); + FactoryPidAdapterBuilder<T> cb(CbTypeComponentDictionary<T> callback); + FactoryPidAdapterBuilder<T> cb(CbDictionary callback); + FactoryPidAdapterBuilder<T> cb(CbComponentDictionary callback); + + <U> FactoryPidAdapterBuilder<T> compositeCb(CbTypeDictionary<U> callback); + <U> FactoryPidAdapterBuilder<T> compositeCb(CbTypeComponentDictionary<U> callback); }
