Author: pderop
Date: Thu Dec 17 23:46:45 2015
New Revision: 1720702

URL: http://svn.apache.org/viewvc?rev=1720702&view=rev
Log:
- Renamed ServiceDependencyBuilder.propagateTo() -> 
ServiceDependencyBuilder.propagate().
- Started to implement BundleDependency builder.

Added:
    
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/BundleDepenencyBuilder.java
    
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/impl/BundleDependencyBuilderImpl.java
Modified:
    
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/Functions.java
    
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/ServiceDependencyBuilder.java
    
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/impl/ServiceDependencyBuilderImpl.java

Added: 
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/BundleDepenencyBuilder.java
URL: 
http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/BundleDepenencyBuilder.java?rev=1720702&view=auto
==============================================================================
--- 
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/BundleDepenencyBuilder.java
 (added)
+++ 
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/BundleDepenencyBuilder.java
 Thu Dec 17 23:46:45 2015
@@ -0,0 +1,200 @@
+package org.apache.felix.dm.builder.lambda;
+
+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.osgi.framework.Bundle;
+
+/**
+ * Defines a dependency on a bundle.
+ * 
+ * @author <a href="mailto:[email protected]";>Felix Project Team</a>
+ */
+public interface BundleDepenencyBuilder 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 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 BundleDepenencyBuilder 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 BundleDepenencyBuilder 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 BundleDepenencyBuilder 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> BundleDepenencyBuilder 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 BundleDepenencyBuilder 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>  BundleDepenencyBuilder onAdd(InstanceComponentBundle<I> add);
+
+    /**
+     * Enables auto configuration for this dependency. This means the 
component implementation (composition) will be
+     * injected with this bundle dependency automatically.
+     * 
+     * @param autoConfig <code>true</code> to enable auto configuration
+     * @return the bundle dependency builder
+     */
+    public BundleDepenencyBuilder autoConfig(boolean autoConfig);
+
+    /**
+     * Enables auto configuration for this dependency. This means the 
component implementation (composition) will be
+     * injected with this bundle dependency automatically.
+     * 
+     * @return the bundle dependency builder
+     */
+    public BundleDepenencyBuilder autoConfig();
+
+    /**
+     * Sets the dependency to be required.
+     * 
+     * @param required <code>true</code> if this bundle dependency is required
+     * @return the bundle dependency builder
+     */
+    public BundleDepenencyBuilder required(boolean required);
+
+    /**
+     * Sets the dependency to be required.
+     * 
+     * @param required <code>true</code> if this bundle dependency is required
+     * @return the bundle dependency builder
+     */
+    public BundleDepenencyBuilder required();
+
+    /**
+     * Sets the bundle to depend on directly.
+     * 
+     * @param bundle the bundle to depend on
+     * @return the bundle dependency builder
+     */
+    public BundleDepenencyBuilder bundle(Bundle bundle);
+
+    /**
+     * Sets the filter condition to depend on. Filters are matched against the 
full manifest of a bundle.
+     * 
+     * @param filter the filter condition
+     * @return the bundle dependency builder
+     * @throws IllegalArgumentException if the filter is invalid
+     */
+    public BundleDepenencyBuilder filter(String filter) throws 
IllegalArgumentException;
+
+    /**
+     * Sets the bundle state mask to depend on. The OSGi BundleTracker 
explains this mask in more detail, but
+     * it is basically a mask with flags for each potential state a bundle can 
be in.
+     * 
+     * @param mask the mask to use
+     * @return the bundle dependency builder
+     */
+    public BundleDepenencyBuilder mask(int mask);
+
+    /**
+     * Sets property propagation. If set to <code>true</code> any bundle 
manifest properties will be added
+     * to the service properties of the component that has this dependency (if 
it registers as a service).
+     * 
+     * @param propagate <code>true</code> to propagate the bundle manifest 
properties
+     * @return the bundle dependency builder
+     */
+    public BundleDepenencyBuilder propagate(boolean propagate);
+    
+    /**
+     * Sets an Object instance and a callback method used to propagate some 
properties to the provided service properties.
+     * The method will be invoked on the specified object instance and must 
have one of the following signatures:
+     * <ul><li>Dictionary callback(ServiceReference, Object service) 
+     * <li>Dictionary callback(ServiceReference)
+     * </ul>
+     * @param instance the Object instance which is used to retrieve 
propagated service properties 
+     * @param method the method to invoke for retrieving the properties to be 
propagated to the service properties.
+     * @return this service dependency. builder
+     */
+    public BundleDepenencyBuilder propagate(Object instance, String method);
+    
+    /**
+     * Sets an Object instance and a callback method used to propagate some 
properties to the provided service properties.
+     * The method will be invoked on the specified object instance and must 
have one of the following signatures:
+     * <ul><li>Dictionary callback(ServiceReference, Object service) 
+     * <li>Dictionary callback(ServiceReference)
+     * </ul>
+     * @param instance the Object instance which is used to retrieve 
propagated service properties 
+     * @param method the method to invoke for retrieving the properties to be 
propagated to the service properties.
+     * @return this service dependency. builder
+     */
+    public BundleDepenencyBuilder propagate(Supplier<Dictionary<?, ?>> 
instance, String method);
+}

Modified: 
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/Functions.java
URL: 
http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/Functions.java?rev=1720702&r1=1720701&r2=1720702&view=diff
==============================================================================
--- 
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/Functions.java
 (original)
+++ 
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/Functions.java
 Thu Dec 17 23:46:45 2015
@@ -2,7 +2,6 @@ package org.apache.felix.dm.builder.lamb
 
 import java.io.Serializable;
 import java.util.Objects;
-import java.util.function.Function;
 
 /**
  * This class regroups some reuisable functional interfaces. 
@@ -68,5 +67,31 @@ public class Functions {
                };
            }
        }
+       
+       /**
+        * A function that accepts three input arguments and returns no result.
+        */
+       @FunctionalInterface
+       public interface Consumer3<T, U, V> extends SerializableLambda {
+           /**
+            * Performs this operation on the given arguments.
+            * @param t the first input argument
+            * @param u the second input argument
+            * @param v the third input argument
+            */
+           void accept(T t, U u, V v);
+
+           /**
+            * Returns a composed {@code BiConsumer} that performs, in 
sequence, this
+            * operation followed by the {@code after} operation.
+            */
+           default Consumer3<T, U, V> andThen(Consumer3<? super T, ? super U, 
? super V> after) {
+               Objects.requireNonNull(after);
+               return (t, u, v) -> {
+                   accept(t, u, v);
+                   after.accept(t, u, v);
+               };
+           }
+       }
                
 }

Modified: 
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/ServiceDependencyBuilder.java
URL: 
http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/ServiceDependencyBuilder.java?rev=1720702&r1=1720701&r2=1720702&view=diff
==============================================================================
--- 
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/ServiceDependencyBuilder.java
 (original)
+++ 
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/ServiceDependencyBuilder.java
 Thu Dec 17 23:46:45 2015
@@ -117,7 +117,7 @@ public interface ServiceDependencyBuilde
      * @param method the method name to call on the object instance. This 
method returns the propagated service properties.
         * @return this builder
      */
-    ServiceDependencyBuilder<T> propagateTo(Object instance, String method);
+    ServiceDependencyBuilder<T> propagate(Object instance, String method);
     
     /**
      * Sets the default implementation if the service is not available.

Added: 
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/impl/BundleDependencyBuilderImpl.java
URL: 
http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/impl/BundleDependencyBuilderImpl.java?rev=1720702&view=auto
==============================================================================
--- 
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/impl/BundleDependencyBuilderImpl.java
 (added)
+++ 
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/impl/BundleDependencyBuilderImpl.java
 Thu Dec 17 23:46:45 2015
@@ -0,0 +1,177 @@
+package org.apache.felix.dm.builder.lambda.impl;
+
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Supplier;
+import java.util.stream.Stream;
+
+import org.apache.felix.dm.BundleDependency;
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.builder.lambda.BundleDepenencyBuilder;
+import org.apache.felix.dm.builder.lambda.Functions.Consumer;
+import org.apache.felix.dm.builder.lambda.Functions.Consumer2;
+import org.osgi.framework.Bundle;
+
+public class BundleDependencyBuilderImpl implements BundleDepenencyBuilder {
+       /**
+        * This interface (lambda) is called when we want to invoke a method 
reference. the lambda is called with all necessary dependency 
+        * informations.
+        * 
+        * When the lambda is called, it will invoke the proper callback on the 
given component instance.
+        *
+        * @param <I> type of a component instance
+        */
+       @FunctionalInterface
+    interface MethodRef<I> {
+       public void accept(I instance, Component c, Bundle bundle);
+    }
+
+       /**
+        * List of service (add/change/remove) callbacks.
+        */
+       private final Map<String, List<MethodRef<Object>>> m_refs = new 
HashMap<>();
+       
+       private String m_added;
+       private String m_changed;
+       private String m_removed;
+       private Object m_instance;
+       private boolean m_reflection;
+
+       
+
+
+       @Override
+       public BundleDepenencyBuilder callbacks(String added, String changed, 
String removed) {
+               m_added = added;
+               m_changed = changed;
+               m_removed = removed;
+               m_reflection = true;
+               return this;
+       }
+
+       @Override
+       public BundleDepenencyBuilder callbacks(Object instance, String added, 
String changed, String removed) {
+               callbacks(added, changed, removed);
+               m_instance = instance;
+               return this;
+       }
+
+       @Override
+       public BundleDepenencyBuilder onAdd(Consumer<Bundle> add) {
+               Objects.equals(m_reflection, false);
+       return setCallbackRef("add", (instance, component, bundle) -> 
add.accept(bundle));             
+       }
+
+       @Override
+       public BundleDepenencyBuilder onAdd(Consumer2<Component, Bundle> add) {
+               return this;
+       }
+
+       @SuppressWarnings("unchecked")
+       @Override
+       public <I> BundleDepenencyBuilder onAdd(InstanceBundle<I> add) {
+               Objects.equals(m_reflection, false);
+       String type = Helpers.getLambdaGenericType(add);
+       return setComponentInstanceCallbackRef("add", type,   
+                       (inst, component, bundle) -> add.call((I) inst, 
bundle));             
+       }
+
+       @SuppressWarnings("unchecked")
+       @Override
+       public <I> BundleDepenencyBuilder onAdd(InstanceComponentBundle<I> add) 
{
+               Objects.equals(m_reflection, false);
+       String type = Helpers.getLambdaGenericType(add);
+       return setComponentInstanceCallbackRef("add", type,   
+                       (inst, component, bundle) -> add.call((I) inst, 
component, bundle));             
+       }
+
+       @Override
+       public BundleDepenencyBuilder autoConfig(boolean autoConfig) {
+               // TODO Auto-generated method stub
+               return this;
+       }
+
+       @Override
+       public BundleDepenencyBuilder autoConfig() {
+               // TODO Auto-generated method stub
+               return this;
+       }
+
+       @Override
+       public BundleDepenencyBuilder required(boolean required) {
+               // TODO Auto-generated method stub
+               return this;
+       }
+
+       @Override
+       public BundleDepenencyBuilder required() {
+               // TODO Auto-generated method stub
+               return this;
+       }
+
+       @Override
+       public BundleDepenencyBuilder bundle(Bundle bundle) {
+               // TODO Auto-generated method stub
+               return this;
+       }
+
+       @Override
+       public BundleDepenencyBuilder filter(String filter) throws 
IllegalArgumentException {
+               // TODO Auto-generated method stub
+               return this;
+       }
+
+       @Override
+       public BundleDepenencyBuilder mask(int mask) {
+               // TODO Auto-generated method stub
+               return this;
+       }
+
+       @Override
+       public BundleDepenencyBuilder propagate(boolean propagate) {
+               // TODO Auto-generated method stub
+               return this;
+       }
+
+       @Override
+       public BundleDepenencyBuilder propagate(Object instance, String method) 
{
+               // TODO Auto-generated method stub
+               return this;
+       }
+
+       @Override
+       public BundleDepenencyBuilder propagate(Supplier<Dictionary<?, ?>> 
instance, String method) {
+               // TODO Auto-generated method stub
+               return this;
+       }
+       
+       @Override
+       public BundleDependency build() {
+               // TODO Auto-generated method stub
+               return null;
+       }
+
+       private <I> BundleDepenencyBuilder setCallbackRef(String cb, 
MethodRef<I> ref) {
+               List<MethodRef<Object>> list = m_refs.computeIfAbsent(cb, l -> 
new ArrayList<>());
+               list.add((instance, component, bundle) -> ref.accept(null, 
component, bundle));
+               return this;
+       }
+       
+       @SuppressWarnings("unchecked")
+       public <I> BundleDepenencyBuilder 
setComponentInstanceCallbackRef(String cb, String type, MethodRef<I> ref) {
+                  List<MethodRef<Object>> list = m_refs.computeIfAbsent(cb, l 
-> new ArrayList<>());
+                  list.add((instance, component, bundle) -> {
+                          Stream.of(component.getInstances()).forEach(inst -> {
+                                  if (Helpers.getClassName(inst).equals(type)) 
{
+                                          ref.accept((I) inst, component, 
bundle);
+                                  }
+                          });
+                  });
+                  return this;
+          }
+       
+}

Modified: 
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/impl/ServiceDependencyBuilderImpl.java
URL: 
http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/impl/ServiceDependencyBuilderImpl.java?rev=1720702&r1=1720701&r2=1720702&view=diff
==============================================================================
--- 
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/impl/ServiceDependencyBuilderImpl.java
 (original)
+++ 
felix/sandbox/pderop/dependencymanager-lambda/org.apache.felix.dependencymanager.lambda/src/org/apache/felix/dm/builder/lambda/impl/ServiceDependencyBuilderImpl.java
 Thu Dec 17 23:46:45 2015
@@ -109,7 +109,7 @@ public class ServiceDependencyBuilderImp
         return this;
     }
 
-    public ServiceDependencyBuilder<T> propagateTo(Object instance, String 
method) {
+    public ServiceDependencyBuilder<T> propagate(Object instance, String 
method) {
         m_propagateInstance = instance;
         m_propagateMethod = method;
         return this;


Reply via email to