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