2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.client
.ui
.dist
.commands
;
18 import javax
.jcr
.Credentials
;
19 import javax
.jcr
.Node
;
20 import javax
.jcr
.Repository
;
21 import javax
.jcr
.RepositoryException
;
22 import javax
.jcr
.RepositoryFactory
;
23 import javax
.jcr
.Session
;
24 import javax
.jcr
.security
.Privilege
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.argeo
.ArgeoException
;
29 import org
.argeo
.ArgeoMonitor
;
30 import org
.argeo
.eclipse
.ui
.EclipseArgeoMonitor
;
31 import org
.argeo
.jcr
.JcrUtils
;
32 import org
.argeo
.slc
.SlcConstants
;
33 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
34 import org
.argeo
.slc
.client
.ui
.dist
.PrivilegedJob
;
35 import org
.argeo
.slc
.client
.ui
.dist
.utils
.CommandHelpers
;
36 import org
.argeo
.slc
.repo
.RepoUtils
;
37 import org
.argeo
.util
.security
.Keyring
;
38 import org
.eclipse
.core
.commands
.AbstractHandler
;
39 import org
.eclipse
.core
.commands
.ExecutionEvent
;
40 import org
.eclipse
.core
.commands
.ExecutionException
;
41 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
42 import org
.eclipse
.core
.runtime
.IStatus
;
43 import org
.eclipse
.core
.runtime
.Status
;
44 import org
.eclipse
.core
.runtime
.jobs
.Job
;
45 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
46 import org
.eclipse
.jface
.dialogs
.InputDialog
;
47 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
48 import org
.eclipse
.jface
.window
.Window
;
49 import org
.eclipse
.swt
.widgets
.Display
;
50 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
53 * Create a copy of the chosen workspace in a remote repository.
55 public class CopyWorkspace
extends AbstractHandler
{
56 private static final Log log
= LogFactory
.getLog(CopyWorkspace
.class);
58 public final static String ID
= DistPlugin
.ID
+ ".copyWorkspace";
59 public final static String DEFAULT_LABEL
= "Duplicate...";
60 public final static ImageDescriptor DEFAULT_ICON
= DistPlugin
61 .getImageDescriptor("icons/addItem.gif");
63 public final static String PARAM_SOURCE_WORKSPACE_NAME
= "srcWkspName";
64 public final static String PARAM_TARGET_REPO_PATH
= "targetRepoPath";
66 // DEPENDENCY INJECTION
67 private RepositoryFactory repositoryFactory
;
68 private Keyring keyring
;
69 private Repository nodeRepository
;
71 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
73 String targetRepoPath
= event
.getParameter(PARAM_TARGET_REPO_PATH
);
74 String wkspName
= event
.getParameter(PARAM_SOURCE_WORKSPACE_NAME
);
76 InputDialog inputDialog
= new InputDialog(HandlerUtil
77 .getActiveWorkbenchWindow(event
).getShell(),
78 "New copy of workspace " + wkspName
,
79 "Choose a name for the workspace to create", "", null);
80 int result
= inputDialog
.open();
81 if (result
== Window
.OK
) {
82 String newWorkspaceName
= inputDialog
.getValue();
84 if (newWorkspaceName
== null || newWorkspaceName
.trim().equals("")
85 || newWorkspaceName
.trim().equals(wkspName
.trim())) {
87 .openError(HandlerUtil
.getActiveShell(event
),
88 "Non valid workspace name", newWorkspaceName
89 + " is not a valid workspace name.",
90 new Status(IStatus
.ERROR
, "not valid", 0,
94 Job copyWkspJob
= new CopyWkspJob(repositoryFactory
, keyring
,
95 nodeRepository
, targetRepoPath
, wkspName
, newWorkspaceName
,
96 HandlerUtil
.getActiveWorkbenchWindow(event
).getShell()
98 copyWkspJob
.setUser(true);
99 copyWkspJob
.schedule();
104 private static class CopyWkspJob
extends PrivilegedJob
{
106 private RepositoryFactory repositoryFactory
;
107 private Keyring keyring
;
108 private Repository localRepository
;
109 private String targetRepoPath
;
110 private String srcWkspName
;
111 private String targetWkspName
;
112 private Display display
;
114 public CopyWkspJob(RepositoryFactory repositoryFactory
,
115 Keyring keyring
, Repository localRepository
,
116 String targetRepoPath
, String srcWkspName
,
117 String targetWkspName
, Display display
) {
118 super("Duplicate workspace");
119 this.repositoryFactory
= repositoryFactory
;
120 this.keyring
= keyring
;
121 this.localRepository
= localRepository
;
122 this.targetRepoPath
= targetRepoPath
;
123 this.srcWkspName
= srcWkspName
;
124 this.targetWkspName
= targetWkspName
;
125 this.display
= display
;
129 protected IStatus
doRun(IProgressMonitor progressMonitor
) {
130 long begin
= System
.currentTimeMillis();
132 ArgeoMonitor monitor
= new EclipseArgeoMonitor(progressMonitor
);
133 monitor
.beginTask("Copy workspace", -1);
134 monitor
.subTask("Copying nodes");
136 Session nodeSession
= null;
137 Session srcSession
= null;
138 Session newSession
= null;
140 nodeSession
= localRepository
.login();
141 Node repoNode
= nodeSession
.getNode(targetRepoPath
);
142 Repository repository
= RepoUtils
.getRepository(
143 repositoryFactory
, keyring
, repoNode
);
144 Credentials credentials
= RepoUtils
.getRepositoryCredentials(
147 srcSession
= repository
.login(credentials
, srcWkspName
);
149 // Create the workspace
150 srcSession
.getWorkspace().createWorkspace(targetWkspName
);
151 Node srcRootNode
= srcSession
.getRootNode();
152 // log in the newly created workspace
153 newSession
= repository
.login(credentials
, targetWkspName
);
154 Node newRootNode
= newSession
.getRootNode();
155 RepoUtils
.copy(srcRootNode
, newRootNode
, monitor
);
157 JcrUtils
.addPrivilege(newSession
, "/", SlcConstants
.ROLE_SLC
,
160 display
.asyncExec(new Runnable() {
162 CommandHelpers
.callCommand(RefreshDistributionsView
.ID
);
167 } catch (RepositoryException re
) {
168 throw new ArgeoException(
169 "Unexpected error while creating the new workspace.",
172 JcrUtils
.logoutQuietly(newSession
);
173 JcrUtils
.logoutQuietly(srcSession
);
174 JcrUtils
.logoutQuietly(nodeSession
);
178 long duration
= (System
.currentTimeMillis() - begin
) / 1000;// in
180 if (log
.isDebugEnabled())
181 log
.debug("Created workspace " + targetWkspName
+ " in "
182 + (duration
/ 60) + "min " + (duration
% 60) + "s");
183 return Status
.OK_STATUS
;
189 /* DEPENDENCY INJECTION */
190 public void setNodeRepository(Repository nodeRepository
) {
191 this.nodeRepository
= nodeRepository
;
194 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
195 this.repositoryFactory
= repositoryFactory
;
198 public void setKeyring(Keyring keyring
) {
199 this.keyring
= keyring
;