From 4e946b88d60b50df84fc25c40f23c4053128b86f Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Sun, 10 Dec 2023 11:44:21 +0100 Subject: [PATCH] Introduce post start callbacks in Argeo Init --- .../src/org/argeo/init/Service.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/org.argeo.init/src/org/argeo/init/Service.java b/org.argeo.init/src/org/argeo/init/Service.java index c63fdcd37..b080a7513 100644 --- a/org.argeo.init/src/org/argeo/init/Service.java +++ b/org.argeo.init/src/org/argeo/init/Service.java @@ -3,10 +3,14 @@ package org.argeo.init; import java.io.IOException; import java.io.InputStream; import java.lang.System.Logger; +import java.lang.System.Logger.Level; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Properties; @@ -26,6 +30,8 @@ public class Service { private static RuntimeContext runtimeContext = null; + private static List postStart = Collections.synchronizedList(new ArrayList<>()); + protected Service(String[] args) { } @@ -118,11 +124,25 @@ public class Service { OsgiRuntimeContext osgiRuntimeContext = new OsgiRuntimeContext(config); osgiRuntimeContext.run(); Service.runtimeContext = osgiRuntimeContext; + for (Runnable run : postStart) { + try { + run.run(); + } catch (Exception e) { + logger.log(Level.ERROR, "Cannot run post start callback " + run, e); + } + } Service.runtimeContext.waitForStop(0); - } catch (NoClassDefFoundError e) { + } catch (NoClassDefFoundError noClassDefFoundE) { StaticRuntimeContext staticRuntimeContext = new StaticRuntimeContext((Map) config); staticRuntimeContext.run(); Service.runtimeContext = staticRuntimeContext; + for (Runnable run : postStart) { + try { + run.run(); + } catch (Exception e) { + logger.log(Level.ERROR, "Cannot run post start callback " + run, e); + } + } Service.runtimeContext.waitForStop(0); } } catch (Exception e) { @@ -136,4 +156,9 @@ public class Service { public static RuntimeContext getRuntimeContext() { return runtimeContext; } + + /** Add a post-start call back to be run after the runtime has been started. */ + public static void addPostStart(Runnable runnable) { + postStart.add(runnable); + } } -- 2.30.2