1 package org
.argeo
.cms
.jcr
.internal
;
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
;
7 import java
.io
.IOException
;
8 import java
.io
.InputStreamReader
;
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
;
21 import javax
.jcr
.Repository
;
22 import javax
.jcr
.RepositoryException
;
23 import javax
.jcr
.Session
;
24 import javax
.servlet
.Servlet
;
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
;
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();
57 private DataModels dataModels
;
58 private String webDavConfig
= JcrHttpUtils
.WEBDAV_CONFIG
;
60 private boolean argeoDataModelExtensionsAvailable
= false;
63 private boolean nodeAvailable
= false;
65 // CmsDeployment cmsDeployment;
67 dataModels
= new DataModels(bc
);
69 ServiceTracker
<?
, ?
> repoContextSt
= new RepositoryContextStc();
71 // KernelUtils.asyncOpen(repoContextSt);
73 // nodeDeployment = CmsJcrActivator.getService(NodeDeployment.class);
75 //JcrInitUtils.addToDeployment(cmsDeployment);
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);
82 // // https://jackrabbit.apache.org/archive/wiki/JCR/NamespaceRegistry_115513459.html
83 // contentRepository.registerTypes("rep", "internal", null);
88 // if (nodeHttp != null)
89 // nodeHttp.destroy();
93 for (ServiceReference
<JackrabbitLocalRepository
> sr
: bc
94 .getServiceReferences(JackrabbitLocalRepository
.class, null)) {
95 bc
.getService(sr
).destroy();
97 } catch (InvalidSyntaxException e1
) {
98 log
.error("Cannot clean repositories", e1
);
103 // public void setCmsDeployment(CmsDeployment cmsDeployment) {
104 // this.cmsDeployment = cmsDeployment;
108 * Checks whether the deployment is available according to expectations, and
109 * mark it as available.
111 // private synchronized void checkReadiness() {
112 // if (isAvailable())
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);
124 // log.debug("## data: " + data);
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);
134 private void prepareNodeRepository(Repository deployedNodeRepository
, List
<String
> publishAsLocalRepo
) {
135 // if (availableSince != null) {
136 // throw new IllegalStateException("Deployment is already available");
140 prepareDataModel(CmsConstants
.NODE_REPOSITORY
, deployedNodeRepository
, publishAsLocalRepo
);
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);
154 // init from repository
155 // Collection<ServiceReference<Repository>> initRepositorySr;
157 // initRepositorySr = bc.getServiceReferences(Repository.class,
158 // "(" + CmsConstants.CN + "=" + CmsConstants.NODE_INIT + ")");
159 // } catch (InvalidSyntaxException e1) {
160 // throw new IllegalArgumentException(e1);
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);
174 /** Init from a (typically remote) repository. */
175 private void initFromRepository(Repository deployedNodeRepository
, Repository initRepository
) {
176 Session initSession
= null;
178 initSession
= initRepository
.login();
179 workspaces
: for (String workspaceName
: initSession
.getWorkspace().getAccessibleWorkspaceNames()) {
180 if ("security".equals(workspaceName
))
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;
189 targetSession
= CmsJcrUtils
.openDataAdminSession(deployedNodeRepository
, workspaceName
);
190 } catch (IllegalArgumentException e
) {// no such workspace
191 Session adminSession
= CmsJcrUtils
.openDataAdminSession(deployedNodeRepository
, null);
193 adminSession
.getWorkspace().createWorkspace(workspaceName
);
195 Jcr
.logout(adminSession
);
197 targetSession
= CmsJcrUtils
.openDataAdminSession(deployedNodeRepository
, workspaceName
);
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)
208 } catch (Exception e
) {
209 log
.error("Cannot copy workspace " + workspaceName
+ " from init repository.", e
);
211 Jcr
.logout(sourceSession
);
212 Jcr
.logout(targetSession
);
215 } catch (RepositoryException e
) {
216 throw new JcrException(e
);
218 Jcr
.logout(initSession
);
222 private void prepareHomeRepository(RepositoryImpl deployedRepository
) {
223 Session adminSession
= KernelUtils
.openAdminSession(deployedRepository
);
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;
232 JcrUtils
.logoutQuietly(adminSession
);
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
);
243 // Keyring only if Argeo extensions are available
244 // if (argeoDataModelExtensionsAvailable) {
245 // new ServiceTracker<CallbackHandler, CallbackHandler>(bc, CallbackHandler.class, null) {
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;
261 /** Session is logged out. */
262 private void prepareDataModel(String cn
, Repository repository
, List
<String
> publishAsLocalRepo
) {
263 Session adminSession
= KernelUtils
.openAdminSession(repository
);
265 Set
<String
> processed
= new HashSet
<String
>();
266 bundles
: for (Bundle bundle
: bc
.getBundles()) {
267 BundleWiring wiring
= bundle
.adapt(BundleWiring
.class);
270 if (CmsConstants
.NODE_REPOSITORY
.equals(cn
))// process all data models
271 processWiring(cn
, adminSession
, wiring
, processed
, false, publishAsLocalRepo
);
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
);
282 JcrUtils
.logoutQuietly(adminSession
);
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
);
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
;
300 boolean publish
= registerDataModelCapability(cn
, adminSession
, capability
, processed
);
302 publishAsLocalRepo
.add((String
) capability
.getAttributes().get(DataModelNamespace
.NAME
));
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");
317 String path
= (String
) attrs
.get(DataModelNamespace
.CND
);
319 File dataModel
= bc
.getBundle().getDataFile("dataModels/" + path
);
320 if (!dataModel
.exists()) {
321 URL url
= capability
.getRevision().getBundle().getResource(path
);
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);
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
);
337 if (KernelUtils
.asBoolean((String
) attrs
.get(DataModelNamespace
.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;
347 publishLocalRepo
= false;
349 return publishLocalRepo
;
352 boolean isStandalone(String dataModelName
) {
354 //return cmsDeployment.getProps(CmsConstants.NODE_REPOS_FACTORY_PID, dataModelName) != null;
357 private void publishLocalRepo(String dataModelName
, Repository repository
) {
358 Hashtable
<String
, Object
> properties
= new Hashtable
<>();
359 properties
.put(CmsConstants
.CN
, dataModelName
);
360 LocalRepository localRepository
;
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() };
367 localRepository
= new LocalRepository(repository
, dataModelName
);
368 classes
= new String
[] { Repository
.class.getName(), LocalRepository
.class.getName() };
370 bc
.registerService(classes
, localRepository
, properties
);
372 // TODO make it configurable
373 registerRepositoryServlets(dataModelName
, localRepository
);
374 if (log
.isTraceEnabled())
375 log
.trace("Published data model " + dataModelName
);
379 // public synchronized Long getAvailableSince() {
380 // return availableSince;
383 // public synchronized boolean isAvailable() {
384 // return availableSince != null;
387 protected void registerRepositoryServlets(String alias
, Repository repository
) {
388 registerRemotingServlet(alias
, repository
);
389 registerWebdavServlet(alias
, repository
);
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
,
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
);
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
,
412 ip
.put(HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX
+ CmsRemotingServlet
.INIT_PARAM_AUTHENTICATE_HEADER
,
415 // Looks like a bug in Jackrabbit remoting init
418 tmpDir
= Files
.createTempDirectory("remoting_" + alias
);
419 } catch (IOException e
) {
420 throw new RuntimeException("Cannot create temp directory for remoting servlet", e
);
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");
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
);
435 private class RepositoryContextStc
extends ServiceTracker
<RepositoryContext
, RepositoryContext
> {
437 public RepositoryContextStc() {
438 super(bc
, RepositoryContext
.class, null);
442 public RepositoryContext
addingService(ServiceReference
<RepositoryContext
> reference
) {
443 RepositoryContext repoContext
= bc
.getService(reference
);
444 String cn
= (String
) reference
.getProperty(CmsConstants
.CN
);
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;
456 prepareDataModel(cn
, repoContext
.getRepository(), publishAsLocalRepo
);
458 // Publish all at once, so that bundles with multiple CNDs are consistent
459 for (String dataModelName
: publishAsLocalRepo
)
460 publishLocalRepo(dataModelName
, repoContext
.getRepository());
466 public void modifiedService(ServiceReference
<RepositoryContext
> reference
, RepositoryContext service
) {
470 public void removedService(ServiceReference
<RepositoryContext
> reference
, RepositoryContext service
) {