You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by cs...@apache.org on 2019/09/17 07:49:48 UTC

[aries-component-dsl] branch master updated (a86ab9c -> e85a373)

This is an automated email from the ASF dual-hosted git repository.

csierra pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/aries-component-dsl.git.


    from a86ab9c  [ARIES-1926] UpdateSupport should not reorder effects
     new d4a8df9  [ARIES-1930] Consistent error handling
     new 0e67873  [ARIES-1930] Consistent error handling
     new 5bd2383  [ARIES-1930] This should not be execute now
     new 452e4d4  [ARIES-1930] Consistent error handling
     new e85a373  Source format

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../java/org/apache/aries/component/dsl/OSGi.java  |  37 +-----
 .../java/org/apache/aries/component/dsl/Utils.java |   3 +-
 .../aries/component/dsl/internal/AllOSGi.java      |  40 ++++---
 .../component/dsl/internal/DistributeOSGiImpl.java |  90 +++++++++++++++
 .../aries/component/dsl/internal/EffectsOSGi.java  |   7 --
 .../aries/component/dsl/internal/JustOSGiImpl.java |  40 +++++--
 .../apache/aries/component/dsl/test/DSLTest.java   | 128 ++++++++++++++++++++-
 7 files changed, 276 insertions(+), 69 deletions(-)
 create mode 100644 component-dsl/src/main/java/org/apache/aries/component/dsl/internal/DistributeOSGiImpl.java


[aries-component-dsl] 02/05: [ARIES-1930] Consistent error handling

Posted by cs...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

csierra pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/aries-component-dsl.git

commit 0e678730866a2f2d82e162445e45d3962dfd7376
Author: Carlos Sierra <ca...@liferay.com>
AuthorDate: Mon Sep 16 17:17:54 2019 +0200

    [ARIES-1930] Consistent error handling
---
 .../aries/component/dsl/internal/AllOSGi.java      | 40 ++++++++++++++--------
 .../apache/aries/component/dsl/test/DSLTest.java   | 35 +++++++++++++++++++
 2 files changed, 60 insertions(+), 15 deletions(-)

diff --git a/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/AllOSGi.java b/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/AllOSGi.java
index 3fc1b7b..9530d5e 100644
--- a/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/AllOSGi.java
+++ b/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/AllOSGi.java
@@ -21,9 +21,7 @@ import org.apache.aries.component.dsl.OSGi;
 import org.apache.aries.component.dsl.OSGiResult;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.ListIterator;
-import java.util.stream.Collectors;
 
 /**
  * @author Carlos Sierra Andrés
@@ -33,23 +31,35 @@ public class AllOSGi<T> extends OSGiImpl<T> {
     @SafeVarargs
     public AllOSGi(OSGi<T>... programs) {
         super((bundleContext, op) -> {
-            ArrayList<OSGiResult> results = new ArrayList<>();
+            ArrayList<OSGiResult> results = new ArrayList<>(programs.length);
 
-            results.addAll(
-                Arrays.stream(programs).
-                    map(o -> o.run(bundleContext, op)).
-                    collect(Collectors.toList()));
+            try {
+                for (OSGi<T> program : programs) {
+                    results.add(program.run(bundleContext, op));
+                }
+            }
+            catch (Exception e) {
+                cleanUp(results);
 
-            return new OSGiResultImpl(
-                () -> {
-                    ListIterator<OSGiResult> iterator =
-                        results.listIterator(results.size());
+                throw e;
+            }
 
-                    while (iterator.hasPrevious()) {
-                        iterator.previous().close();
-                    }
-                }
+            return new OSGiResultImpl(
+                () -> cleanUp(results)
             );
         });
     }
+
+    private static void cleanUp(ArrayList<OSGiResult> results) {
+        ListIterator<OSGiResult> iterator =
+            results.listIterator(results.size());
+
+        while (iterator.hasPrevious()) {
+            try {
+                iterator.previous().close();
+            }
+            catch (Exception ex) {
+            }
+        }
+    }
 }
diff --git a/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java b/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java
index 31efd00..9634de2 100644
--- a/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java
+++ b/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java
@@ -1025,6 +1025,41 @@ public class DSLTest {
     }
 
     @Test
+    public void testAllWithErrors() {
+        ArrayList<Integer> results = new ArrayList<>();
+
+        OSGi<Integer> program = OSGi.all(
+            just(1),
+            just(2),
+            just(3),
+            just(4),
+            just(5)
+        ).effects(
+            results::add, results::remove
+        );
+
+        try(OSGiResult result = program.run(bundleContext)) {
+            assertEquals(Arrays.asList(1, 2, 3, 4, 5), results);
+        }
+
+        try(OSGiResult result = program.filter(i -> {
+                if (i == 5) {
+                    throw new IllegalArgumentException();
+                }
+
+                return true;
+            }
+        ).run(
+            bundleContext)
+        ) {
+            fail();
+        }
+        catch (Exception e) {
+            assertEquals(Collections.emptyList(), results);
+        }
+    }
+
+    @Test
     public void testMultipleApplies() {
         ArrayList<Integer> results = new ArrayList<>();
         AtomicInteger results2 = new AtomicInteger();


[aries-component-dsl] 03/05: [ARIES-1930] This should not be execute now

Posted by cs...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

csierra pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/aries-component-dsl.git

commit 5bd2383687e38c92973a4e45346c9e3d75c7178f
Author: Carlos Sierra <ca...@liferay.com>
AuthorDate: Mon Sep 16 17:25:49 2019 +0200

    [ARIES-1930] This should not be execute now
    
    because the counter part has not executed
---
 .../src/main/java/org/apache/aries/component/dsl/OSGi.java         | 7 -------
 .../java/org/apache/aries/component/dsl/internal/EffectsOSGi.java  | 7 -------
 2 files changed, 14 deletions(-)

diff --git a/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java b/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java
index 7448ad4..e87ec5b 100644
--- a/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java
+++ b/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java
@@ -657,13 +657,6 @@ public interface OSGi<T> extends OSGiRunnable<T> {
 					}
 					catch (Exception e) {
 						try {
-							onRemovedBefore.accept(t);
-						}
-						catch (Exception e1) {
-							//TODO: logging
-						}
-
-						try {
 							onRemovedAfter.accept(t);
 						}
 						catch (Exception e1) {
diff --git a/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/EffectsOSGi.java b/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/EffectsOSGi.java
index ec95355..1d4d459 100644
--- a/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/EffectsOSGi.java
+++ b/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/EffectsOSGi.java
@@ -70,13 +70,6 @@ public class EffectsOSGi extends OSGiImpl<Void> {
             }
             catch (Exception e) {
                 try {
-                    onRemovingBefore.run();
-                }
-                catch (Exception e1) {
-                    //TODO: logging
-                }
-
-                try {
                     onRemovingAfter.run();
                 }
                 catch (Exception e1) {


[aries-component-dsl] 01/05: [ARIES-1930] Consistent error handling

Posted by cs...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

csierra pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/aries-component-dsl.git

commit d4a8df9b7bbea652d24fae80e876c8ed246544ba
Author: Carlos Sierra <ca...@liferay.com>
AuthorDate: Mon Sep 16 17:16:54 2019 +0200

    [ARIES-1930] Consistent error handling
---
 .../aries/component/dsl/internal/JustOSGiImpl.java | 40 ++++++++++++++++------
 .../apache/aries/component/dsl/test/DSLTest.java   | 33 ++++++++++++++++++
 2 files changed, 62 insertions(+), 11 deletions(-)

diff --git a/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/JustOSGiImpl.java b/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/JustOSGiImpl.java
index e8a9803..61f07d0 100644
--- a/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/JustOSGiImpl.java
+++ b/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/JustOSGiImpl.java
@@ -19,6 +19,7 @@
 package org.apache.aries.component.dsl.internal;
 
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
@@ -35,24 +36,41 @@ public class JustOSGiImpl<T> extends OSGiImpl<T> {
 		this(() -> t);
 	}
 
-	public JustOSGiImpl(Supplier<Collection<T>> t) {
+	public JustOSGiImpl(Supplier<Collection<T>> supplier) {
 		super((bundleContext, op) -> {
 
-			List<Runnable> references =
-				t.get().stream().map(op).collect(Collectors.toList());
+			Collection<T> collection = supplier.get();
+			ArrayList<Runnable> references = new ArrayList<>(collection.size());
+
+			try {
+				for (T t : collection) {
+					references.add(op.publish(t));
+				}
+			}
+			catch (Exception e) {
+				cleanUp(references);
+
+				throw e;
+			}
 
 			return new OSGiResultImpl(
-				() -> {
-					ListIterator<Runnable> iterator =
-						references.listIterator(references.size());
-
-					while (iterator.hasPrevious()) {
-						iterator.previous().run();
-					}
-				});
+				() -> cleanUp(references));
 		});
 	}
 
+	private static void cleanUp(ArrayList<Runnable> references) {
+		ListIterator<Runnable> iterator =
+			references.listIterator(references.size());
+
+		while (iterator.hasPrevious()) {
+			try {
+				iterator.previous().run();
+			}
+			catch (Exception e) {
+			}
+		}
+	}
+
 	public JustOSGiImpl(T t) {
 		this(() -> Collections.singletonList(t));
 	}
diff --git a/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java b/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java
index c8ad473..31efd00 100644
--- a/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java
+++ b/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java
@@ -71,6 +71,7 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 public class DSLTest {
 
@@ -992,6 +993,38 @@ public class DSLTest {
     }
 
     @Test
+    public void testJustWithError() {
+        ArrayList<Integer> results = new ArrayList<>();
+
+        OSGi<Integer> program = just(
+            Arrays.asList(1, 2, 3, 4, 5)
+        ).effects(
+            results::add, results::remove
+        );
+
+        try(OSGiResult result = program.run(bundleContext)) {
+            assertEquals(Arrays.asList(1, 2, 3, 4, 5), results);
+        }
+
+        try(OSGiResult result = program.filter(i -> {
+                if (i == 5) {
+                    throw new IllegalArgumentException();
+                }
+
+                return true;
+            }
+        ).run(
+            bundleContext)
+        ) {
+            fail();
+        }
+        catch (Exception e) {
+            assertEquals(Collections.emptyList(), results);
+        }
+
+    }
+
+    @Test
     public void testMultipleApplies() {
         ArrayList<Integer> results = new ArrayList<>();
         AtomicInteger results2 = new AtomicInteger();


[aries-component-dsl] 04/05: [ARIES-1930] Consistent error handling

Posted by cs...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

csierra pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/aries-component-dsl.git

commit 452e4d4e17580baf06968c07b64f4b499ade02a7
Author: Carlos Sierra <ca...@liferay.com>
AuthorDate: Mon Sep 16 17:46:54 2019 +0200

    [ARIES-1930] Consistent error handling
---
 .../java/org/apache/aries/component/dsl/OSGi.java  | 30 +-------
 .../component/dsl/internal/DistributeOSGiImpl.java | 90 ++++++++++++++++++++++
 .../apache/aries/component/dsl/test/DSLTest.java   | 60 ++++++++++++++-
 3 files changed, 152 insertions(+), 28 deletions(-)

diff --git a/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java b/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java
index e87ec5b..60f577b 100644
--- a/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java
+++ b/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java
@@ -31,6 +31,7 @@ import org.apache.aries.component.dsl.function.Function8;
 import org.apache.aries.component.dsl.function.Function9;
 import org.apache.aries.component.dsl.internal.CoalesceOSGiImpl;
 import org.apache.aries.component.dsl.internal.ConfigurationOSGiImpl;
+import org.apache.aries.component.dsl.internal.DistributeOSGiImpl;
 import org.apache.aries.component.dsl.internal.EffectsOSGi;
 import org.apache.aries.component.dsl.internal.NothingOSGiImpl;
 import org.apache.aries.component.dsl.internal.Pad;
@@ -67,12 +68,14 @@ import org.osgi.framework.ServiceObjects;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Dictionary;
 import java.util.HashMap;
 import java.util.List;
+import java.util.ListIterator;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
@@ -573,32 +576,7 @@ public interface OSGi<T> extends OSGiRunnable<T> {
 	}
 
 	default <S> OSGi<S> distribute(Function<OSGi<T>, OSGi<S>> ... funs) {
-		return fromOsgiRunnable((bundleContext, publisher) -> {
-			List<Pad<T, S>> pads =
-				Arrays.stream(
-					funs
-				).map(
-					fun -> new Pad<>(bundleContext, fun, publisher)
-				).collect(
-					Collectors.toList()
-				);
-
-			OSGiResult result = run(
-				bundleContext,
-				t -> {
-					List<Runnable> terminators =
-						pads.stream().map(p -> p.publish(t)).collect(
-							Collectors.toList());
-
-					return () -> terminators.forEach(Runnable::run);
-				});
-
-			return () -> {
-				result.close();
-
-				pads.forEach(Pad::close);
-			};
-		});
+		return new DistributeOSGiImpl<>(this, funs);
 	}
 
 	default OSGi<T> effects(
diff --git a/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/DistributeOSGiImpl.java b/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/DistributeOSGiImpl.java
new file mode 100644
index 0000000..7f00e18
--- /dev/null
+++ b/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/DistributeOSGiImpl.java
@@ -0,0 +1,90 @@
+/**
+ * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
+ *
+ * The contents of this file are subject to the terms of the Liferay Enterprise
+ * Subscription License ("License"). You may not use this file except in
+ * compliance with the License. You can obtain a copy of the License by
+ * contacting Liferay, Inc. See the License for the specific language governing
+ * permissions and limitations under the License, including but not limited to
+ * distribution rights of the Software.
+ *
+ *
+ *
+ */
+
+package org.apache.aries.component.dsl.internal;
+
+import org.apache.aries.component.dsl.OSGi;
+import org.apache.aries.component.dsl.OSGiResult;
+import org.apache.aries.component.dsl.OSGiRunnable;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.function.Function;
+
+/**
+ * @author Carlos Sierra Andrés
+ */
+public class DistributeOSGiImpl<T, S> extends OSGiImpl<S> {
+
+    @SafeVarargs
+    public DistributeOSGiImpl(
+        OSGiRunnable<T> operation, Function<OSGi<T>, OSGi<S>>... funs) {
+
+        super((bundleContext, publisher) -> {
+            Pad<T, S>[] pads = new Pad[funs.length];
+
+            for (int i = 0; i < funs.length; i++) {
+                pads[i] = new Pad<>(bundleContext, funs[i], publisher);
+            }
+
+            OSGiResult result = operation.run(
+                bundleContext,
+                t -> {
+                    List<Runnable> terminators = new ArrayList<>(funs.length);
+
+                    int i = 0;
+
+                    try {
+                        for (; i < funs.length; i++) {
+                            terminators.add(pads[i].publish(t));
+                        }
+                    }
+                    catch (Exception e) {
+                        cleanUp(terminators);
+
+                        throw e;
+                    }
+
+                    return () -> cleanUp(terminators);
+                });
+
+            return () -> {
+                result.close();
+
+                for (Pad<T, S> pad : pads) {
+                    try {
+                        pad.close();
+                    }
+                    catch (Exception e) {
+                    }
+                }
+            };
+        });
+    }
+
+    private static void cleanUp(List<Runnable> terminators) {
+        ListIterator<Runnable> iterator =
+            terminators.listIterator(terminators.size());
+
+        while (iterator.hasPrevious()) {
+            try {
+                iterator.previous().run();
+            }
+            catch (Exception e) {
+            }
+        }
+    }
+
+}
diff --git a/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java b/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java
index 9634de2..a5cf8df 100644
--- a/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java
+++ b/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java
@@ -31,9 +31,7 @@ import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ConfigurationAdmin;
-import org.osgi.service.cm.ConfigurationException;
 import org.osgi.service.cm.ManagedService;
-import org.osgi.service.cm.ManagedServiceFactory;
 import org.osgi.util.tracker.ServiceTracker;
 
 import java.io.IOException;
@@ -1060,6 +1058,64 @@ public class DSLTest {
     }
 
     @Test
+    public void testDistributeWithError() {
+        ArrayList<Integer> results1 = new ArrayList<>();
+        ArrayList<Integer> results2 = new ArrayList<>();
+        ArrayList<Integer> results3 = new ArrayList<>();
+        ArrayList<Integer> results4 = new ArrayList<>();
+
+        OSGi<Integer> program = OSGi.just(
+            Arrays.asList(1, 2, 3)
+        ).distribute(
+            p1 -> p1.effects(results1::add, results1::remove),
+            p2 -> p2.effects(results2::add, results2::remove),
+            p3 -> p3.filter(i -> {
+                    if (i == 2) {
+                        throw new IllegalArgumentException();
+                    }
+
+                    return true;
+                }
+            ).effects(results3::add, results3::remove),
+            p4 -> p4.effects(results4::add, results4::remove)
+        );
+
+        try (OSGiResult run = program.run(bundleContext)) {
+
+        } catch (Exception e) {
+            assertEquals(Collections.emptyList(), results1);
+            assertEquals(Collections.emptyList(), results2);
+            assertEquals(Collections.emptyList(), results3);
+            assertEquals(Collections.emptyList(), results4);
+        }
+
+        program = OSGi.just(
+            Arrays.asList(1, 2, 3)
+        ).recoverWith(
+            (i, e) -> nothing()
+        ).distribute(
+            p1 -> p1.effects(results1::add, results1::remove),
+            p2 -> p2.effects(results2::add, results2::remove),
+            p3 -> p3.filter(i -> {
+                    if (i == 2) {
+                        throw new IllegalArgumentException();
+                    }
+
+                    return true;
+                }
+            ).effects(results3::add, results3::remove),
+            p4 -> p4.effects(results4::add, results4::remove)
+        );
+
+        try (OSGiResult run = program.run(bundleContext)) {
+            assertEquals(Arrays.asList(1, 3), results1);
+            assertEquals(Arrays.asList(1, 3), results2);
+            assertEquals(Arrays.asList(1, 3), results3);
+            assertEquals(Arrays.asList(1, 3), results4);
+        }
+    }
+
+    @Test
     public void testMultipleApplies() {
         ArrayList<Integer> results = new ArrayList<>();
         AtomicInteger results2 = new AtomicInteger();


[aries-component-dsl] 05/05: Source format

Posted by cs...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

csierra pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/aries-component-dsl.git

commit e85a373ca0c1e6b3daaa737d415e8aadaa7e4c50
Author: Carlos Sierra <ca...@liferay.com>
AuthorDate: Mon Sep 16 17:47:16 2019 +0200

    Source format
---
 component-dsl/src/main/java/org/apache/aries/component/dsl/Utils.java | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/component-dsl/src/main/java/org/apache/aries/component/dsl/Utils.java b/component-dsl/src/main/java/org/apache/aries/component/dsl/Utils.java
index b5b2e93..db29d5d 100644
--- a/component-dsl/src/main/java/org/apache/aries/component/dsl/Utils.java
+++ b/component-dsl/src/main/java/org/apache/aries/component/dsl/Utils.java
@@ -59,7 +59,8 @@ public interface Utils {
                 program.splitBy(
                     keyFun,
                     (k, p) ->
-                        highest(p, Comparator.naturalOrder(), q -> OSGi.nothing()).
+                        highest(
+                            p, Comparator.naturalOrder(), q -> OSGi.nothing()).
                             flatMap(t ->
                                 valueFun.apply(t).effects(
                                     v -> map.put(k, v),