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:52 UTC

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

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();