]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/Service.java
Move default UUID factory to the implementation
[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 public class Service implements Runnable, AutoCloseable {
10 private final static Logger log = System.getLogger(Service.class.getName());
11
12 private static RuntimeContext runtimeContext = null;
13
14 protected Service(String[] args) {
15 }
16
17 @Override
18 public void run() {
19 }
20
21 @Override
22 public void close() throws Exception {
23 }
24
25 public static void main(String[] args) {
26 long pid = ProcessHandle.current().pid();
27 log.log(Logger.Level.DEBUG, "Starting with PID " + pid);
28
29 // shutdown on exit
30 Runtime.getRuntime().addShutdownHook(new Thread(() -> {
31 try {
32 if (Service.runtimeContext != null)
33 Service.runtimeContext.close();
34 } catch (Exception e) {
35 e.printStackTrace();
36 System.exit(1);
37 }
38 }, "Runtime shutdown"));
39
40 Map<String, String> config = new HashMap<>();
41 try {
42 try (OsgiRuntimeContext osgiRuntimeContext = new OsgiRuntimeContext(config)) {
43 osgiRuntimeContext.run();
44 Service.runtimeContext = osgiRuntimeContext;
45 Service.runtimeContext.waitForStop(0);
46 } catch (NoClassDefFoundError e) {
47 try (StaticRuntimeContext staticRuntimeContext = new StaticRuntimeContext(config)) {
48 staticRuntimeContext.run();
49 Service.runtimeContext = staticRuntimeContext;
50 Service.runtimeContext.waitForStop(0);
51 }
52 }
53 } catch (Exception e) {
54 e.printStackTrace();
55 System.exit(1);
56 }
57 }
58
59 }