1 package org
.argeo
.slc
.client
.ui
.dist
.commands
;
3 import javax
.jcr
.Credentials
;
5 import javax
.jcr
.Repository
;
6 import javax
.jcr
.RepositoryException
;
7 import javax
.jcr
.RepositoryFactory
;
8 import javax
.jcr
.Session
;
9 import javax
.jcr
.security
.Privilege
;
11 import org
.argeo
.api
.cms
.CmsLog
;
12 import org
.argeo
.cms
.security
.Keyring
;
13 import org
.argeo
.cms
.ui
.workbench
.util
.PrivilegedJob
;
14 import org
.argeo
.eclipse
.ui
.jcr
.EclipseJcrMonitor
;
15 import org
.argeo
.jcr
.JcrMonitor
;
16 import org
.argeo
.jcr
.JcrUtils
;
17 import org
.argeo
.slc
.SlcConstants
;
18 import org
.argeo
.slc
.SlcException
;
19 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
20 import org
.argeo
.slc
.client
.ui
.dist
.utils
.CommandHelpers
;
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
;
29 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
30 import org
.eclipse
.jface
.dialogs
.InputDialog
;
31 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
32 import org
.eclipse
.jface
.window
.Window
;
33 import org
.eclipse
.swt
.widgets
.Display
;
34 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
36 /** Create a copy of the chosen workspace in a remote repository */
37 public class CopyWorkspace
extends AbstractHandler
{
38 private static final CmsLog log
= CmsLog
.getLog(CopyWorkspace
.class);
40 public final static String ID
= DistPlugin
.PLUGIN_ID
+ ".copyWorkspace";
41 public final static String DEFAULT_LABEL
= "Duplicate...";
42 public final static ImageDescriptor DEFAULT_ICON
= DistPlugin
43 .getImageDescriptor("icons/addItem.gif");
45 public final static String PARAM_SOURCE_WORKSPACE_NAME
= "srcWkspName";
46 public final static String PARAM_TARGET_REPO_PATH
= "targetRepoPath";
48 // DEPENDENCY INJECTION
49 private RepositoryFactory repositoryFactory
;
50 private Keyring keyring
;
51 private Repository nodeRepository
;
53 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
55 String targetRepoPath
= event
.getParameter(PARAM_TARGET_REPO_PATH
);
56 String wkspName
= event
.getParameter(PARAM_SOURCE_WORKSPACE_NAME
);
58 InputDialog inputDialog
= new InputDialog(HandlerUtil
59 .getActiveWorkbenchWindow(event
).getShell(),
60 "New copy of workspace " + wkspName
,
61 "Choose a name for the workspace to create", "", null);
62 int result
= inputDialog
.open();
63 if (result
== Window
.OK
) {
64 String newWorkspaceName
= inputDialog
.getValue();
66 if (newWorkspaceName
== null || newWorkspaceName
.trim().equals("")
67 || newWorkspaceName
.trim().equals(wkspName
.trim())) {
69 .openError(HandlerUtil
.getActiveShell(event
),
70 "Non valid workspace name", newWorkspaceName
71 + " is not a valid workspace name.",
72 new Status(IStatus
.ERROR
, "not valid", 0,
76 Job copyWkspJob
= new CopyWkspJob(repositoryFactory
, keyring
,
77 nodeRepository
, targetRepoPath
, wkspName
, newWorkspaceName
,
78 HandlerUtil
.getActiveWorkbenchWindow(event
).getShell()
80 copyWkspJob
.setUser(true);
81 copyWkspJob
.schedule();
86 private static class CopyWkspJob
extends PrivilegedJob
{
88 private RepositoryFactory repositoryFactory
;
89 private Keyring keyring
;
90 private Repository localRepository
;
91 private String targetRepoPath
;
92 private String srcWkspName
;
93 private String targetWkspName
;
94 private Display display
;
96 public CopyWkspJob(RepositoryFactory repositoryFactory
,
97 Keyring keyring
, Repository localRepository
,
98 String targetRepoPath
, String srcWkspName
,
99 String targetWkspName
, Display display
) {
100 super("Duplicate workspace");
101 this.repositoryFactory
= repositoryFactory
;
102 this.keyring
= keyring
;
103 this.localRepository
= localRepository
;
104 this.targetRepoPath
= targetRepoPath
;
105 this.srcWkspName
= srcWkspName
;
106 this.targetWkspName
= targetWkspName
;
107 this.display
= display
;
111 protected IStatus
doRun(IProgressMonitor progressMonitor
) {
112 long begin
= System
.currentTimeMillis();
114 JcrMonitor monitor
= new EclipseJcrMonitor(progressMonitor
);
115 monitor
.beginTask("Copy workspace", -1);
116 monitor
.subTask("Copying nodes");
118 Session nodeSession
= null;
119 Session srcSession
= null;
120 Session newSession
= null;
122 nodeSession
= localRepository
.login();
123 Node repoNode
= nodeSession
.getNode(targetRepoPath
);
124 Repository repository
= RepoUtils
.getRepository(
125 repositoryFactory
, keyring
, repoNode
);
126 Credentials credentials
= RepoUtils
.getRepositoryCredentials(
129 srcSession
= repository
.login(credentials
, srcWkspName
);
131 // Create the workspace
132 srcSession
.getWorkspace().createWorkspace(targetWkspName
);
133 Node srcRootNode
= srcSession
.getRootNode();
134 // log in the newly created workspace
135 newSession
= repository
.login(credentials
, targetWkspName
);
136 Node newRootNode
= newSession
.getRootNode();
137 RepoUtils
.copy(srcRootNode
, newRootNode
, monitor
);
139 JcrUtils
.addPrivilege(newSession
, "/", SlcConstants
.ROLE_SLC
,
142 display
.asyncExec(new Runnable() {
144 CommandHelpers
.callCommand(RefreshDistributionsView
.ID
);
149 } catch (RepositoryException re
) {
150 throw new SlcException(
151 "Unexpected error while creating the new workspace.",
154 JcrUtils
.logoutQuietly(newSession
);
155 JcrUtils
.logoutQuietly(srcSession
);
156 JcrUtils
.logoutQuietly(nodeSession
);
160 long duration
= (System
.currentTimeMillis() - begin
) / 1000;// in
162 if (log
.isDebugEnabled())
163 log
.debug("Created workspace " + targetWkspName
+ " in "
164 + (duration
/ 60) + "min " + (duration
% 60) + "s");
165 return Status
.OK_STATUS
;
170 /* DEPENDENCY INJECTION */
171 public void setNodeRepository(Repository nodeRepository
) {
172 this.nodeRepository
= nodeRepository
;
175 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
176 this.repositoryFactory
= repositoryFactory
;
179 public void setKeyring(Keyring keyring
) {
180 this.keyring
= keyring
;