]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/internal/CmsJcrDeployment.java
Improve migration
[lgpl/argeo-commons.git] / jcr / 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.security.auth.callback.CallbackHandler;
25 import javax.servlet.Servlet;
26
27 import org.apache.jackrabbit.commons.cnd.CndImporter;
28 import org.apache.jackrabbit.core.RepositoryContext;
29 import org.apache.jackrabbit.core.RepositoryImpl;
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.internal.servlet.CmsRemotingServlet;
35 import org.argeo.cms.jcr.internal.servlet.CmsWebDavServlet;
36 import org.argeo.cms.jcr.internal.servlet.JcrHttpUtils;
37 import org.argeo.cms.osgi.DataModelNamespace;
38 import org.argeo.cms.security.CryptoKeyring;
39 import org.argeo.cms.security.Keyring;
40 import org.argeo.jcr.Jcr;
41 import org.argeo.jcr.JcrException;
42 import org.argeo.jcr.JcrUtils;
43 import org.argeo.util.LangUtils;
44 import org.osgi.framework.Bundle;
45 import org.osgi.framework.BundleContext;
46 import org.osgi.framework.Constants;
47 import org.osgi.framework.FrameworkUtil;
48 import org.osgi.framework.InvalidSyntaxException;
49 import org.osgi.framework.ServiceReference;
50 import org.osgi.framework.wiring.BundleCapability;
51 import org.osgi.framework.wiring.BundleWire;
52 import org.osgi.framework.wiring.BundleWiring;
53 import org.osgi.service.cm.ManagedService;
54 import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
55 import org.osgi.util.tracker.ServiceTracker;
56
57 /** Implementation of a CMS deployment. */
58 public class CmsJcrDeployment {
59 private final CmsLog log = CmsLog.getLog(getClass());
60 private final BundleContext bc = FrameworkUtil.getBundle(getClass()).getBundleContext();
61
62 private DataModels dataModels;
63 private String webDavConfig = JcrHttpUtils.WEBDAV_CONFIG;
64
65 private boolean argeoDataModelExtensionsAvailable = false;
66
67 // Readiness
68 private boolean nodeAvailable = false;
69
70 // CmsDeployment cmsDeployment;
71 public void start() {
72 dataModels = new DataModels(bc);
73
74 ServiceTracker<?, ?> repoContextSt = new RepositoryContextStc();
75 repoContextSt.open();
76 // KernelUtils.asyncOpen(repoContextSt);
77
78 // nodeDeployment = CmsJcrActivator.getService(NodeDeployment.class);
79
80 //JcrInitUtils.addToDeployment(cmsDeployment);
81
82 // contentRepository.registerTypes(NamespaceRegistry.PREFIX_JCR, NamespaceRegistry.NAMESPACE_JCR, null);
83 // contentRepository.registerTypes(NamespaceRegistry.PREFIX_MIX, NamespaceRegistry.NAMESPACE_MIX, null);
84 // contentRepository.registerTypes(NamespaceRegistry.PREFIX_NT, NamespaceRegistry.NAMESPACE_NT, null);
85 // // Jackrabbit
86 // // see
87 // // https://jackrabbit.apache.org/archive/wiki/JCR/NamespaceRegistry_115513459.html
88 // contentRepository.registerTypes("rep", "internal", null);
89
90 }
91
92 public void stop() {
93 // if (nodeHttp != null)
94 // nodeHttp.destroy();
95
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, 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 registerRemotingServlet(alias, repository);
394 registerWebdavServlet(alias, repository);
395 }
396
397 protected void registerWebdavServlet(String alias, Repository repository) {
398 CmsWebDavServlet webdavServlet = new CmsWebDavServlet(alias, repository);
399 Hashtable<String, String> ip = new Hashtable<>();
400 ip.put(HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + CmsWebDavServlet.INIT_PARAM_RESOURCE_CONFIG, webDavConfig);
401 ip.put(HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + CmsWebDavServlet.INIT_PARAM_RESOURCE_PATH_PREFIX,
402 "/" + alias);
403
404 ip.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/" + alias + "/*");
405 ip.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT,
406 "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH + "=" + CmsConstants.PATH_DATA + ")");
407 bc.registerService(Servlet.class, webdavServlet, ip);
408 }
409
410 protected void registerRemotingServlet(String alias, Repository repository) {
411 CmsRemotingServlet remotingServlet = new CmsRemotingServlet(alias, repository);
412 Hashtable<String, String> ip = new Hashtable<>();
413 ip.put(CmsConstants.CN, alias);
414 // Properties ip = new Properties();
415 ip.put(HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + CmsRemotingServlet.INIT_PARAM_RESOURCE_PATH_PREFIX,
416 "/" + alias);
417 ip.put(HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + CmsRemotingServlet.INIT_PARAM_AUTHENTICATE_HEADER,
418 "Negotiate");
419
420 // Looks like a bug in Jackrabbit remoting init
421 Path tmpDir;
422 try {
423 tmpDir = Files.createTempDirectory("remoting_" + alias);
424 } catch (IOException e) {
425 throw new RuntimeException("Cannot create temp directory for remoting servlet", e);
426 }
427 ip.put(HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + CmsRemotingServlet.INIT_PARAM_HOME, tmpDir.toString());
428 ip.put(HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + CmsRemotingServlet.INIT_PARAM_TMP_DIRECTORY,
429 "remoting_" + alias);
430 ip.put(HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + CmsRemotingServlet.INIT_PARAM_PROTECTED_HANDLERS_CONFIG,
431 JcrHttpUtils.DEFAULT_PROTECTED_HANDLERS);
432 ip.put(HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + CmsRemotingServlet.INIT_PARAM_CREATE_ABSOLUTE_URI, "false");
433
434 ip.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/" + alias + "/*");
435 ip.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT,
436 "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH + "=" + CmsConstants.PATH_JCR + ")");
437 bc.registerService(Servlet.class, remotingServlet, ip);
438 }
439
440 private class RepositoryContextStc extends ServiceTracker<RepositoryContext, RepositoryContext> {
441
442 public RepositoryContextStc() {
443 super(bc, RepositoryContext.class, null);
444 }
445
446 @Override
447 public RepositoryContext addingService(ServiceReference<RepositoryContext> reference) {
448 RepositoryContext repoContext = bc.getService(reference);
449 String cn = (String) reference.getProperty(CmsConstants.CN);
450 if (cn != null) {
451 List<String> publishAsLocalRepo = new ArrayList<>();
452 if (cn.equals(CmsConstants.NODE_REPOSITORY)) {
453 // JackrabbitDataModelMigration.clearRepositoryCaches(repoContext.getRepositoryConfig());
454 prepareNodeRepository(repoContext.getRepository(), publishAsLocalRepo);
455 // TODO separate home repository
456 prepareHomeRepository(repoContext.getRepository());
457 registerRepositoryServlets(cn, repoContext.getRepository());
458 nodeAvailable = true;
459 // checkReadiness();
460 } else {
461 prepareDataModel(cn, repoContext.getRepository(), publishAsLocalRepo);
462 }
463 // Publish all at once, so that bundles with multiple CNDs are consistent
464 for (String dataModelName : publishAsLocalRepo)
465 publishLocalRepo(dataModelName, repoContext.getRepository());
466 }
467 return repoContext;
468 }
469
470 @Override
471 public void modifiedService(ServiceReference<RepositoryContext> reference, RepositoryContext service) {
472 }
473
474 @Override
475 public void removedService(ServiceReference<RepositoryContext> reference, RepositoryContext service) {
476 }
477
478 }
479
480 }