]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/Service.java
Better integrate RCP build
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / Service.java
1 package org.argeo.init;
2
3 import java.lang.System.Logger;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import org.argeo.init.osgi.OsgiRuntimeContext;
8
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());
12
13 private static RuntimeContext runtimeContext = null;
14
15 protected Service(String[] args) {
16 }
17
18 @Override
19 public void run() {
20 }
21
22 @Override
23 public void close() throws Exception {
24 }
25
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);
29
30 // shutdown on exit
31 Runtime.getRuntime().addShutdownHook(new Thread(() -> {
32 try {
33 if (Service.runtimeContext != null) {
34 // System.out.println("Argeo Init stopping with PID " + pid);
35 Service.runtimeContext.close();
36 Service.runtimeContext.waitForStop(0);
37 }
38 } catch (Exception e) {
39 e.printStackTrace();
40 System.exit(1);
41 }
42 }, "Runtime shutdown"));
43
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()));
49 // }
50 try {
51 try {
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);
61 }
62 } catch (Exception e) {
63 e.printStackTrace();
64 System.exit(1);
65 }
66 log.log(Logger.Level.DEBUG, "Argeo Init stopped with PID " + pid);
67 }
68
69
70 public static RuntimeContext getRuntimeContext() {
71 return runtimeContext;
72 }
73 }