Improve Argeo Init shutdown sequence.
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / Service.java
index 9a01cffed555f88eaf081cd7bcb3d86245f61f68..9dcea49d2d6199cad72b9696191de85ecbd69f53 100644 (file)
@@ -6,6 +6,7 @@ import java.util.Map;
 
 import org.argeo.init.osgi.OsgiRuntimeContext;
 
+/** Configure and launch an Argeo service. */
 public class Service implements Runnable, AutoCloseable {
        private final static Logger log = System.getLogger(Service.class.getName());
 
@@ -24,13 +25,16 @@ public class Service implements Runnable, AutoCloseable {
 
        public static void main(String[] args) {
                long pid = ProcessHandle.current().pid();
-               log.log(Logger.Level.DEBUG, "Starting with PID " + pid);
+               log.log(Logger.Level.DEBUG, "Argeo Init starting with PID " + pid);
 
                // shutdown on exit
                Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                        try {
-                               if (Service.runtimeContext != null)
+                               if (Service.runtimeContext != null) {
+//                                     System.out.println("Argeo Init stopping with PID " + pid);
                                        Service.runtimeContext.close();
+                                       Service.runtimeContext.waitForStop(0);
+                               }
                        } catch (Exception e) {
                                e.printStackTrace();
                                System.exit(1);
@@ -38,22 +42,32 @@ public class Service implements Runnable, AutoCloseable {
                }, "Runtime shutdown"));
 
                Map<String, String> config = new HashMap<>();
+               config.put("osgi.framework.useSystemProperties", "true");
+//             for (Object key : System.getProperties().keySet()) {
+//                     config.put(key.toString(), System.getProperty(key.toString()));
+//                     log.log(Logger.Level.DEBUG, key + "=" + System.getProperty(key.toString()));
+//             }
                try {
-                       try (OsgiRuntimeContext osgiRuntimeContext = new OsgiRuntimeContext(config)) {
+                       try {
+                               OsgiRuntimeContext osgiRuntimeContext = new OsgiRuntimeContext((Map<String, String>) config);
                                osgiRuntimeContext.run();
                                Service.runtimeContext = osgiRuntimeContext;
                                Service.runtimeContext.waitForStop(0);
                        } catch (NoClassDefFoundError e) {
-                               try (StaticRuntimeContext staticRuntimeContext = new StaticRuntimeContext(config)) {
-                                       staticRuntimeContext.run();
-                                       Service.runtimeContext = staticRuntimeContext;
-                                       Service.runtimeContext.waitForStop(0);
-                               }
+                               StaticRuntimeContext staticRuntimeContext = new StaticRuntimeContext((Map<String, String>) config);
+                               staticRuntimeContext.run();
+                               Service.runtimeContext = staticRuntimeContext;
+                               Service.runtimeContext.waitForStop(0);
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                        System.exit(1);
                }
+               log.log(Logger.Level.DEBUG, "Argeo Init stopped with PID " + pid);
        }
 
+       
+       public static RuntimeContext getRuntimeContext() {
+               return runtimeContext;
+       }
 }