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

[aries-component-dsl] 01/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 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();