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