]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/runtime/CmsStateImpl.java
Introduce CMS UX
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / runtime / CmsStateImpl.java
1 package org.argeo.cms.internal.runtime;
2
3 import java.net.InetAddress;
4 import java.net.URL;
5 import java.net.UnknownHostException;
6
7 import javax.security.auth.login.Configuration;
8
9 import org.argeo.api.cms.CmsLog;
10 import org.argeo.api.cms.CmsState;
11 import org.argeo.cms.auth.ident.IdentClient;
12 import org.osgi.framework.Constants;
13
14 /**
15 * Implementation of a {@link CmsState}, initialising the required services.
16 */
17 public class CmsStateImpl implements CmsState {
18 private final static CmsLog log = CmsLog.getLog(CmsStateImpl.class);
19
20 // REFERENCES
21 private Long availableSince;
22
23 private String stateUuid;
24 // private final boolean cleanState;
25 private String hostname;
26
27 public void start() {
28 // Runtime.getRuntime().addShutdownHook(new CmsShutdown());
29
30 try {
31 initSecurity();
32 // initArgeoLogger();
33
34 if (log.isTraceEnabled())
35 log.trace("CMS State started");
36
37 this.stateUuid = KernelUtils.getFrameworkProp(Constants.FRAMEWORK_UUID);
38 // this.cleanState = stateUuid.equals(frameworkUuid);
39 try {
40 this.hostname = InetAddress.getLocalHost().getHostName();
41 } catch (UnknownHostException e) {
42 log.error("Cannot set hostname: " + e);
43 }
44
45 availableSince = System.currentTimeMillis();
46 if (log.isDebugEnabled())
47 // log.debug("## CMS starting... stateUuid=" + this.stateUuid + (cleanState ? "
48 // (clean state) " : " "));
49 log.debug("## CMS starting... (" + stateUuid + ")");
50
51 // initI18n();
52 // initServices();
53
54 } catch (RuntimeException e) {
55 log.error("## FATAL: CMS activator failed", e);
56 }
57 }
58
59 private void initSecurity() {
60 if (System.getProperty(KernelConstants.JAAS_CONFIG_PROP) == null) {
61 String jaasConfig = KernelConstants.JAAS_CONFIG;
62 URL url = getClass().getResource(jaasConfig);
63 // System.setProperty(KernelConstants.JAAS_CONFIG_PROP,
64 // url.toExternalForm());
65 KernelUtils.setJaasConfiguration(url);
66 }
67 // explicitly load JAAS configuration
68 Configuration.getConfiguration();
69 }
70
71 public void stop() {
72 if (log.isDebugEnabled())
73 log.debug("CMS stopping... (" + this.stateUuid + ")");
74
75 long duration = ((System.currentTimeMillis() - availableSince) / 1000) / 60;
76 log.info("## ARGEO CMS STOPPED after " + (duration / 60) + "h " + (duration % 60) + "min uptime ##");
77 }
78
79
80 @Override
81 public Long getAvailableSince() {
82 return availableSince;
83 }
84
85 /*
86 * ACCESSORS
87 */
88 public String getHostname() {
89 return hostname;
90 }
91
92 /*
93 * STATIC
94 */
95 public static IdentClient getIdentClient(String remoteAddr) {
96 if (!IdentClient.isDefaultAuthdPassphraseFileAvailable())
97 return null;
98 // TODO make passphrase more configurable
99 return new IdentClient(remoteAddr);
100 }
101 }