]> git.argeo.org Git - lgpl/argeo-commons.git/blob - init/Service.java
Prepare next development cycle
[lgpl/argeo-commons.git] / init / Service.java
1 package org.argeo.init;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.lang.System.Logger;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
8 import java.nio.file.Paths;
9 import java.util.HashMap;
10 import java.util.Map;
11 import java.util.Objects;
12 import java.util.Properties;
13
14 import org.argeo.init.logging.ThinLoggerFinder;
15 import org.argeo.init.osgi.OsgiBoot;
16 import org.argeo.init.osgi.OsgiRuntimeContext;
17
18 /** Configure and launch an Argeo service. */
19 public class Service {
20 private final static Logger logger = System.getLogger(Service.class.getName());
21
22 public final static String PROP_ARGEO_INIT_MAIN = "argeo.init.main";
23
24 private static RuntimeContext runtimeContext = null;
25
26 protected Service(String[] args) {
27 }
28
29 public static void main(String[] args) {
30 final long pid = ProcessHandle.current().pid();
31 logger.log(Logger.Level.DEBUG, () -> "Argeo Init starting with PID " + pid);
32
33 // shutdown on exit
34 Runtime.getRuntime().addShutdownHook(new Thread(() -> {
35 try {
36 if (Service.runtimeContext != null) {
37 // System.out.println("Argeo Init stopping with PID " + pid);
38 Service.runtimeContext.close();
39 Service.runtimeContext.waitForStop(0);
40 }
41 } catch (Exception e) {
42 e.printStackTrace();
43 Runtime.getRuntime().halt(1);
44 }
45 }, "Runtime shutdown"));
46
47 // TODO use args as well
48 String dataArea = System.getProperty(OsgiBoot.PROP_OSGI_INSTANCE_AREA);
49 String stateArea = System.getProperty(OsgiBoot.PROP_OSGI_CONFIGURATION_AREA);
50 String configArea = System.getProperty(OsgiBoot.PROP_OSGI_SHARED_CONFIGURATION_AREA);
51
52 if (configArea != null) {
53 Path configAreaPath = Paths.get(configArea);
54 Path additionalSystemPropertiesPath = configAreaPath.resolve("system.properties");
55 if (Files.exists(additionalSystemPropertiesPath)) {
56 Properties properties = new Properties();
57 try (InputStream in = Files.newInputStream(additionalSystemPropertiesPath)) {
58 properties.load(in);
59 } catch (IOException e) {
60 logger.log(Logger.Level.ERROR,
61 "Cannot load additional system properties " + additionalSystemPropertiesPath, e);
62 }
63
64 for (Object key : properties.keySet()) {
65 String currentValue = System.getProperty(key.toString());
66 String value = properties.getProperty(key.toString());
67 if (currentValue != null) {
68 if (!Objects.equals(value, currentValue))
69 logger.log(Logger.Level.WARNING, "System property " + key + " already set with value "
70 + currentValue + " instead of " + value + ". Ignoring new value.");
71 } else {
72 System.setProperty(key.toString(), value);
73 }
74 }
75 ThinLoggerFinder.reloadConfiguration();
76 }
77 }
78
79 Map<String, String> config = new HashMap<>();
80 config.put(PROP_ARGEO_INIT_MAIN, "true");
81
82 try {
83 try {
84 if (stateArea != null)
85 config.put(OsgiBoot.PROP_OSGI_CONFIGURATION_AREA, stateArea);
86 if (configArea != null)
87 config.put(OsgiBoot.PROP_OSGI_SHARED_CONFIGURATION_AREA, configArea);
88 if (dataArea != null)
89 config.put(OsgiBoot.PROP_OSGI_INSTANCE_AREA, dataArea);
90 // config.put(OsgiBoot.PROP_OSGI_USE_SYSTEM_PROPERTIES, "true");
91
92 OsgiRuntimeContext osgiRuntimeContext = new OsgiRuntimeContext(config);
93 osgiRuntimeContext.run();
94 Service.runtimeContext = osgiRuntimeContext;
95 Service.runtimeContext.waitForStop(0);
96 } catch (NoClassDefFoundError e) {
97 StaticRuntimeContext staticRuntimeContext = new StaticRuntimeContext((Map<String, String>) config);
98 staticRuntimeContext.run();
99 Service.runtimeContext = staticRuntimeContext;
100 Service.runtimeContext.waitForStop(0);
101 }
102 } catch (Exception e) {
103 e.printStackTrace();
104 System.exit(1);
105 }
106 logger.log(Logger.Level.DEBUG, "Argeo Init stopped with PID " + pid);
107 }
108
109 public static RuntimeContext getRuntimeContext() {
110 return runtimeContext;
111 }
112 }