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