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
.Collection
;
16 import java
.util
.HashSet
;
17 import java
.util
.Hashtable
;
18 import java
.util
.Iterator
;
19 import java
.util
.List
;
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
;
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
;
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();
67 private DataModels dataModels
;
68 private String webDavConfig
= JcrHttpUtils
.WEBDAV_CONFIG
;
70 private boolean argeoDataModelExtensionsAvailable
= false;
73 private boolean nodeAvailable
= false;
75 CmsDeployment cmsDeployment
;
77 public CmsJcrDeployment() {
78 dataModels
= new DataModels(bc
);
84 ServiceTracker
<?
, ?
> repoContextSt
= new RepositoryContextStc();
85 // repoContextSt.open();
86 KernelUtils
.asyncOpen(repoContextSt
);
88 // nodeDeployment = CmsJcrActivator.getService(NodeDeployment.class);
90 JcrInitUtils
.addToDeployment(cmsDeployment
);
94 public void destroy() {
95 // if (nodeHttp != null)
96 // nodeHttp.destroy();
99 for (ServiceReference
<JackrabbitLocalRepository
> sr
: bc
100 .getServiceReferences(JackrabbitLocalRepository
.class, null)) {
101 bc
.getService(sr
).destroy();
103 } catch (InvalidSyntaxException e1
) {
104 log
.error("Cannot clean repositories", e1
);
109 public void setCmsDeployment(CmsDeployment cmsDeployment
) {
110 this.cmsDeployment
= cmsDeployment
;
114 * Checks whether the deployment is available according to expectations, and
115 * mark it as available.
117 // private synchronized void checkReadiness() {
118 // if (isAvailable())
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);
130 // log.debug("## data: " + data);
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);
140 private void prepareNodeRepository(Repository deployedNodeRepository
, List
<String
> publishAsLocalRepo
) {
141 // if (availableSince != null) {
142 // throw new IllegalStateException("Deployment is already available");
146 prepareDataModel(CmsConstants
.NODE_REPOSITORY
, deployedNodeRepository
, publishAsLocalRepo
);
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);
160 // init from repository
161 Collection
<ServiceReference
<Repository
>> initRepositorySr
;
163 initRepositorySr
= bc
.getServiceReferences(Repository
.class,
164 "(" + CmsConstants
.CN
+ "=" + CmsConstants
.NODE_INIT
+ ")");
165 } catch (InvalidSyntaxException e1
) {
166 throw new IllegalArgumentException(e1
);
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
);
180 /** Init from a (typically remote) repository. */
181 private void initFromRepository(Repository deployedNodeRepository
, Repository initRepository
) {
182 Session initSession
= null;
184 initSession
= initRepository
.login();
185 workspaces
: for (String workspaceName
: initSession
.getWorkspace().getAccessibleWorkspaceNames()) {
186 if ("security".equals(workspaceName
))
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;
195 targetSession
= CmsJcrUtils
.openDataAdminSession(deployedNodeRepository
, workspaceName
);
196 } catch (IllegalArgumentException e
) {// no such workspace
197 Session adminSession
= CmsJcrUtils
.openDataAdminSession(deployedNodeRepository
, null);
199 adminSession
.getWorkspace().createWorkspace(workspaceName
);
201 Jcr
.logout(adminSession
);
203 targetSession
= CmsJcrUtils
.openDataAdminSession(deployedNodeRepository
, workspaceName
);
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)
214 } catch (Exception e
) {
215 log
.error("Cannot copy workspace " + workspaceName
+ " from init repository.", e
);
217 Jcr
.logout(sourceSession
);
218 Jcr
.logout(targetSession
);
221 } catch (RepositoryException e
) {
222 throw new JcrException(e
);
224 Jcr
.logout(initSession
);
228 private void prepareHomeRepository(RepositoryImpl deployedRepository
) {
229 Session adminSession
= KernelUtils
.openAdminSession(deployedRepository
);
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;
238 JcrUtils
.logoutQuietly(adminSession
);
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
);
249 // Keyring only if Argeo extensions are available
250 if (argeoDataModelExtensionsAvailable
) {
251 new ServiceTracker
<CallbackHandler
, CallbackHandler
>(bc
, CallbackHandler
.class, null) {
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
;
267 /** Session is logged out. */
268 private void prepareDataModel(String cn
, Repository repository
, List
<String
> publishAsLocalRepo
) {
269 Session adminSession
= KernelUtils
.openAdminSession(repository
);
271 Set
<String
> processed
= new HashSet
<String
>();
272 bundles
: for (Bundle bundle
: bc
.getBundles()) {
273 BundleWiring wiring
= bundle
.adapt(BundleWiring
.class);
276 if (CmsConstants
.NODE_REPOSITORY
.equals(cn
))// process all data models
277 processWiring(cn
, adminSession
, wiring
, processed
, false, publishAsLocalRepo
);
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
);
288 JcrUtils
.logoutQuietly(adminSession
);
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
);
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
;
306 boolean publish
= registerDataModelCapability(cn
, adminSession
, capability
, processed
);
308 publishAsLocalRepo
.add((String
) capability
.getAttributes().get(DataModelNamespace
.NAME
));
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");
323 String path
= (String
) attrs
.get(DataModelNamespace
.CND
);
325 File dataModel
= bc
.getBundle().getDataFile("dataModels/" + path
);
326 if (!dataModel
.exists()) {
327 URL url
= capability
.getRevision().getBundle().getResource(path
);
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);
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
);
343 if (KernelUtils
.asBoolean((String
) attrs
.get(DataModelNamespace
.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;
353 publishLocalRepo
= false;
355 return publishLocalRepo
;
358 boolean isStandalone(String dataModelName
) {
359 return cmsDeployment
.getProps(CmsConstants
.NODE_REPOS_FACTORY_PID
, dataModelName
) != null;
362 private void publishLocalRepo(String dataModelName
, Repository repository
) {
363 Hashtable
<String
, Object
> properties
= new Hashtable
<>();
364 properties
.put(CmsConstants
.CN
, dataModelName
);
365 LocalRepository localRepository
;
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() };
372 localRepository
= new LocalRepository(repository
, dataModelName
);
373 classes
= new String
[] { Repository
.class.getName(), LocalRepository
.class.getName() };
375 bc
.registerService(classes
, localRepository
, properties
);
377 // TODO make it configurable
378 registerRepositoryServlets(dataModelName
, localRepository
);
379 if (log
.isTraceEnabled())
380 log
.trace("Published data model " + dataModelName
);
384 // public synchronized Long getAvailableSince() {
385 // return availableSince;
388 // public synchronized boolean isAvailable() {
389 // return availableSince != null;
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);
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
,
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
);
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
,
418 ip
.put(HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX
+ CmsRemotingServlet
.INIT_PARAM_AUTHENTICATE_HEADER
,
421 // Looks like a bug in Jackrabbit remoting init
424 tmpDir
= Files
.createTempDirectory("remoting_" + alias
);
425 } catch (IOException e
) {
426 throw new RuntimeException("Cannot create temp directory for remoting servlet", e
);
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");
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
);
441 private class RepositoryContextStc
extends ServiceTracker
<RepositoryContext
, RepositoryContext
> {
443 public RepositoryContextStc() {
444 super(bc
, RepositoryContext
.class, null);
448 public RepositoryContext
addingService(ServiceReference
<RepositoryContext
> reference
) {
449 RepositoryContext repoContext
= bc
.getService(reference
);
450 String cn
= (String
) reference
.getProperty(CmsConstants
.CN
);
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;
462 prepareDataModel(cn
, repoContext
.getRepository(), publishAsLocalRepo
);
464 // Publish all at once, so that bundles with multiple CNDs are consistent
465 for (String dataModelName
: publishAsLocalRepo
)
466 publishLocalRepo(dataModelName
, repoContext
.getRepository());
472 public void modifiedService(ServiceReference
<RepositoryContext
> reference
, RepositoryContext service
) {
476 public void removedService(ServiceReference
<RepositoryContext
> reference
, RepositoryContext service
) {