1 package org
.argeo
.cms
.internal
.runtime
;
3 import static java
.util
.Locale
.ENGLISH
;
5 import java
.lang
.management
.ManagementFactory
;
7 import java
.util
.Locale
;
8 import java
.util
.concurrent
.CompletableFuture
;
9 import java
.util
.concurrent
.ExecutionException
;
11 import org
.argeo
.api
.cms
.CmsConstants
;
12 import org
.argeo
.api
.cms
.CmsContext
;
13 import org
.argeo
.api
.cms
.CmsDeployment
;
14 import org
.argeo
.api
.cms
.CmsLog
;
15 import org
.argeo
.api
.cms
.CmsState
;
16 import org
.argeo
.cms
.LocaleUtils
;
17 import org
.argeo
.cms
.internal
.osgi
.NodeUserAdmin
;
18 import org
.ietf
.jgss
.GSSCredential
;
19 import org
.osgi
.service
.useradmin
.UserAdmin
;
21 public class CmsContextImpl
implements CmsContext
{
22 private final CmsLog log
= CmsLog
.getLog(getClass());
23 // private final BundleContext bc = FrameworkUtil.getBundle(getClass()).getBundleContext();
25 // private EgoRepository egoRepository;
26 private static CompletableFuture
<CmsContextImpl
> instance
= new CompletableFuture
<CmsContextImpl
>();
28 private CmsState cmsState
;
29 private CmsDeployment cmsDeployment
;
30 private UserAdmin userAdmin
;
33 private Locale defaultLocale
;
34 private List
<Locale
> locales
= null;
36 private Long availableSince
;
38 // public CmsContextImpl() {
43 Object defaultLocaleValue
= KernelUtils
.getFrameworkProp(CmsConstants
.I18N_DEFAULT_LOCALE
);
44 defaultLocale
= defaultLocaleValue
!= null ?
new Locale(defaultLocaleValue
.toString())
45 : new Locale(ENGLISH
.getLanguage());
46 locales
= LocaleUtils
.asLocaleList(KernelUtils
.getFrameworkProp(CmsConstants
.I18N_LOCALES
));
48 // new ServiceTracker<Repository, Repository>(bc, Repository.class, null) {
50 // public Repository addingService(ServiceReference<Repository> reference) {
51 // Object cn = reference.getProperty(NodeConstants.CN);
52 // if (cn != null && cn.equals(NodeConstants.EGO_REPOSITORY)) {
53 //// egoRepository = (EgoRepository) bc.getService(reference);
54 // if (log.isTraceEnabled())
55 // log.trace("Home repository is available");
57 // return super.addingService(reference);
61 // public void removedService(ServiceReference<Repository> reference, Repository service) {
62 // super.removedService(reference, service);
63 //// egoRepository = null;
73 public void destroy() {
78 * Checks whether the deployment is available according to expectations, and
79 * mark it as available.
81 private void checkReadiness() {
84 if (cmsDeployment
!= null && userAdmin
!= null) {
85 String data
= KernelUtils
.getFrameworkProp(KernelUtils
.OSGI_INSTANCE_AREA
);
86 String state
= KernelUtils
.getFrameworkProp(KernelUtils
.OSGI_CONFIGURATION_AREA
);
87 availableSince
= System
.currentTimeMillis();
88 long jvmUptime
= ManagementFactory
.getRuntimeMXBean().getUptime();
89 String jvmUptimeStr
= " in " + (jvmUptime
/ 1000) + "." + (jvmUptime
% 1000) + "s";
90 log
.info("## ARGEO CMS AVAILABLE" + (log
.isDebugEnabled() ? jvmUptimeStr
: "") + " ##");
91 if (log
.isDebugEnabled()) {
92 log
.debug("## state: " + state
);
94 log
.debug("## data: " + data
);
96 long begin
= cmsState
.getAvailableSince();
97 long initDuration
= System
.currentTimeMillis() - begin
;
98 if (log
.isTraceEnabled())
99 log
.trace("Kernel initialization took " + initDuration
+ "ms");
100 tributeToFreeSoftware(initDuration
);
102 throw new IllegalStateException("Deployment is not available");
106 final private void tributeToFreeSoftware(long initDuration
) {
107 if (log
.isTraceEnabled()) {
108 long ms
= initDuration
/ 100;
109 log
.trace("Spend " + ms
+ "ms" + " reflecting on the progress brought to mankind" + " by Free Software...");
110 long beginNano
= System
.nanoTime();
113 } catch (InterruptedException e
) {
116 long durationNano
= System
.nanoTime() - beginNano
;
117 final double M
= 1000d
* 1000d
;
118 double sleepAccuracy
= ((double) durationNano
) / (ms
* M
);
119 log
.trace("Sleep accuracy: " + String
.format("%.2f", 100 - (sleepAccuracy
* 100 - 100)) + " %");
124 public void createWorkgroup(String dn
) {
125 // if (egoRepository == null)
126 // throw new CmsException("Ego repository is not available");
127 // // TODO add check that the group exists
128 // egoRepository.createWorkgroup(dn);
129 throw new UnsupportedOperationException();
132 public void setCmsDeployment(CmsDeployment cmsDeployment
) {
133 this.cmsDeployment
= cmsDeployment
;
136 public void setCmsState(CmsState cmsState
) {
137 this.cmsState
= cmsState
;
140 public void setUserAdmin(UserAdmin userAdmin
) {
141 this.userAdmin
= userAdmin
;
145 public Locale
getDefaultLocale() {
146 return defaultLocale
;
150 public List
<Locale
> getLocales() {
155 public synchronized Long
getAvailableSince() {
156 return availableSince
;
159 public synchronized boolean isAvailable() {
160 return availableSince
!= null;
167 public synchronized static CmsContext
getCmsContext() {
168 return getInstance();
171 /** Required by USER login module. */
172 public synchronized static UserAdmin
getUserAdmin() {
173 return getInstance().userAdmin
;
176 /** Required by SPNEGO login module. */
178 public synchronized static GSSCredential
getAcceptorCredentials() {
179 // FIXME find a cleaner way
180 return ((NodeUserAdmin
) getInstance().userAdmin
).getAcceptorCredentials();
183 private synchronized static void setInstance(CmsContextImpl cmsContextImpl
) {
184 if (cmsContextImpl
!= null) {
185 if (instance
.isDone())
186 throw new IllegalStateException("CMS Context is already set");
187 instance
.complete(cmsContextImpl
);
189 instance
= new CompletableFuture
<CmsContextImpl
>();
193 private synchronized static CmsContextImpl
getInstance() {
195 return instance
.get();
196 } catch (InterruptedException
| ExecutionException e
) {
197 throw new IllegalStateException("Cannot retrieve CMS Context", e
);