]> git.argeo.org Git - lgpl/argeo-commons.git/blob - runtime/CmsStateImpl.java
Prepare next development cycle
[lgpl/argeo-commons.git] / runtime / CmsStateImpl.java
1 package org.argeo.cms.internal.runtime;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.InetAddress;
6 import java.net.URL;
7 import java.net.UnknownHostException;
8 import java.nio.file.Files;
9 import java.nio.file.Path;
10 import java.util.TreeMap;
11 import java.util.UUID;
12
13 import javax.security.auth.login.Configuration;
14
15 import org.argeo.api.cms.CmsConstants;
16 import org.argeo.api.cms.CmsLog;
17 import org.argeo.api.cms.CmsState;
18 import org.argeo.api.uuid.UuidFactory;
19 import org.argeo.cms.auth.ident.IdentClient;
20 import org.argeo.util.directory.ldap.LdifParser;
21
22 /**
23 * Implementation of a {@link CmsState}, initialising the required services.
24 */
25 public class CmsStateImpl implements CmsState {
26 private final static CmsLog log = CmsLog.getLog(CmsStateImpl.class);
27
28 // REFERENCES
29 private Long availableSince;
30
31 private UUID uuid;
32 // private final boolean cleanState;
33 private String hostname;
34
35 private UuidFactory uuidFactory;
36
37 public void start() {
38 // Runtime.getRuntime().addShutdownHook(new CmsShutdown());
39
40 try {
41 initSecurity();
42 // initArgeoLogger();
43
44 if (log.isTraceEnabled())
45 log.trace("CMS State started");
46
47 // String stateUuidStr = KernelUtils.getFrameworkProp(Constants.FRAMEWORK_UUID);
48 // this.uuid = UUID.fromString(stateUuidStr);
49 this.uuid = uuidFactory.timeUUID();
50 // this.cleanState = stateUuid.equals(frameworkUuid);
51 try {
52 this.hostname = InetAddress.getLocalHost().getHostName();
53 } catch (UnknownHostException e) {
54 log.error("Cannot set hostname: " + e);
55 }
56
57 availableSince = System.currentTimeMillis();
58 if (log.isDebugEnabled())
59 // log.debug("## CMS starting... stateUuid=" + this.stateUuid + (cleanState ? "
60 // (clean state) " : " "));
61 log.debug("## CMS starting... (" + uuid + ")");
62
63
64 // initI18n();
65 // initServices();
66 if (!Files.exists(getDataPath(CmsConstants.NODE))) {// first init
67 firstInit();
68 }
69
70 } catch (RuntimeException | IOException e) {
71 log.error("## FATAL: CMS activator failed", e);
72 }
73 }
74
75 private void initSecurity() {
76 if (System.getProperty(KernelConstants.JAAS_CONFIG_PROP) == null) {
77 String jaasConfig = KernelConstants.JAAS_CONFIG;
78 URL url = getClass().getResource(jaasConfig);
79 // System.setProperty(KernelConstants.JAAS_CONFIG_PROP,
80 // url.toExternalForm());
81 KernelUtils.setJaasConfiguration(url);
82 }
83 // explicitly load JAAS configuration
84 Configuration.getConfiguration();
85 }
86
87 public void stop() {
88 if (log.isDebugEnabled())
89 log.debug("CMS stopping... (" + this.uuid + ")");
90
91 long duration = ((System.currentTimeMillis() - availableSince) / 1000) / 60;
92 log.info("## ARGEO CMS STOPPED after " + (duration / 60) + "h " + (duration % 60) + "min uptime ##");
93 }
94
95 private void firstInit() throws IOException {
96 log.info("## FIRST INIT ##");
97 // FirstInit firstInit = new FirstInit();
98 InitUtils.prepareFirstInitInstanceArea();
99 }
100
101 @Override
102 public String getDeployProperty(String key) {
103 return KernelUtils.getFrameworkProp(key);
104 }
105
106 @Override
107 public Path getDataPath(String relativePath) {
108 return KernelUtils.getOsgiInstancePath(relativePath);
109 }
110
111 @Override
112 public Long getAvailableSince() {
113 return availableSince;
114 }
115
116 /*
117 * ACCESSORS
118 */
119 public String getHostname() {
120 return hostname;
121 }
122
123 @Override
124 public UUID getUuid() {
125 return uuid;
126 }
127
128 public void setUuidFactory(UuidFactory uuidFactory) {
129 this.uuidFactory = uuidFactory;
130 }
131
132 /*
133 * STATIC
134 */
135 public static IdentClient getIdentClient(String remoteAddr) {
136 if (!IdentClient.isDefaultAuthdPassphraseFileAvailable())
137 return null;
138 // TODO make passphrase more configurable
139 return new IdentClient(remoteAddr);
140 }
141 }