1 package org
.argeo
.init
;
3 import java
.lang
.System
.Logger
;
4 import java
.util
.HashMap
;
7 import org
.argeo
.init
.osgi
.OsgiRuntimeContext
;
9 /** Configure and launch an Argeo service. */
10 public class Service
implements Runnable
, AutoCloseable
{
11 private final static Logger log
= System
.getLogger(Service
.class.getName());
13 private static RuntimeContext runtimeContext
= null;
15 protected Service(String
[] args
) {
23 public void close() throws Exception
{
26 public static void main(String
[] args
) {
27 long pid
= ProcessHandle
.current().pid();
28 log
.log(Logger
.Level
.DEBUG
, "Argeo Init starting with PID " + pid
);
31 Runtime
.getRuntime().addShutdownHook(new Thread(() -> {
33 if (Service
.runtimeContext
!= null) {
34 // System.out.println("Argeo Init stopping with PID " + pid);
35 Service
.runtimeContext
.close();
36 Service
.runtimeContext
.waitForStop(0);
38 } catch (Exception e
) {
42 }, "Runtime shutdown"));
44 Map
<String
, String
> config
= new HashMap
<>();
45 config
.put("osgi.framework.useSystemProperties", "true");
46 // for (Object key : System.getProperties().keySet()) {
47 // config.put(key.toString(), System.getProperty(key.toString()));
48 // log.log(Logger.Level.DEBUG, key + "=" + System.getProperty(key.toString()));
52 OsgiRuntimeContext osgiRuntimeContext
= new OsgiRuntimeContext((Map
<String
, String
>) config
);
53 osgiRuntimeContext
.run();
54 Service
.runtimeContext
= osgiRuntimeContext
;
55 Service
.runtimeContext
.waitForStop(0);
56 } catch (NoClassDefFoundError e
) {
57 StaticRuntimeContext staticRuntimeContext
= new StaticRuntimeContext((Map
<String
, String
>) config
);
58 staticRuntimeContext
.run();
59 Service
.runtimeContext
= staticRuntimeContext
;
60 Service
.runtimeContext
.waitForStop(0);
62 } catch (Exception e
) {
66 log
.log(Logger
.Level
.DEBUG
, "Argeo Init stopped with PID " + pid
);
70 public static RuntimeContext
getRuntimeContext() {
71 return runtimeContext
;