1 package org
.argeo
.slc
.client
.ui
.dist
.commands
;
3 import javax
.jcr
.Credentials
;
4 import javax
.jcr
.NoSuchWorkspaceException
;
6 import javax
.jcr
.Repository
;
7 import javax
.jcr
.RepositoryException
;
8 import javax
.jcr
.RepositoryFactory
;
9 import javax
.jcr
.Session
;
10 import javax
.jcr
.query
.Query
;
11 import javax
.jcr
.query
.QueryResult
;
13 import org
.apache
.commons
.logging
.Log
;
14 import org
.apache
.commons
.logging
.LogFactory
;
15 import org
.argeo
.api
.security
.Keyring
;
16 import org
.argeo
.eclipse
.ui
.EclipseJcrMonitor
;
17 import org
.argeo
.jcr
.JcrMonitor
;
18 import org
.argeo
.jcr
.JcrUtils
;
19 import org
.argeo
.slc
.SlcException
;
20 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
21 import org
.argeo
.slc
.repo
.RepoUtils
;
22 import org
.eclipse
.core
.commands
.AbstractHandler
;
23 import org
.eclipse
.core
.commands
.ExecutionEvent
;
24 import org
.eclipse
.core
.commands
.ExecutionException
;
25 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
26 import org
.eclipse
.core
.runtime
.IStatus
;
27 import org
.eclipse
.core
.runtime
.Status
;
28 import org
.eclipse
.core
.runtime
.jobs
.Job
;
30 /** Merge two workspaces */
31 public class MergeWorkspaces
extends AbstractHandler
{
32 private final static Log log
= LogFactory
.getLog(MergeWorkspaces
.class);
34 public final static String ID
= DistPlugin
.PLUGIN_ID
+ ".mergeWorkspaces";
35 public final static String DEFAULT_LABEL
= "Merge";
37 public final static String PARAM_SOURCE_WORKSPACE_NAME
= "srcWkspName";
38 public final static String PARAM_SOURCE_REPO_PATH
= "srcRepoPath";
39 public final static String PARAM_TARGET_WORKSPACE_NAME
= "targetWkspName";
40 public final static String PARAM_TARGET_REPO_PATH
= "targetRepoPath";
42 // DEPENDENCY INJECTION
43 private RepositoryFactory repositoryFactory
;
44 private Keyring keyring
;
45 private Repository nodeRepository
;
47 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
48 String targetRepoPath
= event
.getParameter(PARAM_TARGET_REPO_PATH
);
49 String targetWkspName
= event
.getParameter(PARAM_TARGET_WORKSPACE_NAME
);
50 String sourceRepoPath
= event
.getParameter(PARAM_SOURCE_REPO_PATH
);
51 String sourceWkspName
= event
.getParameter(PARAM_SOURCE_WORKSPACE_NAME
);
53 Session nodeSession
= null;
55 nodeSession
= nodeRepository
.login();
56 Node srcRepoNode
= nodeSession
.getNode(sourceRepoPath
);
57 Repository srcRepository
= RepoUtils
.getRepository(repositoryFactory
, keyring
, srcRepoNode
);
58 Credentials srcCredentials
= RepoUtils
.getRepositoryCredentials(keyring
, srcRepoNode
);
60 Node targetRepoNode
= nodeSession
.getNode(targetRepoPath
);
61 Repository targetRepository
= RepoUtils
.getRepository(repositoryFactory
, keyring
, targetRepoNode
);
62 Credentials targetCredentials
= RepoUtils
.getRepositoryCredentials(keyring
, targetRepoNode
);
64 // String msg = "Are you sure you want to merge distribution ["
65 // + sourceWkspName + "] in distribution [" + targetWkspName
68 // boolean result = MessageDialog.openConfirm(
69 // HandlerUtil.getActiveShell(event), "Confirm Merge", msg);
72 // Open sessions here since the background thread
73 // won't necessarily be authenticated.
74 // Job should close the sessions.
75 Session sourceSession
= srcRepository
.login(srcCredentials
, sourceWkspName
);
76 Session targetSession
;
78 targetSession
= targetRepository
.login(targetCredentials
, targetWkspName
);
79 } catch (NoSuchWorkspaceException e
) {
80 Session defaultSession
= targetRepository
.login(targetCredentials
);
82 defaultSession
.getWorkspace().createWorkspace(targetWkspName
);
83 } catch (Exception e1
) {
84 throw new SlcException("Cannot create new workspace " + targetWkspName
, e
);
86 JcrUtils
.logoutQuietly(defaultSession
);
88 targetSession
= targetRepository
.login(targetCredentials
, targetWkspName
);
91 Job workspaceMergeJob
= new WorkspaceMergeJob(sourceSession
, targetSession
);
92 workspaceMergeJob
.setUser(true);
93 workspaceMergeJob
.schedule();
94 } catch (RepositoryException re
) {
95 throw new SlcException("Unexpected error while merging workspaces.", re
);
97 JcrUtils
.logoutQuietly(nodeSession
);
102 private static class WorkspaceMergeJob
extends Job
{
103 private Session sourceSession
;
104 private Session targetSession
;
106 public WorkspaceMergeJob(Session sourceSession
, Session targetSession
) {
107 super("Workspace merge");
108 this.sourceSession
= sourceSession
;
109 this.targetSession
= targetSession
;
113 protected IStatus
run(IProgressMonitor eclipseMonitor
) {
114 long begin
= System
.currentTimeMillis();
116 Query countQuery
= sourceSession
.getWorkspace().getQueryManager()
117 .createQuery("select file from [nt:file] as file", Query
.JCR_SQL2
);
118 QueryResult result
= countQuery
.execute();
119 Long expectedCount
= result
.getNodes().getSize();
120 if (log
.isDebugEnabled())
121 log
.debug("Will copy " + expectedCount
+ " files...");
123 JcrMonitor monitor
= new EclipseJcrMonitor(eclipseMonitor
);
124 eclipseMonitor
.beginTask("Copy files", expectedCount
.intValue());
126 Long count
= JcrUtils
.copyFiles(sourceSession
.getRootNode(), targetSession
.getRootNode(), true, monitor
,
130 long duration
= (System
.currentTimeMillis() - begin
) / 1000;// in
132 if (log
.isDebugEnabled())
133 log
.debug("Copied " + count
+ " files in " + (duration
/ 60) + "min " + (duration
% 60) + "s");
135 return Status
.OK_STATUS
;
136 } catch (RepositoryException e
) {
137 return new Status(IStatus
.ERROR
, DistPlugin
.PLUGIN_ID
, "Cannot merge", e
);
139 JcrUtils
.logoutQuietly(sourceSession
);
140 JcrUtils
.logoutQuietly(targetSession
);
145 /* DEPENDENCY INJECTION */
146 public void setNodeRepository(Repository nodeRepository
) {
147 this.nodeRepository
= nodeRepository
;
150 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
151 this.repositoryFactory
= repositoryFactory
;
154 public void setKeyring(Keyring keyring
) {
155 this.keyring
= keyring
;