]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/kernel/CmsDeployment.java
7a180cfc5287adf5612ea3deaaf9d65162d8a112
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / kernel / CmsDeployment.java
1 package org.argeo.cms.internal.kernel;
2
3 import static org.argeo.node.DataModelNamespace.CMS_DATA_MODEL_NAMESPACE;
4
5 import java.io.InputStreamReader;
6 import java.io.Reader;
7 import java.lang.management.ManagementFactory;
8 import java.net.URL;
9 import java.util.HashSet;
10 import java.util.Hashtable;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Set;
14
15 import javax.jcr.Repository;
16 import javax.jcr.Session;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.apache.jackrabbit.commons.cnd.CndImporter;
21 import org.apache.jackrabbit.core.RepositoryContext;
22 import org.argeo.cms.CmsException;
23 import org.argeo.jcr.ArgeoJcrConstants;
24 import org.argeo.jcr.JcrUtils;
25 import org.argeo.node.DataModelNamespace;
26 import org.argeo.node.NodeConstants;
27 import org.argeo.node.NodeDeployment;
28 import org.argeo.node.NodeState;
29 import org.osgi.framework.Bundle;
30 import org.osgi.framework.BundleContext;
31 import org.osgi.framework.Constants;
32 import org.osgi.framework.FrameworkUtil;
33 import org.osgi.framework.ServiceReference;
34 import org.osgi.framework.wiring.BundleCapability;
35 import org.osgi.framework.wiring.BundleWire;
36 import org.osgi.framework.wiring.BundleWiring;
37 import org.osgi.service.cm.ConfigurationAdmin;
38 import org.osgi.service.http.HttpService;
39 import org.osgi.service.useradmin.UserAdmin;
40 import org.osgi.util.tracker.ServiceTracker;
41
42 public class CmsDeployment implements NodeDeployment {
43 private final Log log = LogFactory.getLog(getClass());
44 private final BundleContext bc = FrameworkUtil.getBundle(getClass()).getBundleContext();
45
46 private DeployConfig deployConfig;
47 private HomeRepository homeRepository;
48
49 private Long availableSince;
50
51 private final boolean cleanState;
52 // Readiness
53 private boolean nodeAvailable = false;
54 private boolean userAdminAvailable = false;
55 private boolean httpExpected = false;
56 private boolean httpAvailable = false;
57
58 public CmsDeployment() {
59 ServiceReference<NodeState> nodeStateSr = bc.getServiceReference(NodeState.class);
60 if (nodeStateSr == null)
61 throw new CmsException("No node state available");
62
63 NodeState nodeState = bc.getService(nodeStateSr);
64 cleanState = nodeState.isClean();
65
66 initTrackers();
67 }
68
69 private void initTrackers() {
70 new PrepareHttpStc().open();
71 new RepositoryContextStc().open();
72 new ServiceTracker<UserAdmin, UserAdmin>(bc, UserAdmin.class, null) {
73 @Override
74 public UserAdmin addingService(ServiceReference<UserAdmin> reference) {
75 userAdminAvailable = true;
76 checkReadiness();
77 return super.addingService(reference);
78 }
79 }.open();
80 new ServiceTracker<ConfigurationAdmin, ConfigurationAdmin>(bc, ConfigurationAdmin.class, null) {
81 @Override
82 public ConfigurationAdmin addingService(ServiceReference<ConfigurationAdmin> reference) {
83 ConfigurationAdmin configurationAdmin = bc.getService(reference);
84 deployConfig = new DeployConfig(configurationAdmin, cleanState);
85 httpExpected = deployConfig.getProps(KernelConstants.JETTY_FACTORY_PID, "default") != null;
86 return super.addingService(reference);
87 }
88 }.open();
89 }
90
91 public void shutdown() {
92 if (deployConfig != null)
93 deployConfig.save();
94 }
95
96 private void checkReadiness() {
97 if (nodeAvailable && userAdminAvailable && (httpExpected ? httpAvailable : true)) {
98 availableSince = System.currentTimeMillis();
99 long jvmUptime = ManagementFactory.getRuntimeMXBean().getUptime();
100 log.info("## ARGEO CMS AVAILABLE in " + (jvmUptime / 1000) + "." + (jvmUptime % 1000) + "s ##");
101 long begin = bc.getService(bc.getServiceReference(NodeState.class)).getAvailableSince();
102 long initDuration = System.currentTimeMillis() - begin;
103 if (log.isTraceEnabled())
104 log.trace("Kernel initialization took " + initDuration + "ms");
105 directorsCut(initDuration);
106 }
107 }
108
109 final private void directorsCut(long initDuration) {
110 // final long ms = 128l + (long) (Math.random() * 128d);
111 long ms = initDuration / 100;
112 log.info("Spend " + ms + "ms" + " reflecting on the progress brought to mankind" + " by Free Software...");
113 long beginNano = System.nanoTime();
114 try {
115 Thread.sleep(ms, 0);
116 } catch (InterruptedException e) {
117 // silent
118 }
119 long durationNano = System.nanoTime() - beginNano;
120 final double M = 1000d * 1000d;
121 double sleepAccuracy = ((double) durationNano) / (ms * M);
122 if (log.isDebugEnabled())
123 log.debug("Sleep accuracy: " + String.format("%.2f", 100 - (sleepAccuracy * 100 - 100)) + " %");
124 }
125
126 private void prepareNodeRepository(Repository deployedNodeRepository) {
127 if (availableSince != null) {
128 throw new CmsException("Deployment is already available");
129 }
130
131 prepareDataModel(KernelUtils.openAdminSession(deployedNodeRepository));
132 Hashtable<String, String> regProps = new Hashtable<String, String>();
133 regProps.put(NodeConstants.CN, ArgeoJcrConstants.ALIAS_HOME);
134 regProps.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS, ArgeoJcrConstants.ALIAS_HOME);
135 homeRepository = new HomeRepository(deployedNodeRepository);
136 // register
137 bc.registerService(Repository.class, homeRepository, regProps);
138 }
139
140 /** Session is logged out. */
141 private void prepareDataModel(Session adminSession) {
142 try {
143 Set<String> processed = new HashSet<String>();
144 bundles: for (Bundle bundle : bc.getBundles()) {
145 BundleWiring wiring = bundle.adapt(BundleWiring.class);
146 if (wiring == null) {
147 if (log.isTraceEnabled())
148 log.error("No wiring for " + bundle.getSymbolicName());
149 continue bundles;
150 }
151 processWiring(adminSession, wiring, processed);
152 }
153 } finally {
154 JcrUtils.logoutQuietly(adminSession);
155 }
156 }
157
158 private void processWiring(Session adminSession, BundleWiring wiring, Set<String> processed) {
159 // recursively process requirements first
160 List<BundleWire> requiredWires = wiring.getRequiredWires(CMS_DATA_MODEL_NAMESPACE);
161 for (BundleWire wire : requiredWires) {
162 processWiring(adminSession, wire.getProviderWiring(), processed);
163 // registerCnd(adminSession, wire.getCapability(), processed);
164 }
165 List<BundleCapability> capabilities = wiring.getCapabilities(CMS_DATA_MODEL_NAMESPACE);
166 for (BundleCapability capability : capabilities) {
167 registerCnd(adminSession, capability, processed);
168 }
169 }
170
171 private void registerCnd(Session adminSession, BundleCapability capability, Set<String> processed) {
172 Map<String, Object> attrs = capability.getAttributes();
173 String name = attrs.get(DataModelNamespace.CAPABILITY_NAME_ATTRIBUTE).toString();
174 if (processed.contains(name)) {
175 if (log.isTraceEnabled())
176 log.trace("Data model " + name + " has already been processed");
177 return;
178 }
179 String path = attrs.get(DataModelNamespace.CAPABILITY_CND_ATTRIBUTE).toString();
180 URL url = capability.getRevision().getBundle().getResource(path);
181 try (Reader reader = new InputStreamReader(url.openStream())) {
182 CndImporter.registerNodeTypes(reader, adminSession, true);
183 processed.add(name);
184 if (log.isDebugEnabled())
185 log.debug("Registered CND " + url);
186 } catch (Exception e) {
187 throw new CmsException("Cannot import CND " + url, e);
188 }
189
190 Hashtable<String, Object> properties = new Hashtable<>();
191 properties.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS, name);
192 properties.put(NodeConstants.CN, name);
193 if (name.equals(ArgeoJcrConstants.ALIAS_NODE))
194 properties.put(Constants.SERVICE_RANKING, Integer.MAX_VALUE);
195 bc.registerService(Repository.class, adminSession.getRepository(), properties);
196 if (log.isDebugEnabled())
197 log.debug("Published data model " + name);
198 }
199
200 @Override
201 public Long getAvailableSince() {
202 return availableSince;
203 }
204
205 private class RepositoryContextStc extends ServiceTracker<RepositoryContext, RepositoryContext> {
206
207 public RepositoryContextStc() {
208 super(bc, RepositoryContext.class, null);
209 }
210
211 @Override
212 public RepositoryContext addingService(ServiceReference<RepositoryContext> reference) {
213 RepositoryContext nodeRepo = bc.getService(reference);
214 Object cn = reference.getProperty(NodeConstants.CN);
215 if (cn != null && cn.equals(ArgeoJcrConstants.ALIAS_NODE)) {
216 prepareNodeRepository(nodeRepo.getRepository());
217 nodeAvailable = true;
218 checkReadiness();
219 }
220 return nodeRepo;
221 }
222
223 @Override
224 public void modifiedService(ServiceReference<RepositoryContext> reference, RepositoryContext service) {
225 }
226
227 @Override
228 public void removedService(ServiceReference<RepositoryContext> reference, RepositoryContext service) {
229 }
230
231 }
232
233 private class PrepareHttpStc extends ServiceTracker<HttpService, HttpService> {
234 private DataHttp dataHttp;
235 private NodeHttp nodeHttp;
236
237 public PrepareHttpStc() {
238 super(bc, HttpService.class, null);
239 }
240
241 @Override
242 public HttpService addingService(ServiceReference<HttpService> reference) {
243 HttpService httpService = addHttpService(reference);
244 return httpService;
245 }
246
247 @Override
248 public void removedService(ServiceReference<HttpService> reference, HttpService service) {
249 if (dataHttp != null)
250 dataHttp.destroy();
251 dataHttp = null;
252 if (nodeHttp != null)
253 nodeHttp.destroy();
254 nodeHttp = null;
255 }
256
257 private HttpService addHttpService(ServiceReference<HttpService> sr) {
258 HttpService httpService = bc.getService(sr);
259 // TODO find constants
260 Object httpPort = sr.getProperty("http.port");
261 Object httpsPort = sr.getProperty("https.port");
262 dataHttp = new DataHttp(httpService);
263 nodeHttp = new NodeHttp(httpService, bc);
264 log.info(httpPortsMsg(httpPort, httpsPort));
265 httpAvailable = true;
266 checkReadiness();
267 return httpService;
268 }
269
270 private String httpPortsMsg(Object httpPort, Object httpsPort) {
271 return "HTTP " + httpPort + (httpsPort != null ? " - HTTPS " + httpsPort : "");
272 }
273 }
274
275 }