You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by pa...@apache.org on 2019/05/07 15:22:02 UTC

[sling-org-apache-sling-feature-launcher] 01/02: Make timeout work with framework start

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

pauls pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-launcher.git

commit 080ce222b14f969efe72d5e9dcb69367502394c2
Author: Karl Pauls <ka...@gmail.com>
AuthorDate: Tue May 7 17:12:09 2019 +0200

    Make timeout work with framework start
---
 .../launcher/impl/launchers/AbstractRunner.java    | 39 ++++++++++++++--------
 1 file changed, 26 insertions(+), 13 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/launcher/impl/launchers/AbstractRunner.java b/src/main/java/org/apache/sling/feature/launcher/impl/launchers/AbstractRunner.java
index af15614..b4d4275 100644
--- a/src/main/java/org/apache/sling/feature/launcher/impl/launchers/AbstractRunner.java
+++ b/src/main/java/org/apache/sling/feature/launcher/impl/launchers/AbstractRunner.java
@@ -32,7 +32,13 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.Callable;
 import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executor;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
@@ -145,25 +151,32 @@ public abstract class AbstractRunner implements Callable<Integer> {
     {
         CountDownLatch latch = new CountDownLatch(1);
 
-        FrameworkListener listener = new FrameworkListener()
+        Executor executor = Executors.newSingleThreadExecutor();
+        Future<Void> result = ((ExecutorService) executor).submit(new Callable<Void>()
         {
             @Override
-            public void frameworkEvent(FrameworkEvent frameworkEvent)
+            public Void call() throws Exception
             {
-                if (frameworkEvent.getType() == FrameworkEvent.STARTED) {
-                    latch.countDown();
-                }
+                framework.start();
+                return null;
             }
-        };
-
-        framework.getBundleContext().addFrameworkListener(listener);
-
-        framework.start();
+        });
 
         try {
-            return latch.await(timeout, unit);
-        } finally {
-            framework.getBundleContext().removeFrameworkListener(listener);
+            result.get(timeout, unit);
+            return true;
+        } catch (TimeoutException ex) {
+            return false;
+        } catch (ExecutionException ex) {
+            Throwable cause = ex.getCause();
+            if (cause instanceof BundleException) {
+                throw (BundleException) cause;
+            } else {
+                throw (RuntimeException) cause;
+            }
+        }
+        finally {
+            ((ExecutorService) executor).shutdownNow();
         }
     }