1 package org
.argeo
.init
;
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
;
11 import java
.util
.Objects
;
12 import java
.util
.Properties
;
14 import org
.argeo
.init
.logging
.ThinLoggerFinder
;
15 import org
.argeo
.init
.osgi
.OsgiBoot
;
16 import org
.argeo
.init
.osgi
.OsgiRuntimeContext
;
18 /** Configure and launch an Argeo service. */
19 public class Service
{
20 private final static Logger logger
= System
.getLogger(Service
.class.getName());
22 public final static String PROP_ARGEO_INIT_MAIN
= "argeo.init.main";
24 private static RuntimeContext runtimeContext
= null;
26 protected Service(String
[] args
) {
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
);
34 Runtime
.getRuntime().addShutdownHook(new Thread(() -> {
36 if (Service
.runtimeContext
!= null) {
37 // System.out.println("Argeo Init stopping with PID " + pid);
38 Service
.runtimeContext
.close();
39 Service
.runtimeContext
.waitForStop(0);
41 } catch (Exception e
) {
43 Runtime
.getRuntime().halt(1);
45 }, "Runtime shutdown"));
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
);
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
)) {
59 } catch (IOException e
) {
60 logger
.log(Logger
.Level
.ERROR
,
61 "Cannot load additional system properties " + additionalSystemPropertiesPath
, e
);
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.");
72 System
.setProperty(key
.toString(), value
);
75 ThinLoggerFinder
.reloadConfiguration();
79 Map
<String
, String
> config
= new HashMap
<>();
80 config
.put(PROP_ARGEO_INIT_MAIN
, "true");
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
);
89 config
.put(OsgiBoot
.PROP_OSGI_INSTANCE_AREA
, dataArea
);
90 // config.put(OsgiBoot.PROP_OSGI_USE_SYSTEM_PROPERTIES, "true");
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);
102 } catch (Exception e
) {
106 logger
.log(Logger
.Level
.DEBUG
, "Argeo Init stopped with PID " + pid
);
109 public static RuntimeContext
getRuntimeContext() {
110 return runtimeContext
;