]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/internal/CmsJcrDeployment.java
Prepare next development cycle
[lgpl/argeo-commons.git] / jcr / internal / CmsJcrDeployment.java
1 package org.argeo.cms.jcr.internal;
2
3 import static org.argeo.cms.osgi.DataModelNamespace.CMS_DATA_MODEL_NAMESPACE;
4 import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX;
5
6 import java.io.File;
7 import java.io.IOException;
8 import java.io.InputStreamReader;
9 import java.io.Reader;
10 import java.net.URL;
11 import java.nio.file.Files;
12 import java.nio.file.Path;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.Hashtable;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22
23 import javax.jcr.Repository;
24 import javax.jcr.RepositoryException;
25 import javax.jcr.Session;
26 import javax.security.auth.callback.CallbackHandler;
27 import javax.servlet.Servlet;
28
29 import org.apache.jackrabbit.commons.cnd.CndImporter;
30 import org.apache.jackrabbit.core.RepositoryContext;
31 import org.apache.jackrabbit.core.RepositoryImpl;
32 import org.argeo.api.cms.CmsConstants;
33 import org.argeo.api.cms.CmsDeployment;
34 import org.argeo.api.cms.CmsLog;
35 import org.argeo.cms.ArgeoNames;
36 import org.argeo.cms.internal.jcr.JcrInitUtils;
37 import org.argeo.cms.jcr.CmsJcrUtils;
38 import org.argeo.cms.jcr.internal.servlet.CmsRemotingServlet;
39 import org.argeo.cms.jcr.internal.servlet.CmsWebDavServlet;
40 import org.argeo.cms.jcr.internal.servlet.JcrHttpUtils;
41 import org.argeo.cms.osgi.DataModelNamespace;
42 import org.argeo.cms.security.CryptoKeyring;
43 import org.argeo.cms.security.Keyring;
44 import org.argeo.jcr.Jcr;
45 import org.argeo.jcr.JcrException;
46 import org.argeo.jcr.JcrUtils;
47 import org.argeo.util.LangUtils;
48 import org.argeo.util.naming.LdapAttrs;
49 import org.osgi.framework.Bundle;
50 import org.osgi.framework.BundleContext;
51 import org.osgi.framework.Constants;
52 import org.osgi.framework.FrameworkUtil;
53 import org.osgi.framework.InvalidSyntaxException;
54 import org.osgi.framework.ServiceReference;
55 import org.osgi.framework.wiring.BundleCapability;
56 import org.osgi.framework.wiring.BundleWire;
57 import org.osgi.framework.wiring.BundleWiring;
58 import org.osgi.service.cm.ManagedService;
59 import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
60 import org.osgi.util.tracker.ServiceTracker;
61
62 /** Implementation of a CMS deployment. */
63 public class CmsJcrDeployment {
64 private final CmsLog log = CmsLog.getLog(getClass());
65 private final BundleContext bc = FrameworkUtil.getBundle(getClass()).getBundleContext();
66
67 private DataModels dataModels;
68 private String webDavConfig = JcrHttpUtils.WEBDAV_CONFIG;
69
70 private boolean argeoDataModelExtensionsAvailable = false;
71
72 // Readiness
73 private boolean nodeAvailable = false;
74
75 CmsDeployment cmsDeployment;
76
77 public CmsJcrDeployment() {
78 // initTrackers();
79 }
80
81 public void start() {
82 dataModels = new DataModels(bc);
83
84 ServiceTracker<?, ?> repoContextSt = new RepositoryContextStc();
85 repoContextSt.open();
86 //KernelUtils.asyncOpen(repoContextSt);
87
88 // nodeDeployment = CmsJcrActivator.getService(NodeDeployment.class);
89
90 JcrInitUtils.addToDeployment(cmsDeployment);
91
92 }
93
94 public void stop() {
95 // if (nodeHttp != null)
96 // nodeHttp.destroy();
97
98 try {
99 for (ServiceReference<JackrabbitLocalRepository> sr : bc
100 .getServiceReferences(JackrabbitLocalRepository.class, null)) {
101 bc.getService(sr).destroy();
102 }
103 } catch (InvalidSyntaxException e1) {
104 log.error("Cannot clean repositories", e1);
105 }
106
107 }
108
109 public void setCmsDeployment(CmsDeployment cmsDeployment) {
110 this.cmsDeployment = cmsDeployment;
111 }
112
113 /**
114 * Checks whether the deployment is available according to expectations, and
115 * mark it as available.
116 */
117 // private synchronized void checkReadiness() {
118 // if (isAvailable())
119 // return;
120 // if (nodeAvailable && userAdminAvailable && (httpExpected ? httpAvailable : true)) {
121 // String data = KernelUtils.getFrameworkProp(KernelUtils.OSGI_INSTANCE_AREA);
122 // String state = KernelUtils.getFrameworkProp(KernelUtils.OSGI_CONFIGURATION_AREA);
123 // availableSince = System.currentTimeMillis();
124 // long jvmUptime = ManagementFactory.getRuntimeMXBean().getUptime();
125 // String jvmUptimeStr = " in " + (jvmUptime / 1000) + "." + (jvmUptime % 1000) + "s";
126 // log.info("## ARGEO NODE AVAILABLE" + (log.isDebugEnabled() ? jvmUptimeStr : "") + " ##");
127 // if (log.isDebugEnabled()) {
128 // log.debug("## state: " + state);
129 // if (data != null)
130 // log.debug("## data: " + data);
131 // }
132 // long begin = bc.getService(bc.getServiceReference(NodeState.class)).getAvailableSince();
133 // long initDuration = System.currentTimeMillis() - begin;
134 // if (log.isTraceEnabled())
135 // log.trace("Kernel initialization took " + initDuration + "ms");
136 // tributeToFreeSoftware(initDuration);
137 // }
138 // }
139
140 private void prepareNodeRepository(Repository deployedNodeRepository, List<String> publishAsLocalRepo) {
141 // if (availableSince != null) {
142 // throw new IllegalStateException("Deployment is already available");
143 // }
144
145 // home
146 prepareDataModel(CmsConstants.NODE_REPOSITORY, deployedNodeRepository, publishAsLocalRepo);
147
148 // init from backup
149 // if (deployConfig.isFirstInit()) {
150 // Path restorePath = Paths.get(System.getProperty("user.dir"), "restore");
151 // if (Files.exists(restorePath)) {
152 // if (log.isDebugEnabled())
153 // log.debug("Found backup " + restorePath + ", restoring it...");
154 // LogicalRestore logicalRestore = new LogicalRestore(bc, deployedNodeRepository, restorePath);
155 // KernelUtils.doAsDataAdmin(logicalRestore);
156 // log.info("Restored backup from " + restorePath);
157 // }
158 // }
159
160 // init from repository
161 Collection<ServiceReference<Repository>> initRepositorySr;
162 try {
163 initRepositorySr = bc.getServiceReferences(Repository.class,
164 "(" + CmsConstants.CN + "=" + CmsConstants.NODE_INIT + ")");
165 } catch (InvalidSyntaxException e1) {
166 throw new IllegalArgumentException(e1);
167 }
168 Iterator<ServiceReference<Repository>> it = initRepositorySr.iterator();
169 while (it.hasNext()) {
170 ServiceReference<Repository> sr = it.next();
171 Object labeledUri = sr.getProperties().get(LdapAttrs.labeledURI.name());
172 Repository initRepository = bc.getService(sr);
173 if (log.isDebugEnabled())
174 log.debug("Found init repository " + labeledUri + ", copying it...");
175 initFromRepository(deployedNodeRepository, initRepository);
176 log.info("Node repository initialised from " + labeledUri);
177 }
178 }
179
180 /** Init from a (typically remote) repository. */
181 private void initFromRepository(Repository deployedNodeRepository, Repository initRepository) {
182 Session initSession = null;
183 try {
184 initSession = initRepository.login();
185 workspaces: for (String workspaceName : initSession.getWorkspace().getAccessibleWorkspaceNames()) {
186 if ("security".equals(workspaceName))
187 continue workspaces;
188 if (log.isDebugEnabled())
189 log.debug("Copying workspace " + workspaceName + " from init repository...");
190 long begin = System.currentTimeMillis();
191 Session targetSession = null;
192 Session sourceSession = null;
193 try {
194 try {
195 targetSession = CmsJcrUtils.openDataAdminSession(deployedNodeRepository, workspaceName);
196 } catch (IllegalArgumentException e) {// no such workspace
197 Session adminSession = CmsJcrUtils.openDataAdminSession(deployedNodeRepository, null);
198 try {
199 adminSession.getWorkspace().createWorkspace(workspaceName);
200 } finally {
201 Jcr.logout(adminSession);
202 }
203 targetSession = CmsJcrUtils.openDataAdminSession(deployedNodeRepository, workspaceName);
204 }
205 sourceSession = initRepository.login(workspaceName);
206 // JcrUtils.copyWorkspaceXml(sourceSession, targetSession);
207 // TODO deal with referenceable nodes
208 JcrUtils.copy(sourceSession.getRootNode(), targetSession.getRootNode());
209 targetSession.save();
210 long duration = System.currentTimeMillis() - begin;
211 if (log.isDebugEnabled())
212 log.debug("Copied workspace " + workspaceName + " from init repository in " + (duration / 1000)
213 + " s");
214 } catch (Exception e) {
215 log.error("Cannot copy workspace " + workspaceName + " from init repository.", e);
216 } finally {
217 Jcr.logout(sourceSession);
218 Jcr.logout(targetSession);
219 }
220 }
221 } catch (RepositoryException e) {
222 throw new JcrException(e);
223 } finally {
224 Jcr.logout(initSession);
225 }
226 }
227
228 private void prepareHomeRepository(RepositoryImpl deployedRepository) {
229 Session adminSession = KernelUtils.openAdminSession(deployedRepository);
230 try {
231 argeoDataModelExtensionsAvailable = Arrays
232 .asList(adminSession.getWorkspace().getNamespaceRegistry().getURIs())
233 .contains(ArgeoNames.ARGEO_NAMESPACE);
234 } catch (RepositoryException e) {
235 log.warn("Cannot check whether Argeo namespace is registered assuming it isn't.", e);
236 argeoDataModelExtensionsAvailable = false;
237 } finally {
238 JcrUtils.logoutQuietly(adminSession);
239 }
240
241 // Publish home with the highest service ranking
242 Hashtable<String, Object> regProps = new Hashtable<>();
243 regProps.put(CmsConstants.CN, CmsConstants.EGO_REPOSITORY);
244 regProps.put(Constants.SERVICE_RANKING, Integer.MAX_VALUE);
245 Repository egoRepository = new EgoRepository(deployedRepository, false);
246 bc.registerService(Repository.class, egoRepository, regProps);
247 registerRepositoryServlets(CmsConstants.EGO_REPOSITORY, egoRepository);
248
249 // Keyring only if Argeo extensions are available
250 if (argeoDataModelExtensionsAvailable) {
251 new ServiceTracker<CallbackHandler, CallbackHandler>(bc, CallbackHandler.class, null) {
252
253 @Override
254 public CallbackHandler addingService(ServiceReference<CallbackHandler> reference) {
255 NodeKeyRing nodeKeyring = new NodeKeyRing(egoRepository);
256 CallbackHandler callbackHandler = bc.getService(reference);
257 nodeKeyring.setDefaultCallbackHandler(callbackHandler);
258 bc.registerService(LangUtils.names(Keyring.class, CryptoKeyring.class, ManagedService.class),
259 nodeKeyring, LangUtils.dict(Constants.SERVICE_PID, CmsConstants.NODE_KEYRING_PID));
260 return callbackHandler;
261 }
262
263 }.open();
264 }
265 }
266
267 /** Session is logged out. */
268 private void prepareDataModel(String cn, Repository repository, List<String> publishAsLocalRepo) {
269 Session adminSession = KernelUtils.openAdminSession(repository);
270 try {
271 Set<String> processed = new HashSet<String>();
272 bundles: for (Bundle bundle : bc.getBundles()) {
273 BundleWiring wiring = bundle.adapt(BundleWiring.class);
274 if (wiring == null)
275 continue bundles;
276 if (CmsConstants.NODE_REPOSITORY.equals(cn))// process all data models
277 processWiring(cn, adminSession, wiring, processed, false, publishAsLocalRepo);
278 else {
279 List<BundleCapability> capabilities = wiring.getCapabilities(CMS_DATA_MODEL_NAMESPACE);
280 for (BundleCapability capability : capabilities) {
281 String dataModelName = (String) capability.getAttributes().get(DataModelNamespace.NAME);
282 if (dataModelName.equals(cn))// process only own data model
283 processWiring(cn, adminSession, wiring, processed, false, publishAsLocalRepo);
284 }
285 }
286 }
287 } finally {
288 JcrUtils.logoutQuietly(adminSession);
289 }
290 }
291
292 private void processWiring(String cn, Session adminSession, BundleWiring wiring, Set<String> processed,
293 boolean importListedAbstractModels, List<String> publishAsLocalRepo) {
294 // recursively process requirements first
295 List<BundleWire> requiredWires = wiring.getRequiredWires(CMS_DATA_MODEL_NAMESPACE);
296 for (BundleWire wire : requiredWires) {
297 processWiring(cn, adminSession, wire.getProviderWiring(), processed, true, publishAsLocalRepo);
298 }
299
300 List<BundleCapability> capabilities = wiring.getCapabilities(CMS_DATA_MODEL_NAMESPACE);
301 capabilities: for (BundleCapability capability : capabilities) {
302 if (!importListedAbstractModels
303 && KernelUtils.asBoolean((String) capability.getAttributes().get(DataModelNamespace.ABSTRACT))) {
304 continue capabilities;
305 }
306 boolean publish = registerDataModelCapability(cn, adminSession, capability, processed);
307 if (publish)
308 publishAsLocalRepo.add((String) capability.getAttributes().get(DataModelNamespace.NAME));
309 }
310 }
311
312 private boolean registerDataModelCapability(String cn, Session adminSession, BundleCapability capability,
313 Set<String> processed) {
314 Map<String, Object> attrs = capability.getAttributes();
315 String name = (String) attrs.get(DataModelNamespace.NAME);
316 if (processed.contains(name)) {
317 if (log.isTraceEnabled())
318 log.trace("Data model " + name + " has already been processed");
319 return false;
320 }
321
322 // CND
323 String path = (String) attrs.get(DataModelNamespace.CND);
324 if (path != null) {
325 File dataModel = bc.getBundle().getDataFile("dataModels/" + path);
326 if (!dataModel.exists()) {
327 URL url = capability.getRevision().getBundle().getResource(path);
328 if (url == null)
329 throw new IllegalArgumentException("No data model '" + name + "' found under path " + path);
330 try (Reader reader = new InputStreamReader(url.openStream())) {
331 CndImporter.registerNodeTypes(reader, adminSession, true);
332 processed.add(name);
333 dataModel.getParentFile().mkdirs();
334 dataModel.createNewFile();
335 if (log.isDebugEnabled())
336 log.debug("Registered CND " + url);
337 } catch (Exception e) {
338 log.error("Cannot import CND " + url, e);
339 }
340 }
341 }
342
343 if (KernelUtils.asBoolean((String) attrs.get(DataModelNamespace.ABSTRACT)))
344 return false;
345 // Non abstract
346 boolean isStandalone = isStandalone(name);
347 boolean publishLocalRepo;
348 if (isStandalone && name.equals(cn))// includes the node itself
349 publishLocalRepo = true;
350 else if (!isStandalone && cn.equals(CmsConstants.NODE_REPOSITORY))
351 publishLocalRepo = true;
352 else
353 publishLocalRepo = false;
354
355 return publishLocalRepo;
356 }
357
358 boolean isStandalone(String dataModelName) {
359 return cmsDeployment.getProps(CmsConstants.NODE_REPOS_FACTORY_PID, dataModelName) != null;
360 }
361
362 private void publishLocalRepo(String dataModelName, Repository repository) {
363 Hashtable<String, Object> properties = new Hashtable<>();
364 properties.put(CmsConstants.CN, dataModelName);
365 LocalRepository localRepository;
366 String[] classes;
367 if (repository instanceof RepositoryImpl) {
368 localRepository = new JackrabbitLocalRepository((RepositoryImpl) repository, dataModelName);
369 classes = new String[] { Repository.class.getName(), LocalRepository.class.getName(),
370 JackrabbitLocalRepository.class.getName() };
371 } else {
372 localRepository = new LocalRepository(repository, dataModelName);
373 classes = new String[] { Repository.class.getName(), LocalRepository.class.getName() };
374 }
375 bc.registerService(classes, localRepository, properties);
376
377 // TODO make it configurable
378 registerRepositoryServlets(dataModelName, localRepository);
379 if (log.isTraceEnabled())
380 log.trace("Published data model " + dataModelName);
381 }
382
383 // @Override
384 // public synchronized Long getAvailableSince() {
385 // return availableSince;
386 // }
387 //
388 // public synchronized boolean isAvailable() {
389 // return availableSince != null;
390 // }
391
392 protected void registerRepositoryServlets(String alias, Repository repository) {
393 // FIXME re-enable it with a proper class loader
394 // registerRemotingServlet(alias, repository);
395 // registerWebdavServlet(alias, repository);
396 }
397
398 protected void registerWebdavServlet(String alias, Repository repository) {
399 CmsWebDavServlet webdavServlet = new CmsWebDavServlet(alias, repository);
400 Hashtable<String, String> ip = new Hashtable<>();
401 ip.put(HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + CmsWebDavServlet.INIT_PARAM_RESOURCE_CONFIG, webDavConfig);
402 ip.put(HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + CmsWebDavServlet.INIT_PARAM_RESOURCE_PATH_PREFIX,
403 "/" + alias);
404
405 ip.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/" + alias + "/*");
406 ip.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT,
407 "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH + "=" + CmsConstants.PATH_DATA + ")");
408 bc.registerService(Servlet.class, webdavServlet, ip);
409 }
410
411 protected void registerRemotingServlet(String alias, Repository repository) {
412 CmsRemotingServlet remotingServlet = new CmsRemotingServlet(alias, repository);
413 Hashtable<String, String> ip = new Hashtable<>();
414 ip.put(CmsConstants.CN, alias);
415 // Properties ip = new Properties();
416 ip.put(HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + CmsRemotingServlet.INIT_PARAM_RESOURCE_PATH_PREFIX,
417 "/" + alias);
418 ip.put(HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + CmsRemotingServlet.INIT_PARAM_AUTHENTICATE_HEADER,
419 "Negotiate");
420
421 // Looks like a bug in Jackrabbit remoting init
422 Path tmpDir;
423 try {
424 tmpDir = Files.createTempDirectory("remoting_" + alias);
425 } catch (IOException e) {
426 throw new RuntimeException("Cannot create temp directory for remoting servlet", e);
427 }
428 ip.put(HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + CmsRemotingServlet.INIT_PARAM_HOME, tmpDir.toString());
429 ip.put(HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + CmsRemotingServlet.INIT_PARAM_TMP_DIRECTORY,
430 "remoting_" + alias);
431 ip.put(HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + CmsRemotingServlet.INIT_PARAM_PROTECTED_HANDLERS_CONFIG,
432 JcrHttpUtils.DEFAULT_PROTECTED_HANDLERS);
433 ip.put(HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + CmsRemotingServlet.INIT_PARAM_CREATE_ABSOLUTE_URI, "false");
434
435 ip.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/" + alias + "/*");
436 ip.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT,
437 "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH + "=" + CmsConstants.PATH_JCR + ")");
438 bc.registerService(Servlet.class, remotingServlet, ip);
439 }
440
441 private class RepositoryContextStc extends ServiceTracker<RepositoryContext, RepositoryContext> {
442
443 public RepositoryContextStc() {
444 super(bc, RepositoryContext.class, null);
445 }
446
447 @Override
448 public RepositoryContext addingService(ServiceReference<RepositoryContext> reference) {
449 RepositoryContext repoContext = bc.getService(reference);
450 String cn = (String) reference.getProperty(CmsConstants.CN);
451 if (cn != null) {
452 List<String> publishAsLocalRepo = new ArrayList<>();
453 if (cn.equals(CmsConstants.NODE_REPOSITORY)) {
454 // JackrabbitDataModelMigration.clearRepositoryCaches(repoContext.getRepositoryConfig());
455 prepareNodeRepository(repoContext.getRepository(), publishAsLocalRepo);
456 // TODO separate home repository
457 prepareHomeRepository(repoContext.getRepository());
458 registerRepositoryServlets(cn, repoContext.getRepository());
459 nodeAvailable = true;
460 // checkReadiness();
461 } else {
462 prepareDataModel(cn, repoContext.getRepository(), publishAsLocalRepo);
463 }
464 // Publish all at once, so that bundles with multiple CNDs are consistent
465 for (String dataModelName : publishAsLocalRepo)
466 publishLocalRepo(dataModelName, repoContext.getRepository());
467 }
468 return repoContext;
469 }
470
471 @Override
472 public void modifiedService(ServiceReference<RepositoryContext> reference, RepositoryContext service) {
473 }
474
475 @Override
476 public void removedService(ServiceReference<RepositoryContext> reference, RepositoryContext service) {
477 }
478
479 }
480
481 }