1 package org
.argeo
.cms
.internal
.runtime
;
3 import java
.lang
.management
.ManagementFactory
;
4 import java
.util
.ArrayList
;
5 import java
.util
.HashMap
;
7 import java
.util
.Locale
;
10 import java
.util
.concurrent
.CompletableFuture
;
11 import java
.util
.concurrent
.ExecutionException
;
13 import javax
.security
.auth
.Subject
;
15 import org
.argeo
.api
.cms
.CmsContext
;
16 import org
.argeo
.api
.cms
.CmsDeployment
;
17 import org
.argeo
.api
.cms
.CmsEventBus
;
18 import org
.argeo
.api
.cms
.CmsLog
;
19 import org
.argeo
.api
.cms
.CmsSession
;
20 import org
.argeo
.api
.cms
.CmsSessionId
;
21 import org
.argeo
.api
.cms
.CmsState
;
22 import org
.argeo
.api
.uuid
.UuidFactory
;
23 import org
.argeo
.cms
.CmsDeployProperty
;
24 import org
.argeo
.cms
.internal
.auth
.CmsSessionImpl
;
25 import org
.ietf
.jgss
.GSSCredential
;
26 import org
.osgi
.service
.useradmin
.UserAdmin
;
28 /** Reference implementation of {@link CmsContext}. */
29 public class CmsContextImpl
implements CmsContext
{
31 private final CmsLog log
= CmsLog
.getLog(getClass());
33 private static CompletableFuture
<CmsContextImpl
> instance
= new CompletableFuture
<CmsContextImpl
>();
35 private CmsState cmsState
;
36 private CmsDeployment cmsDeployment
;
37 private UserAdmin userAdmin
;
38 private UuidFactory uuidFactory
;
39 private CmsEventBus cmsEventBus
;
42 private Locale defaultLocale
;
43 private List
<Locale
> locales
= null;
45 private Long availableSince
;
47 // time in ms to wait for CMS to be ready
48 private final long readynessTimeout
= 30 * 1000;
51 private Map
<UUID
, CmsSessionImpl
> cmsSessionsByUuid
= new HashMap
<>();
52 private Map
<String
, CmsSessionImpl
> cmsSessionsByLocalId
= new HashMap
<>();
55 List
<String
> codes
= CmsStateImpl
.getDeployProperties(cmsState
, CmsDeployProperty
.LOCALE
);
56 locales
= getLocaleList(codes
);
57 if (locales
.size() == 0)
58 throw new IllegalStateException("At least one locale must be set");
59 defaultLocale
= locales
.get(0);
62 long begin
= System
.currentTimeMillis();
64 readyness
: while (!checkReadiness()) {
65 duration
= System
.currentTimeMillis() - begin
;
66 if (duration
> readynessTimeout
) {
67 log
.error("## CMS not ready after " + duration
+ " ms. Giving up checking.");
72 } catch (InterruptedException e
) {
75 }, "Check readiness").start();
84 * Checks whether the deployment is available according to expectations, and
85 * mark it as available.
87 private boolean checkReadiness() {
90 if (cmsDeployment
== null)
93 if (((CmsDeploymentImpl
) cmsDeployment
).allExpectedServicesAvailable() && userAdmin
!= null) {
94 String data
= KernelUtils
.getFrameworkProp(KernelUtils
.OSGI_INSTANCE_AREA
);
95 String state
= KernelUtils
.getFrameworkProp(KernelUtils
.OSGI_CONFIGURATION_AREA
);
96 availableSince
= System
.currentTimeMillis();
97 long jvmUptime
= ManagementFactory
.getRuntimeMXBean().getUptime();
98 String jvmUptimeStr
= " in " + (jvmUptime
/ 1000) + "." + (jvmUptime
% 1000) + "s";
99 log
.info("## ARGEO CMS " + cmsState
.getUuid() + " AVAILABLE" + (log
.isDebugEnabled() ? jvmUptimeStr
: "")
101 if (log
.isDebugEnabled()) {
102 log
.debug("## state: " + state
);
104 log
.debug("## data: " + data
);
106 long begin
= cmsState
.getAvailableSince();
107 long initDuration
= System
.currentTimeMillis() - begin
;
108 if (log
.isTraceEnabled())
109 log
.trace("Kernel initialization took " + initDuration
+ "ms");
110 tributeToFreeSoftware(initDuration
);
115 // throw new IllegalStateException("Deployment is not available");
119 final private void tributeToFreeSoftware(long initDuration
) {
120 if (log
.isTraceEnabled()) {
121 long ms
= initDuration
/ 100;
122 log
.trace("Spend " + ms
+ "ms" + " reflecting on the progress brought to mankind" + " by Free Software...");
123 long beginNano
= System
.nanoTime();
126 } catch (InterruptedException e
) {
129 long durationNano
= System
.nanoTime() - beginNano
;
130 final double M
= 1000d
* 1000d
;
131 double sleepAccuracy
= ((double) durationNano
) / (ms
* M
);
132 log
.trace("Sleep accuracy: " + String
.format("%.2f", 100 - (sleepAccuracy
* 100 - 100)) + " %");
137 public void createWorkgroup(String dn
) {
138 // if (egoRepository == null)
139 // throw new CmsException("Ego repository is not available");
140 // // TODO add check that the group exists
141 // egoRepository.createWorkgroup(dn);
142 throw new UnsupportedOperationException();
145 /** Returns null if argument is null. */
146 private static List
<Locale
> getLocaleList(List
<String
> codes
) {
149 ArrayList
<Locale
> availableLocales
= new ArrayList
<Locale
>();
150 for (String code
: codes
) {
153 // variant not supported
154 int indexUnd
= code
.indexOf("_");
157 String language
= code
.substring(0, indexUnd
);
158 String country
= code
.substring(indexUnd
+ 1);
159 locale
= new Locale(language
, country
);
161 locale
= new Locale(code
);
163 availableLocales
.add(locale
);
165 return availableLocales
;
168 public void setCmsDeployment(CmsDeployment cmsDeployment
) {
169 this.cmsDeployment
= cmsDeployment
;
172 public void setCmsState(CmsState cmsState
) {
173 this.cmsState
= cmsState
;
176 public void setUserAdmin(UserAdmin userAdmin
) {
177 this.userAdmin
= userAdmin
;
180 public UuidFactory
getUuidFactory() {
184 public void setUuidFactory(UuidFactory uuidFactory
) {
185 this.uuidFactory
= uuidFactory
;
189 public Locale
getDefaultLocale() {
190 return defaultLocale
;
194 public UUID
timeUUID() {
195 return uuidFactory
.timeUUID();
199 public List
<Locale
> getLocales() {
204 public Long
getAvailableSince() {
205 return availableSince
;
208 public boolean isAvailable() {
209 return availableSince
!= null;
212 public CmsState
getCmsState() {
217 public CmsEventBus
getCmsEventBus() {
221 public void setCmsEventBus(CmsEventBus cmsEventBus
) {
222 this.cmsEventBus
= cmsEventBus
;
229 public static CmsContextImpl
getCmsContext() {
230 return getInstance();
233 /** Required by SPNEGO login module. */
234 public GSSCredential
getAcceptorCredentials() {
235 // TODO find a cleaner way
236 return ((CmsUserAdmin
) userAdmin
).getAcceptorCredentials();
239 private static void setInstance(CmsContextImpl cmsContextImpl
) {
240 if (cmsContextImpl
!= null) {
241 if (instance
.isDone())
242 throw new IllegalStateException("CMS Context is already set");
243 instance
.complete(cmsContextImpl
);
245 if (!instance
.isDone())
246 instance
.cancel(true);
247 instance
= new CompletableFuture
<CmsContextImpl
>();
251 private static CmsContextImpl
getInstance() {
253 return instance
.get();
254 } catch (InterruptedException
| ExecutionException e
) {
255 throw new IllegalStateException("Cannot retrieve CMS Context", e
);
259 public UserAdmin
getUserAdmin() {
268 public CmsSession
getCmsSession(Subject subject
) {
269 if (subject
.getPrivateCredentials(CmsSessionId
.class).isEmpty())
271 CmsSessionId cmsSessionId
= subject
.getPrivateCredentials(CmsSessionId
.class).iterator().next();
272 return getCmsSessionByUuid(cmsSessionId
.getUuid());
275 public void registerCmsSession(CmsSessionImpl cmsSession
) {
276 if (cmsSessionsByUuid
.containsKey(cmsSession
.getUuid())
277 || cmsSessionsByLocalId
.containsKey(cmsSession
.getLocalId()))
278 throw new IllegalStateException("CMS session " + cmsSession
+ " is already registered.");
279 cmsSessionsByUuid
.put(cmsSession
.getUuid(), cmsSession
);
280 cmsSessionsByLocalId
.put(cmsSession
.getLocalId(), cmsSession
);
283 public void unregisterCmsSession(CmsSessionImpl cmsSession
) {
284 if (!cmsSessionsByUuid
.containsKey(cmsSession
.getUuid())
285 || !cmsSessionsByLocalId
.containsKey(cmsSession
.getLocalId()))
286 throw new IllegalStateException("CMS session " + cmsSession
+ " is not registered.");
287 CmsSession removed
= cmsSessionsByUuid
.remove(cmsSession
.getUuid());
288 assert removed
== cmsSession
;
289 cmsSessionsByLocalId
.remove(cmsSession
.getLocalId());
293 * The {@link CmsSession} related to this UUID, or <code>null</null> if not
296 public CmsSessionImpl
getCmsSessionByUuid(UUID uuid
) {
297 return cmsSessionsByUuid
.get(uuid
);
301 * The {@link CmsSession} related to this local id, or <code>null</null> if not
304 public CmsSessionImpl
getCmsSessionByLocalId(String localId
) {
305 return cmsSessionsByLocalId
.get(localId
);