Introduce post start callbacks in Argeo Init
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 10 Dec 2023 10:44:21 +0000 (11:44 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 10 Dec 2023 10:44:21 +0000 (11:44 +0100)
org.argeo.init/src/org/argeo/init/Service.java

index c63fdcd376daffa6ac0492f78ae38bef28ff5fd9..b080a7513b97a3b10c07870eb3ec328af056f2d6 100644 (file)
@@ -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<Runnable> 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<String, String>) 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);
+       }
 }