1 package org
.argeo
.cms
.internal
.runtime
;
3 import static java
.util
.Locale
.ENGLISH
;
5 import java
.lang
.management
.ManagementFactory
;
6 import java
.util
.HashMap
;
8 import java
.util
.Locale
;
10 import java
.util
.UUID
;
11 import java
.util
.concurrent
.CompletableFuture
;
12 import java
.util
.concurrent
.ExecutionException
;
14 import javax
.security
.auth
.Subject
;
16 import org
.argeo
.api
.acr
.spi
.ProvidedContent
;
17 import org
.argeo
.api
.acr
.spi
.ProvidedRepository
;
18 import org
.argeo
.api
.cms
.CmsConstants
;
19 import org
.argeo
.api
.cms
.CmsContext
;
20 import org
.argeo
.api
.cms
.CmsDeployment
;
21 import org
.argeo
.api
.cms
.CmsLog
;
22 import org
.argeo
.api
.cms
.CmsSession
;
23 import org
.argeo
.api
.cms
.CmsSessionId
;
24 import org
.argeo
.api
.cms
.CmsState
;
25 import org
.argeo
.api
.uuid
.UuidFactory
;
26 import org
.argeo
.cms
.LocaleUtils
;
27 import org
.argeo
.cms
.internal
.auth
.CmsSessionImpl
;
28 import org
.ietf
.jgss
.GSSCredential
;
29 import org
.osgi
.service
.useradmin
.UserAdmin
;
31 public class CmsContextImpl
implements CmsContext
{
32 private final CmsLog log
= CmsLog
.getLog(getClass());
33 // private final BundleContext bc = FrameworkUtil.getBundle(getClass()).getBundleContext();
35 // private EgoRepository egoRepository;
36 private static CompletableFuture
<CmsContextImpl
> instance
= new CompletableFuture
<CmsContextImpl
>();
38 private CmsState cmsState
;
39 private CmsDeployment cmsDeployment
;
40 private UserAdmin userAdmin
;
41 private UuidFactory uuidFactory
;
42 // private ProvidedRepository contentRepository;
45 private Locale defaultLocale
;
46 private List
<Locale
> locales
= null;
48 private Long availableSince
;
51 private Map
<UUID
, CmsSessionImpl
> cmsSessionsByUuid
= new HashMap
<>();
52 private Map
<String
, CmsSessionImpl
> cmsSessionsByLocalId
= new HashMap
<>();
54 // public CmsContextImpl() {
59 Object defaultLocaleValue
= KernelUtils
.getFrameworkProp(CmsConstants
.I18N_DEFAULT_LOCALE
);
60 defaultLocale
= defaultLocaleValue
!= null ?
new Locale(defaultLocaleValue
.toString())
61 : new Locale(ENGLISH
.getLanguage());
62 locales
= LocaleUtils
.asLocaleList(KernelUtils
.getFrameworkProp(CmsConstants
.I18N_LOCALES
));
64 // new ServiceTracker<Repository, Repository>(bc, Repository.class, null) {
66 // public Repository addingService(ServiceReference<Repository> reference) {
67 // Object cn = reference.getProperty(NodeConstants.CN);
68 // if (cn != null && cn.equals(NodeConstants.EGO_REPOSITORY)) {
69 //// egoRepository = (EgoRepository) bc.getService(reference);
70 // if (log.isTraceEnabled())
71 // log.trace("Home repository is available");
73 // return super.addingService(reference);
77 // public void removedService(ServiceReference<Repository> reference, Repository service) {
78 // super.removedService(reference, service);
79 //// egoRepository = null;
94 * Checks whether the deployment is available according to expectations, and
95 * mark it as available.
97 private void checkReadiness() {
100 if (cmsDeployment
!= null && userAdmin
!= null) {
101 String data
= KernelUtils
.getFrameworkProp(KernelUtils
.OSGI_INSTANCE_AREA
);
102 String state
= KernelUtils
.getFrameworkProp(KernelUtils
.OSGI_CONFIGURATION_AREA
);
103 availableSince
= System
.currentTimeMillis();
104 long jvmUptime
= ManagementFactory
.getRuntimeMXBean().getUptime();
105 String jvmUptimeStr
= " in " + (jvmUptime
/ 1000) + "." + (jvmUptime
% 1000) + "s";
106 log
.info("## ARGEO CMS AVAILABLE" + (log
.isDebugEnabled() ? jvmUptimeStr
: "") + " ##");
107 if (log
.isDebugEnabled()) {
108 log
.debug("## state: " + state
);
110 log
.debug("## data: " + data
);
112 long begin
= cmsState
.getAvailableSince();
113 long initDuration
= System
.currentTimeMillis() - begin
;
114 if (log
.isTraceEnabled())
115 log
.trace("Kernel initialization took " + initDuration
+ "ms");
116 tributeToFreeSoftware(initDuration
);
118 throw new IllegalStateException("Deployment is not available");
122 final private void tributeToFreeSoftware(long initDuration
) {
123 if (log
.isTraceEnabled()) {
124 long ms
= initDuration
/ 100;
125 log
.trace("Spend " + ms
+ "ms" + " reflecting on the progress brought to mankind" + " by Free Software...");
126 long beginNano
= System
.nanoTime();
129 } catch (InterruptedException e
) {
132 long durationNano
= System
.nanoTime() - beginNano
;
133 final double M
= 1000d
* 1000d
;
134 double sleepAccuracy
= ((double) durationNano
) / (ms
* M
);
135 log
.trace("Sleep accuracy: " + String
.format("%.2f", 100 - (sleepAccuracy
* 100 - 100)) + " %");
140 public void createWorkgroup(String dn
) {
141 // if (egoRepository == null)
142 // throw new CmsException("Ego repository is not available");
143 // // TODO add check that the group exists
144 // egoRepository.createWorkgroup(dn);
145 throw new UnsupportedOperationException();
148 public void setCmsDeployment(CmsDeployment cmsDeployment
) {
149 this.cmsDeployment
= cmsDeployment
;
152 public void setCmsState(CmsState cmsState
) {
153 this.cmsState
= cmsState
;
156 public void setUserAdmin(UserAdmin userAdmin
) {
157 this.userAdmin
= userAdmin
;
160 public UuidFactory
getUuidFactory() {
164 public void setUuidFactory(UuidFactory uuidFactory
) {
165 this.uuidFactory
= uuidFactory
;
168 // public ProvidedRepository getContentRepository() {
169 // return contentRepository;
172 // public void setContentRepository(ProvidedRepository contentRepository) {
173 // this.contentRepository = contentRepository;
177 public Locale
getDefaultLocale() {
178 return defaultLocale
;
182 public List
<Locale
> getLocales() {
187 public synchronized Long
getAvailableSince() {
188 return availableSince
;
191 public synchronized boolean isAvailable() {
192 return availableSince
!= null;
196 public CmsState
getCmsState() {
204 public synchronized static CmsContextImpl
getCmsContext() {
205 return getInstance();
208 // /** Required by USER login module. */
209 // public synchronized static UserAdmin getUserAdmin() {
210 // return getInstance().userAdmin;
213 /** Required by SPNEGO login module. */
215 public synchronized static GSSCredential
getAcceptorCredentials() {
216 // FIXME find a cleaner way
217 return ((CmsUserAdmin
) getInstance().userAdmin
).getAcceptorCredentials();
220 private synchronized static void setInstance(CmsContextImpl cmsContextImpl
) {
221 if (cmsContextImpl
!= null) {
222 if (instance
.isDone())
223 throw new IllegalStateException("CMS Context is already set");
224 instance
.complete(cmsContextImpl
);
226 instance
= new CompletableFuture
<CmsContextImpl
>();
230 private synchronized static CmsContextImpl
getInstance() {
232 return instance
.get();
233 } catch (InterruptedException
| ExecutionException e
) {
234 throw new IllegalStateException("Cannot retrieve CMS Context", e
);
238 public UserAdmin
getUserAdmin() {
247 public synchronized CmsSession
getCmsSession(Subject subject
) {
248 if (subject
.getPrivateCredentials(CmsSessionId
.class).isEmpty())
250 CmsSessionId cmsSessionId
= subject
.getPrivateCredentials(CmsSessionId
.class).iterator().next();
251 return getCmsSessionByUuid(cmsSessionId
.getUuid());
254 public synchronized void registerCmsSession(CmsSessionImpl cmsSession
) {
255 if (cmsSessionsByUuid
.containsKey(cmsSession
.getUuid())
256 || cmsSessionsByLocalId
.containsKey(cmsSession
.getLocalId()))
257 throw new IllegalStateException("CMS session " + cmsSession
+ " is already registered.");
258 cmsSessionsByUuid
.put(cmsSession
.getUuid(), cmsSession
);
259 cmsSessionsByLocalId
.put(cmsSession
.getLocalId(), cmsSession
);
262 public synchronized void unregisterCmsSession(CmsSessionImpl cmsSession
) {
263 if (!cmsSessionsByUuid
.containsKey(cmsSession
.getUuid())
264 || !cmsSessionsByLocalId
.containsKey(cmsSession
.getLocalId()))
265 throw new IllegalStateException("CMS session " + cmsSession
+ " is not registered.");
266 CmsSession removed
= cmsSessionsByUuid
.remove(cmsSession
.getUuid());
267 assert removed
== cmsSession
;
268 cmsSessionsByLocalId
.remove(cmsSession
.getLocalId());
272 * The {@link CmsSession} related to this UUID, or <code>null</null> if not
275 public synchronized CmsSessionImpl
getCmsSessionByUuid(UUID uuid
) {
276 return cmsSessionsByUuid
.get(uuid
);
280 * The {@link CmsSession} related to this local id, or <code>null</null> if not
283 public synchronized CmsSessionImpl
getCmsSessionByLocalId(String localId
) {
284 return cmsSessionsByLocalId
.get(localId
);