aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From csie...@apache.org
Subject svn commit: r1833695 - in /aries/trunk/component-dsl: component-dsl/src/main/java/org/apache/aries/component/dsl/ component-dsl/src/main/java/org/apache/aries/component/dsl/internal/ itests/src/main/java/org/apache/aries/component/dsl/test/
Date Mon, 18 Jun 2018 09:34:18 GMT
Author: csierra
Date: Mon Jun 18 09:34:18 2018
New Revision: 1833695

URL: http://svn.apache.org/viewvc?rev=1833695&view=rev
Log:
[ARIES-1813] Add effects execution places

Modified:
    aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java
    aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/Utils.java
    aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/EffectsOSGi.java
    aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java

Modified: aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java
URL: http://svn.apache.org/viewvc/aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java?rev=1833695&r1=1833694&r2=1833695&view=diff
==============================================================================
--- aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java
(original)
+++ aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java
Mon Jun 18 09:34:18 2018
@@ -224,11 +224,21 @@ public interface OSGi<T> extends OSGiRun
 	static OSGi<Void> effect(Effect<Void> effect) {
 		return new EffectsOSGi(
 			() -> effect.getOnIncoming().accept(null),
+			NOOP,
+			NOOP,
 			() -> effect.getOnLeaving().accept(null));
 	}
 
 	static OSGi<Void> effects(Runnable onAdding, Runnable onRemoving) {
-		return new EffectsOSGi(onAdding, onRemoving);
+		return new EffectsOSGi(onAdding, NOOP, NOOP, onRemoving);
+	}
+
+	static OSGi<Void> effects(
+		Runnable onAddingBefore, Runnable onAddingAfter,
+		Runnable onRemovingBefore, Runnable onRemovingAfter) {
+
+		return new EffectsOSGi(
+			onAddingBefore, onAddingAfter, onRemovingBefore, onRemovingAfter);
 	}
 
 	static <T> OSGi<T> fromOsgiRunnable(OSGiRunnable<T> runnable) {
@@ -589,28 +599,74 @@ public interface OSGi<T> extends OSGiRun
 	default OSGi<T> effects(
 		Consumer<? super T> onAdded, Consumer<? super T> onRemoved) {
 
+		return effects(onAdded, __ -> {}, __ -> {}, onRemoved);
+	}
+
+	default OSGi<T> effects(
+		Consumer<? super T> onAddedBefore, Consumer<? super T> onAddedAfter,
+		Consumer<? super T> onRemovedBefore,
+		Consumer<? super T> onRemovedAfter) {
+
 		return fromOsgiRunnable((bundleContext, op) ->
 			run(
 				bundleContext,
 				t -> {
-					onAdded.accept(t);
+					onAddedBefore.accept(t);
 
-					Runnable terminator;
 					try {
-						terminator = op.apply(t);
+						Runnable terminator = op.publish(t);
+
+						try {
+							onAddedAfter.accept(t);
+						}
+						catch (Exception e) {
+							//TODO: logging
+						}
+
+						return
+							() -> {
+								try {
+									onRemovedBefore.accept(t);
+								}
+								catch (Exception e) {
+									//TODO: logging
+								}
+
+								try {
+									terminator.run();
+								}
+								catch (Exception e) {
+									//TODO: logging
+								}
+
+								try {
+									onRemovedAfter.accept(t);
+								}
+								catch (Exception e) {
+									//TODO: logging
+								}
+							};
 					}
 					catch (Exception e) {
-						onRemoved.accept(t);
+						try {
+							onRemovedBefore.accept(t);
+						}
+						catch (Exception e1) {
+							//TODO: logging
+						}
+
+						try {
+							onRemovedAfter.accept(t);
+						}
+						catch (Exception e1) {
+							//TODO: logging
+						}
 
 						throw e;
 					}
-
-					return () -> {
-						onRemoved.accept(t);
-
-						terminator.run();
-					};
-				}));
+				}
+			)
+		);
 	}
 
 	default OSGi<T> effects(Effect<? super T> effect) {

Modified: aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/Utils.java
URL: http://svn.apache.org/viewvc/aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/Utils.java?rev=1833695&r1=1833694&r2=1833695&view=diff
==============================================================================
--- aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/Utils.java
(original)
+++ aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/Utils.java
Mon Jun 18 09:34:18 2018
@@ -25,8 +25,10 @@ import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Function;
 
+import static org.apache.aries.component.dsl.OSGi.NOOP;
 import static org.apache.aries.component.dsl.OSGi.just;
 
 /**
@@ -39,7 +41,7 @@ public interface Utils {
             OSGi.just(ArrayList<T>::new).flatMap(list ->
             OSGi.all(
                 OSGi.just(ArrayList<T>::new),
-                program.effects(list::add, list::remove).
+                program.effects(list::add, __ -> {}, list::remove, __ -> {}).
                 then(OSGi.just(() -> new ArrayList<>(list)
                 ))
             ).transform(
@@ -61,7 +63,9 @@ public interface Utils {
                             flatMap(t ->
                                 valueFun.apply(t).effects(
                                     v -> map.put(k, v),
-                                    __ -> map.remove(k)
+                                    __ -> {},
+                                    __ -> map.remove(k),
+                                    __ -> {}
                                 )
                             )
                 ).then(OSGi.just(() -> new HashMap<>(map)))

Modified: aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/EffectsOSGi.java
URL: http://svn.apache.org/viewvc/aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/EffectsOSGi.java?rev=1833695&r1=1833694&r2=1833695&view=diff
==============================================================================
--- aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/EffectsOSGi.java
(original)
+++ aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/EffectsOSGi.java
Mon Jun 18 09:34:18 2018
@@ -22,31 +22,61 @@ package org.apache.aries.component.dsl.i
  */
 public class EffectsOSGi extends OSGiImpl<Void> {
 
-    public EffectsOSGi(Runnable onAdding, Runnable onRemoving) {
+    public EffectsOSGi(
+        Runnable onAddingBefore, Runnable onAddingAfter,
+        Runnable onRemovingBefore, Runnable onRemovingAfter) {
+
         super((bundleContext, op) -> {
-            onAdding.run();
+            onAddingBefore.run();
 
             try {
                 Runnable terminator = op.publish(null);
 
+                try {
+                    onAddingAfter.run();
+                }
+                catch (Exception e) {
+                    //TODO: logging
+                }
+
                 return new OSGiResultImpl(
                     () -> {
                         try {
+                            onRemovingBefore.run();
+                        }
+                        catch (Exception e) {
+                            //TODO: logging
+                        }
+
+                        try {
                             terminator.run();
                         }
-                        finally {
-                            try {
-                                onRemoving.run();
-                            }
-                            catch (Exception e) {
-                                //TODO: logging
-                            }
+                        catch (Exception e) {
+                            //TODO: logging
                         }
 
+                        try {
+                            onRemovingAfter.run();
+                        }
+                        catch (Exception e) {
+                            //TODO: logging
+                        }
                  });
             }
             catch (Exception e) {
-                onRemoving.run();
+                try {
+                    onRemovingBefore.run();
+                }
+                catch (Exception e1) {
+                    //TODO: logging
+                }
+
+                try {
+                    onRemovingAfter.run();
+                }
+                catch (Exception e1) {
+                    //TODO: logging
+                }
 
                 throw e;
             }

Modified: aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java?rev=1833695&r1=1833694&r2=1833695&view=diff
==============================================================================
--- aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java
(original)
+++ aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java
Mon Jun 18 09:34:18 2018
@@ -237,6 +237,37 @@ public class DSLTest {
     }
 
     @Test
+    public void testEffectsOrder() {
+        ArrayList<Object> effects = new ArrayList<>();
+
+        OSGi<Void> program = just(Arrays.asList(1, 2, 3, 4)).effects(
+            effects::add,
+            effects::remove
+        ).
+        flatMap(t -> OSGi.effects(
+            () -> assertEquals(1, effects.stream().filter(t::equals).count()),
+            () -> assertEquals(1, effects.stream().filter(t::equals).count())
+        ).then(just(t))).
+        effects(
+            effects::add,
+            effects::remove
+        ).flatMap(t ->
+        OSGi.effects(
+            () ->
+                assertEquals(2, effects.stream().filter(t::equals).count()),
+            () ->
+                assertEquals(2, effects.stream().filter(t::equals).count()))
+        );
+
+        program = OSGi.effects(
+            () -> assertTrue(effects.isEmpty()),
+            () -> assertTrue(effects.isEmpty())).
+            then(program);
+
+        try (OSGiResult result = program.run(bundleContext)) {}
+    }
+
+    @Test
     public void testCoalesce() {
         ProbeImpl<String> program1 = new ProbeImpl<>();
         ProbeImpl<String> program2 = new ProbeImpl<>();



Mime
View raw message