]> git.argeo.org Git - lgpl/argeo-commons.git/blob - argeo/init/ServiceMain.java
Prepare next development cycle
[lgpl/argeo-commons.git] / argeo / init / ServiceMain.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.lang.System.Logger.Level;
7 import java.nio.file.Files;
8 import java.nio.file.Path;
9 import java.nio.file.Paths;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Objects;
16 import java.util.Properties;
17 import java.util.TreeMap;
18
19 import org.argeo.api.init.InitConstants;
20 import org.argeo.init.logging.ThinLoggerFinder;
21 import org.argeo.init.osgi.OsgiRuntimeContext;
22 import org.argeo.internal.init.InternalState;
23
24 /** Configures and launches a single runtime, typically as a systemd service. */
25 public class ServiceMain {
26 private final static Logger logger = System.getLogger(ServiceMain.class.getName());
27
28 final static String FILE_SYSTEM_PROPERTIES = "system.properties";
29
30 public final static String PROP_ARGEO_INIT_MAIN = "argeo.init.main";
31
32 // private static RuntimeContext runtimeContext = null;
33
34 private static List<Runnable> postStart = Collections.synchronizedList(new ArrayList<>());
35
36 protected ServiceMain(String[] args) {
37 }
38
39 public static void main(String[] args) {
40 final long pid = ProcessHandle.current().pid();
41 logger.log(Logger.Level.DEBUG, () -> "Argeo Init starting with PID " + pid);
42
43 // shutdown on exit
44 Runtime.getRuntime().addShutdownHook(new Thread(() -> {
45 try {
46 if (InternalState.getMainRuntimeContext() != null) {
47 InternalState.getMainRuntimeContext().close();
48 InternalState.getMainRuntimeContext().waitForStop(0);
49 }
50 } catch (Exception e) {
51 e.printStackTrace();
52 Runtime.getRuntime().halt(1);
53 }
54 }, "Runtime shutdown"));
55
56 // TODO use args as well
57 String dataArea = System.getProperty(InitConstants.PROP_OSGI_INSTANCE_AREA);
58 String stateArea = System.getProperty(InitConstants.PROP_OSGI_CONFIGURATION_AREA);
59 String configArea = System.getProperty(InitConstants.PROP_OSGI_SHARED_CONFIGURATION_AREA);
60
61 if (configArea != null) {
62 Path configAreaPath = Paths.get(configArea);
63 Path additionalSystemPropertiesPath = configAreaPath.resolve(FILE_SYSTEM_PROPERTIES);
64 if (Files.exists(additionalSystemPropertiesPath)) {
65 Properties properties = new Properties();
66 try (InputStream in = Files.newInputStream(additionalSystemPropertiesPath)) {
67 properties.load(in);
68 } catch (IOException e) {
69 logger.log(Logger.Level.ERROR,
70 "Cannot load additional system properties " + additionalSystemPropertiesPath, e);
71 }
72
73 for (Object key : properties.keySet()) {
74 String currentValue = System.getProperty(key.toString());
75 String value = properties.getProperty(key.toString());
76 if (currentValue != null) {
77 if (!Objects.equals(value, currentValue))
78 logger.log(Logger.Level.WARNING, "System property " + key + " already set with value "
79 + currentValue + " instead of " + value + ". Ignoring new value.");
80 } else {
81 System.setProperty(key.toString(), value);
82 logger.log(Logger.Level.TRACE, () -> "Added " + key + "=" + value
83 + " to system properties, from " + additionalSystemPropertiesPath.getFileName());
84 }
85 }
86 ThinLoggerFinder.reloadConfiguration();
87 }
88 }
89
90 Map<String, String> config = new HashMap<>();
91 config.put(PROP_ARGEO_INIT_MAIN, "true");
92
93 // add OSGi system properties to the configuration
94 sysprops: for (Object key : new TreeMap<>(System.getProperties()).keySet()) {
95 String keyStr = key.toString();
96 switch (keyStr) {
97 case InitConstants.PROP_OSGI_CONFIGURATION_AREA:
98 case InitConstants.PROP_OSGI_SHARED_CONFIGURATION_AREA:
99 case InitConstants.PROP_OSGI_INSTANCE_AREA:
100 // we should already have dealt with those
101 continue sysprops;
102 default:
103 }
104
105 if (keyStr.startsWith("osgi.") || keyStr.startsWith("org.osgi.") || keyStr.startsWith("eclipse.")
106 || keyStr.startsWith("org.eclipse.equinox.") || keyStr.startsWith("felix.")) {
107 String value = System.getProperty(keyStr);
108 config.put(keyStr, value);
109 logger.log(Logger.Level.TRACE,
110 () -> "Added " + key + "=" + value + " to configuration, from system properties");
111 }
112 }
113
114 try {
115 try {
116 if (stateArea != null)
117 config.put(InitConstants.PROP_OSGI_CONFIGURATION_AREA, stateArea);
118 if (configArea != null)
119 config.put(InitConstants.PROP_OSGI_SHARED_CONFIGURATION_AREA, configArea);
120 if (dataArea != null)
121 config.put(InitConstants.PROP_OSGI_INSTANCE_AREA, dataArea);
122 // config.put(OsgiBoot.PROP_OSGI_USE_SYSTEM_PROPERTIES, "true");
123
124 OsgiRuntimeContext osgiRuntimeContext = new OsgiRuntimeContext(config);
125 osgiRuntimeContext.run();
126 InternalState.setMainRuntimeContext(osgiRuntimeContext);
127 for (Runnable run : postStart) {
128 try {
129 run.run();
130 } catch (Exception e) {
131 logger.log(Level.ERROR, "Cannot run post start callback " + run, e);
132 }
133 }
134 InternalState.getMainRuntimeContext().waitForStop(0);
135 } catch (NoClassDefFoundError noClassDefFoundE) {
136 StaticRuntimeContext staticRuntimeContext = new StaticRuntimeContext((Map<String, String>) config);
137 staticRuntimeContext.run();
138 InternalState.setMainRuntimeContext(staticRuntimeContext);
139 for (Runnable run : postStart) {
140 try {
141 run.run();
142 } catch (Exception e) {
143 logger.log(Level.ERROR, "Cannot run post start callback " + run, e);
144 }
145 }
146 InternalState.getMainRuntimeContext().waitForStop(0);
147 }
148 } catch (Exception e) {
149 e.printStackTrace();
150 System.exit(1);
151 }
152 logger.log(Logger.Level.DEBUG, "Argeo Init stopped with PID " + pid);
153 }
154
155 /** Add a post-start call back to be run after the runtime has been started. */
156 public static void addPostStart(Runnable runnable) {
157 postStart.add(runnable);
158 }
159 }