1 package org
.argeo
.slc
.repo
;
3 import java
.io
.InputStream
;
4 import java
.util
.Arrays
;
5 import java
.util
.Calendar
;
6 import java
.util
.GregorianCalendar
;
7 import java
.util
.HashMap
;
10 import java
.util
.TimeZone
;
12 import javax
.jcr
.Binary
;
13 import javax
.jcr
.Credentials
;
14 import javax
.jcr
.NoSuchWorkspaceException
;
15 import javax
.jcr
.Node
;
16 import javax
.jcr
.NodeIterator
;
17 import javax
.jcr
.Property
;
18 import javax
.jcr
.PropertyIterator
;
19 import javax
.jcr
.PropertyType
;
20 import javax
.jcr
.Repository
;
21 import javax
.jcr
.RepositoryException
;
22 import javax
.jcr
.RepositoryFactory
;
23 import javax
.jcr
.Session
;
24 import javax
.jcr
.SimpleCredentials
;
25 import javax
.jcr
.nodetype
.NodeType
;
26 import javax
.jcr
.query
.Query
;
27 import javax
.jcr
.query
.QueryResult
;
29 import org
.apache
.commons
.io
.IOUtils
;
30 import org
.apache
.commons
.logging
.Log
;
31 import org
.apache
.commons
.logging
.LogFactory
;
32 import org
.argeo
.jcr
.JcrMonitor
;
33 import org
.argeo
.jcr
.JcrUtils
;
34 import org
.argeo
.api
.NodeUtils
;
35 import org
.argeo
.slc
.SlcException
;
36 import org
.xml
.sax
.SAXException
;
39 * Synchronise workspaces from a remote software repository to the local
40 * repository (Synchronisation in the other direction does not work).
42 * Workspaces are retrieved by name given a map that links the source with a
43 * target name. If a target workspace does not exist, it is created. Otherwise
44 * we copy the content of the source workspace into the target one.
46 public class RepoSync
implements Runnable
{
47 private final static Log log
= LogFactory
.getLog(RepoSync
.class);
49 // Centralizes definition of workspaces that must be ignored by the sync.
50 private final static List
<String
> IGNORED_WKSP_LIST
= Arrays
.asList("security", "localrepo");
52 private final Calendar zero
;
53 private Session sourceDefaultSession
= null;
54 private Session targetDefaultSession
= null;
56 private Repository sourceRepository
;
57 private Credentials sourceCredentials
;
58 private Repository targetRepository
;
59 private Credentials targetCredentials
;
61 // if Repository and Credentials objects are not explicitly set
62 private String sourceRepoUri
;
63 private String sourceUsername
;
64 private char[] sourcePassword
;
65 private String targetRepoUri
;
66 private String targetUsername
;
67 private char[] targetPassword
;
69 private RepositoryFactory repositoryFactory
;
71 private JcrMonitor monitor
;
72 private Map
<String
, String
> workspaceMap
;
75 private Boolean filesOnly
= false;
78 zero
= new GregorianCalendar(TimeZone
.getTimeZone("UTC"));
79 zero
.setTimeInMillis(0);
84 * Shortcut to instantiate a RepoSync with already known repositories and
87 * @param sourceRepository
88 * @param sourceCredentials
89 * @param targetRepository
90 * @param targetCredentials
92 public RepoSync(Repository sourceRepository
, Credentials sourceCredentials
, Repository targetRepository
,
93 Credentials targetCredentials
) {
95 this.sourceRepository
= sourceRepository
;
96 this.sourceCredentials
= sourceCredentials
;
97 this.targetRepository
= targetRepository
;
98 this.targetCredentials
= targetCredentials
;
103 long begin
= System
.currentTimeMillis();
106 if (sourceRepository
== null)
107 sourceRepository
= NodeUtils
.getRepositoryByUri(repositoryFactory
, sourceRepoUri
);
108 if (sourceCredentials
== null && sourceUsername
!= null)
109 sourceCredentials
= new SimpleCredentials(sourceUsername
, sourcePassword
);
110 // FIXME make it more generic
111 sourceDefaultSession
= sourceRepository
.login(sourceCredentials
, RepoConstants
.DEFAULT_DEFAULT_WORKSPACE
);
113 if (targetRepository
== null)
114 targetRepository
= NodeUtils
.getRepositoryByUri(repositoryFactory
, targetRepoUri
);
115 if (targetCredentials
== null && targetUsername
!= null)
116 targetCredentials
= new SimpleCredentials(targetUsername
, targetPassword
);
117 targetDefaultSession
= targetRepository
.login(targetCredentials
);
119 Map
<String
, Exception
> errors
= new HashMap
<String
, Exception
>();
120 for (String sourceWorkspaceName
: sourceDefaultSession
.getWorkspace().getAccessibleWorkspaceNames()) {
121 if (monitor
!= null && monitor
.isCanceled())
124 if (workspaceMap
!= null && !workspaceMap
.containsKey(sourceWorkspaceName
))
126 if (IGNORED_WKSP_LIST
.contains(sourceWorkspaceName
))
129 Session sourceSession
= null;
130 Session targetSession
= null;
131 String targetWorkspaceName
= workspaceMap
.get(sourceWorkspaceName
);
134 targetSession
= targetRepository
.login(targetCredentials
, targetWorkspaceName
);
135 } catch (NoSuchWorkspaceException e
) {
136 targetDefaultSession
.getWorkspace().createWorkspace(targetWorkspaceName
);
137 targetSession
= targetRepository
.login(targetCredentials
, targetWorkspaceName
);
139 sourceSession
= sourceRepository
.login(sourceCredentials
, sourceWorkspaceName
);
140 syncWorkspace(sourceSession
, targetSession
);
141 } catch (Exception e
) {
142 errors
.put("Could not sync workspace " + sourceWorkspaceName
, e
);
143 if (log
.isErrorEnabled())
147 JcrUtils
.logoutQuietly(sourceSession
);
148 JcrUtils
.logoutQuietly(targetSession
);
152 if (monitor
!= null && monitor
.isCanceled())
153 log
.info("Sync has been canceled by user");
155 long duration
= (System
.currentTimeMillis() - begin
) / 1000;// s
156 log
.info("Sync " + sourceRepoUri
+ " to " + targetRepoUri
+ " in " + (duration
/ 60)
158 + "min " + (duration
% 60) + "s");
160 if (errors
.size() > 0) {
161 throw new SlcException("Sync failed " + errors
);
163 } catch (RepositoryException e
) {
164 throw new SlcException("Cannot sync " + sourceRepoUri
+ " to " + targetRepoUri
, e
);
166 JcrUtils
.logoutQuietly(sourceDefaultSession
);
167 JcrUtils
.logoutQuietly(targetDefaultSession
);
171 private long getNodesNumber(Session session
) {
172 if (IGNORED_WKSP_LIST
.contains(session
.getWorkspace().getName()))
175 Query countQuery
= session
.getWorkspace().getQueryManager().createQuery(
176 "select file from [" + (true ? NodeType
.NT_FILE
: NodeType
.NT_BASE
) + "] as file", Query
.JCR_SQL2
);
178 QueryResult result
= countQuery
.execute();
179 Long expectedCount
= result
.getNodes().getSize();
180 return expectedCount
;
181 } catch (RepositoryException e
) {
182 throw new SlcException("Unexpected error while computing " + "the size of the fetch for workspace "
183 + session
.getWorkspace().getName(), e
);
187 protected void syncWorkspace(Session sourceSession
, Session targetSession
) {
188 if (monitor
!= null) {
189 monitor
.beginTask("Computing fetch size...", -1);
190 Long totalAmount
= getNodesNumber(sourceSession
);
191 monitor
.beginTask("Fetch", totalAmount
.intValue());
195 String msg
= "Synchronizing workspace: " + sourceSession
.getWorkspace().getName();
197 monitor
.setTaskName(msg
);
198 if (log
.isDebugEnabled())
201 for (NodeIterator it
= sourceSession
.getRootNode().getNodes(); it
.hasNext();) {
202 Node node
= it
.nextNode();
203 if (node
.getName().contains(":"))
205 if (node
.getName().equals("download"))
207 if (!node
.isNodeType(NodeType
.NT_HIERARCHY_NODE
))
209 syncNode(node
, targetSession
);
212 // JcrUtils.copyFiles(sourceSession.getRootNode(), targetSession.getRootNode(),
215 // for (NodeIterator it = sourceSession.getRootNode().getNodes(); it.hasNext();)
217 // Node node = it.nextNode();
218 // if (node.getName().equals("jcr:system"))
220 // syncNode(node, targetSession);
223 if (log
.isDebugEnabled())
224 log
.debug("Synced " + sourceSession
.getWorkspace().getName());
225 } catch (Exception e
) {
227 throw new SlcException("Cannot sync " + sourceSession
.getWorkspace().getName() + " to "
228 + targetSession
.getWorkspace().getName(), e
);
232 /** factorizes monitor management */
233 private void updateMonitor(String msg
) {
234 updateMonitor(msg
, false);
237 protected void syncNode(Node sourceNode
, Session targetSession
) throws RepositoryException
, SAXException
{
240 if (targetSession
.itemExists(sourceNode
.getPath()))
241 targetNode
= targetSession
.getNode(sourceNode
.getPath());
243 targetNode
= JcrUtils
.mkdirs(targetSession
, sourceNode
.getPath(), NodeType
.NT_FOLDER
);
244 JcrUtils
.copyFiles(sourceNode
, targetNode
, true, monitor
, true);
247 // Boolean singleLevel = singleLevel(sourceNode);
249 if (monitor
!= null && monitor
.isCanceled()) {
250 updateMonitor("Fetched has been canceled, " + "process is terminating");
254 Node targetParentNode
= targetSession
.getNode(sourceNode
.getParent().getPath());
256 if (monitor
!= null && sourceNode
.isNodeType(NodeType
.NT_HIERARCHY_NODE
))
257 monitor
.subTask("Process " + sourceNode
.getPath());
260 if (!targetSession
.itemExists(sourceNode
.getPath())) {
262 targetNode
= targetParentNode
.addNode(sourceNode
.getName(), sourceNode
.getPrimaryNodeType().getName());
265 targetNode
= targetSession
.getNode(sourceNode
.getPath());
266 if (!targetNode
.getPrimaryNodeType().getName().equals(sourceNode
.getPrimaryNodeType().getName()))
267 targetNode
.setPrimaryType(sourceNode
.getPrimaryNodeType().getName());
271 // sourceNode.getSession().exportSystemView(sourceNode.getPath(),
272 // contentHandler, false, singleLevel);
274 // if (singleLevel) {
275 // if (targetSession.hasPendingChanges()) {
277 // // (isNew ? "Added " : "Updated ") + targetNode.getPath(),
280 // targetSession.save();
282 // // updateMonitor("Checked " + targetNode.getPath(), false);
286 // mixin and properties
287 for (NodeType nt
: sourceNode
.getMixinNodeTypes()) {
288 if (!targetNode
.isNodeType(nt
.getName()) && targetNode
.canAddMixin(nt
.getName()))
289 targetNode
.addMixin(nt
.getName());
291 copyProperties(sourceNode
, targetNode
);
294 NodeIterator ni
= sourceNode
.getNodes();
295 while (ni
!= null && ni
.hasNext()) {
296 Node sourceChild
= ni
.nextNode();
297 syncNode(sourceChild
, targetSession
);
300 copyTimestamps(sourceNode
, targetNode
);
302 if (sourceNode
.isNodeType(NodeType
.NT_HIERARCHY_NODE
)) {
303 if (targetSession
.hasPendingChanges()) {
304 if (sourceNode
.isNodeType(NodeType
.NT_FILE
))
305 updateMonitor((isNew ?
"Added " : "Updated ") + targetNode
.getPath(), true);
307 targetSession
.save();
309 if (sourceNode
.isNodeType(NodeType
.NT_FILE
))
310 updateMonitor("Checked " + targetNode
.getPath(), false);
313 } catch (RepositoryException e
) {
314 throw new SlcException("Cannot sync source node " + sourceNode
, e
);
318 private void copyTimestamps(Node sourceNode
, Node targetNode
) throws RepositoryException
{
319 if (sourceNode
.getDefinition().isProtected())
321 if (targetNode
.getDefinition().isProtected())
323 copyTimestamp(sourceNode
, targetNode
, Property
.JCR_CREATED
);
324 copyTimestamp(sourceNode
, targetNode
, Property
.JCR_CREATED_BY
);
325 copyTimestamp(sourceNode
, targetNode
, Property
.JCR_LAST_MODIFIED
);
326 copyTimestamp(sourceNode
, targetNode
, Property
.JCR_LAST_MODIFIED_BY
);
329 private void copyTimestamp(Node sourceNode
, Node targetNode
, String property
) throws RepositoryException
{
330 if (sourceNode
.hasProperty(property
)) {
331 Property p
= sourceNode
.getProperty(property
);
332 if (p
.getDefinition().isProtected())
334 if (targetNode
.hasProperty(property
)
335 && targetNode
.getProperty(property
).getValue().equals(sourceNode
.getProperty(property
).getValue()))
337 targetNode
.setProperty(property
, sourceNode
.getProperty(property
).getValue());
341 private void copyProperties(Node sourceNode
, Node targetNode
) throws RepositoryException
{
342 properties
: for (PropertyIterator pi
= sourceNode
.getProperties(); pi
.hasNext();) {
343 Property p
= pi
.nextProperty();
344 if (p
.getDefinition().isProtected())
346 if (p
.getName().equals(Property
.JCR_CREATED
) || p
.getName().equals(Property
.JCR_CREATED_BY
)
347 || p
.getName().equals(Property
.JCR_LAST_MODIFIED
)
348 || p
.getName().equals(Property
.JCR_LAST_MODIFIED_BY
))
351 if (p
.getType() == PropertyType
.BINARY
) {
352 copyBinary(p
, targetNode
);
355 if (p
.isMultiple()) {
356 if (!targetNode
.hasProperty(p
.getName())
357 || !Arrays
.equals(targetNode
.getProperty(p
.getName()).getValues(), p
.getValues()))
358 targetNode
.setProperty(p
.getName(), p
.getValues());
360 if (!targetNode
.hasProperty(p
.getName())
361 || !targetNode
.getProperty(p
.getName()).getValue().equals(p
.getValue()))
362 targetNode
.setProperty(p
.getName(), p
.getValue());
368 private static void copyBinary(Property p
, Node targetNode
) throws RepositoryException
{
369 InputStream in
= null;
370 Binary sourceBinary
= null;
371 Binary targetBinary
= null;
373 sourceBinary
= p
.getBinary();
374 if (targetNode
.hasProperty(p
.getName()))
375 targetBinary
= targetNode
.getProperty(p
.getName()).getBinary();
377 // optim FIXME make it more configurable
378 if (targetBinary
!= null)
379 if (sourceBinary
.getSize() == targetBinary
.getSize()) {
380 if (log
.isTraceEnabled())
381 log
.trace("Skipped " + p
.getPath());
385 in
= sourceBinary
.getStream();
386 targetBinary
= targetNode
.getSession().getValueFactory().createBinary(in
);
387 targetNode
.setProperty(p
.getName(), targetBinary
);
388 } catch (Exception e
) {
389 throw new SlcException("Could not transfer " + p
, e
);
391 IOUtils
.closeQuietly(in
);
392 JcrUtils
.closeQuietly(sourceBinary
);
393 JcrUtils
.closeQuietly(targetBinary
);
397 /** factorizes monitor management */
398 private void updateMonitor(String msg
, Boolean doLog
) {
399 if (doLog
&& log
.isDebugEnabled())
401 if (monitor
!= null) {
403 monitor
.subTask(msg
);
407 // private void syncNode_old(Node sourceNode, Node targetParentNode)
408 // throws RepositoryException, SAXException {
410 // // enable cancelation of the current fetch process
411 // // fxme insure the repository stays in a stable state
412 // if (monitor != null && monitor.isCanceled()) {
413 // updateMonitor("Fetched has been canceled, "
414 // + "process is terminating");
418 // Boolean noRecurse = singleLevel(sourceNode);
419 // Calendar sourceLastModified = null;
420 // if (sourceNode.isNodeType(NodeType.MIX_LAST_MODIFIED)) {
421 // sourceLastModified = sourceNode.getProperty(
422 // Property.JCR_LAST_MODIFIED).getDate();
425 // if (sourceNode.getDefinition().isProtected())
426 // log.warn(sourceNode + " is protected.");
428 // if (!targetParentNode.hasNode(sourceNode.getName())) {
429 // String msg = "Adding " + sourceNode.getPath();
430 // updateMonitor(msg);
431 // if (log.isDebugEnabled())
433 // ContentHandler contentHandler = targetParentNode
436 // .getImportContentHandler(targetParentNode.getPath(),
437 // ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);
438 // sourceNode.getSession().exportSystemView(sourceNode.getPath(),
439 // contentHandler, false, noRecurse);
441 // Node targetNode = targetParentNode.getNode(sourceNode.getName());
442 // if (sourceLastModified != null) {
443 // Calendar targetLastModified = null;
444 // if (targetNode.isNodeType(NodeType.MIX_LAST_MODIFIED)) {
445 // targetLastModified = targetNode.getProperty(
446 // Property.JCR_LAST_MODIFIED).getDate();
449 // if (targetLastModified == null
450 // || targetLastModified.before(sourceLastModified)) {
451 // String msg = "Updating " + targetNode.getPath();
452 // updateMonitor(msg);
453 // if (log.isDebugEnabled())
455 // ContentHandler contentHandler = targetParentNode
458 // .getImportContentHandler(
459 // targetParentNode.getPath(),
460 // ImportUUIDBehavior.IMPORT_UUID_COLLISION_REMOVE_EXISTING);
461 // sourceNode.getSession().exportSystemView(
462 // sourceNode.getPath(), contentHandler, false,
465 // String msg = "Skipped up to date " + targetNode.getPath();
466 // updateMonitor(msg);
467 // if (log.isDebugEnabled())
476 // Node targetNode = targetParentNode.getNode(sourceNode.getName());
477 // if (sourceLastModified != null) {
478 // Calendar zero = new GregorianCalendar();
479 // zero.setTimeInMillis(0);
480 // targetNode.setProperty(Property.JCR_LAST_MODIFIED, zero);
481 // targetNode.getSession().save();
484 // for (NodeIterator it = sourceNode.getNodes(); it.hasNext();) {
485 // syncNode_old(it.nextNode(), targetNode);
488 // if (sourceLastModified != null) {
489 // targetNode.setProperty(Property.JCR_LAST_MODIFIED,
490 // sourceLastModified);
491 // targetNode.getSession().save();
496 protected Boolean
singleLevel(Node sourceNode
) throws RepositoryException
{
497 if (sourceNode
.isNodeType(NodeType
.NT_FILE
))
503 * Synchronises only one workspace, retrieved by name without changing its name.
505 public void setSourceWksp(String sourceWksp
) {
506 if (sourceWksp
!= null && !sourceWksp
.trim().equals("")) {
507 Map
<String
, String
> map
= new HashMap
<String
, String
>();
508 map
.put(sourceWksp
, sourceWksp
);
514 * Synchronises a map of workspaces that will be retrieved by name. If the
515 * target name is not defined (eg null or an empty string) for a given source
516 * workspace, we use the source name as target name.
518 public void setWkspMap(Map
<String
, String
> workspaceMap
) {
519 // clean the list to ease later use
520 this.workspaceMap
= new HashMap
<String
, String
>();
521 if (workspaceMap
!= null) {
522 workspaceNames
: for (String srcName
: workspaceMap
.keySet()) {
523 String targetName
= workspaceMap
.get(srcName
);
526 if (srcName
.trim().equals(""))
527 continue workspaceNames
;
528 if (targetName
== null || "".equals(targetName
.trim()))
529 targetName
= srcName
;
530 this.workspaceMap
.put(srcName
, targetName
);
533 // clean the map to ease later use
534 if (this.workspaceMap
.size() == 0)
535 this.workspaceMap
= null;
538 public void setMonitor(JcrMonitor monitor
) {
539 this.monitor
= monitor
;
542 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
543 this.repositoryFactory
= repositoryFactory
;
546 public void setSourceRepoUri(String sourceRepoUri
) {
547 this.sourceRepoUri
= sourceRepoUri
;
550 public void setSourceUsername(String sourceUsername
) {
551 this.sourceUsername
= sourceUsername
;
554 public void setSourcePassword(char[] sourcePassword
) {
555 this.sourcePassword
= sourcePassword
;
558 public void setTargetRepoUri(String targetRepoUri
) {
559 this.targetRepoUri
= targetRepoUri
;
562 public void setTargetUsername(String targetUsername
) {
563 this.targetUsername
= targetUsername
;
566 public void setTargetPassword(char[] targetPassword
) {
567 this.targetPassword
= targetPassword
;
570 public void setSourceRepository(Repository sourceRepository
) {
571 this.sourceRepository
= sourceRepository
;
574 public void setSourceCredentials(Credentials sourceCredentials
) {
575 this.sourceCredentials
= sourceCredentials
;
578 public void setTargetRepository(Repository targetRepository
) {
579 this.targetRepository
= targetRepository
;
582 public void setTargetCredentials(Credentials targetCredentials
) {
583 this.targetCredentials
= targetCredentials
;
586 public void setFilesOnly(Boolean filesOnly
) {
587 this.filesOnly
= filesOnly
;