]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/runtime/CmsContextImpl.java
Introduce CMS UX
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / runtime / CmsContextImpl.java
1 package org.argeo.cms.internal.runtime;
2
3 import static java.util.Locale.ENGLISH;
4
5 import java.lang.management.ManagementFactory;
6 import java.util.List;
7 import java.util.Locale;
8 import java.util.concurrent.CompletableFuture;
9 import java.util.concurrent.ExecutionException;
10
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;
20
21 public class CmsContextImpl implements CmsContext {
22 private final CmsLog log = CmsLog.getLog(getClass());
23 // private final BundleContext bc = FrameworkUtil.getBundle(getClass()).getBundleContext();
24
25 // private EgoRepository egoRepository;
26 private static CompletableFuture<CmsContextImpl> instance = new CompletableFuture<CmsContextImpl>();
27
28 private CmsState cmsState;
29 private CmsDeployment cmsDeployment;
30 private UserAdmin userAdmin;
31
32 // i18n
33 private Locale defaultLocale;
34 private List<Locale> locales = null;
35
36 private Long availableSince;
37
38 // public CmsContextImpl() {
39 // initTrackers();
40 // }
41
42 public void start() {
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));
47 // node repository
48 // new ServiceTracker<Repository, Repository>(bc, Repository.class, null) {
49 // @Override
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");
56 // }
57 // return super.addingService(reference);
58 // }
59 //
60 // @Override
61 // public void removedService(ServiceReference<Repository> reference, Repository service) {
62 // super.removedService(reference, service);
63 //// egoRepository = null;
64 // }
65 //
66 // }.open();
67
68 checkReadiness();
69
70 setInstance(this);
71 }
72
73 public void stop() {
74 setInstance(null);
75 }
76
77 /**
78 * Checks whether the deployment is available according to expectations, and
79 * mark it as available.
80 */
81 private void checkReadiness() {
82 if (isAvailable())
83 return;
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);
93 if (data != null)
94 log.debug("## data: " + data);
95 }
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);
101 } else {
102 throw new IllegalStateException("Deployment is not available");
103 }
104 }
105
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();
111 try {
112 Thread.sleep(ms, 0);
113 } catch (InterruptedException e) {
114 // silent
115 }
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)) + " %");
120 }
121 }
122
123 @Override
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();
130 }
131
132 public void setCmsDeployment(CmsDeployment cmsDeployment) {
133 this.cmsDeployment = cmsDeployment;
134 }
135
136 public void setCmsState(CmsState cmsState) {
137 this.cmsState = cmsState;
138 }
139
140 public void setUserAdmin(UserAdmin userAdmin) {
141 this.userAdmin = userAdmin;
142 }
143
144 @Override
145 public Locale getDefaultLocale() {
146 return defaultLocale;
147 }
148
149 @Override
150 public List<Locale> getLocales() {
151 return locales;
152 }
153
154 @Override
155 public synchronized Long getAvailableSince() {
156 return availableSince;
157 }
158
159 public synchronized boolean isAvailable() {
160 return availableSince != null;
161 }
162
163 /*
164 * STATIC
165 */
166
167 public synchronized static CmsContext getCmsContext() {
168 return getInstance();
169 }
170
171 /** Required by USER login module. */
172 public synchronized static UserAdmin getUserAdmin() {
173 return getInstance().userAdmin;
174 }
175
176 /** Required by SPNEGO login module. */
177 @Deprecated
178 public synchronized static GSSCredential getAcceptorCredentials() {
179 // FIXME find a cleaner way
180 return ((NodeUserAdmin) getInstance().userAdmin).getAcceptorCredentials();
181 }
182
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);
188 } else {
189 instance = new CompletableFuture<CmsContextImpl>();
190 }
191 }
192
193 private synchronized static CmsContextImpl getInstance() {
194 try {
195 return instance.get();
196 } catch (InterruptedException | ExecutionException e) {
197 throw new IllegalStateException("Cannot retrieve CMS Context", e);
198 }
199 }
200
201 }