]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/CopyWorkspace.java
Clean session management.
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / commands / CopyWorkspace.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.slc.client.ui.dist.commands;
17
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;
25
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;
51
52 /**
53 * Create a copy of the chosen workspace in the current repository.
54 */
55
56 public class CopyWorkspace extends AbstractHandler {
57 private static final Log log = LogFactory.getLog(CopyWorkspace.class);
58
59 public final static String ID = DistPlugin.ID + ".copyWorkspace";
60 public final static String DEFAULT_LABEL = "Duplicate...";
61 public final static String PARAM_SOURCE_WORKSPACE_NAME = "srcWkspName";
62 public final static String PARAM_TARGET_REPO_PATH = "targetRepoPath";
63 public final static ImageDescriptor DEFAULT_ICON = DistPlugin
64 .getImageDescriptor("icons/addItem.gif");
65
66 // DEPENDENCY INJECTION
67 private RepositoryFactory repositoryFactory;
68 private Keyring keyring;
69 private Repository nodeRepository;
70
71 public Object execute(ExecutionEvent event) throws ExecutionException {
72
73 String targetRepoPath = event.getParameter(PARAM_TARGET_REPO_PATH);
74 String wkspName = event.getParameter(PARAM_SOURCE_WORKSPACE_NAME);
75
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();
83
84 if (newWorkspaceName == null || newWorkspaceName.trim().equals("")
85 || newWorkspaceName.trim().equals(wkspName.trim())) {
86 ErrorDialog
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,
91 "Error", null));
92 return null;
93 }
94 Job copyWkspJob = new CopyWkspJob(repositoryFactory, keyring,
95 nodeRepository, targetRepoPath, wkspName, newWorkspaceName,
96 HandlerUtil.getActiveWorkbenchWindow(event).getShell()
97 .getDisplay());
98 copyWkspJob.setUser(true);
99 copyWkspJob.schedule();
100 }
101
102 // Session nodeSession = null;
103 // Session srcSession = null;
104 // Session newSession = null;
105 // try {
106 // nodeSession = nodeRepository.login();
107 // Node repoNode = nodeSession.getNode(targetRepoPath);
108 // Repository repository = RepoUtils.getRepository(repositoryFactory,
109 // keyring, repoNode);
110 // Credentials credentials = RepoUtils.getRepositoryCredentials(
111 // keyring, repoNode);
112 //
113 // InputDialog inputDialog = new InputDialog(HandlerUtil
114 // .getActiveWorkbenchWindow(event).getShell(),
115 // "New copy of workspace " + wkspName,
116 // "Choose a name for the workspace to create", "", null);
117 // int result = inputDialog.open();
118 // if (result == Window.OK) {
119 // String newWorkspaceName = inputDialog.getValue();
120 // srcSession = repository.login(credentials, wkspName);
121 //
122 // // Create the workspace
123 // srcSession.getWorkspace().createWorkspace(newWorkspaceName);
124 // Node srcRootNode = srcSession.getRootNode();
125 // // log in the newly created workspace
126 // newSession = repository.login(credentials, newWorkspaceName);
127 // Node newRootNode = newSession.getRootNode();
128 // RepoUtils.copy(srcRootNode, newRootNode);
129 // newSession.save();
130 // JcrUtils.addPrivilege(newSession, "/", SlcConstants.ROLE_SLC,
131 // Privilege.JCR_ALL);
132 // CommandHelpers.callCommand(RefreshDistributionsView.ID);
133 // }
134 // } catch (RepositoryException re) {
135 // throw new ArgeoException(
136 // "Unexpected error while creating the new workspace.", re);
137 // } finally {
138 // JcrUtils.logoutQuietly(newSession);
139 // JcrUtils.logoutQuietly(srcSession);
140 // JcrUtils.logoutQuietly(nodeSession);
141 // }
142 return null;
143 }
144
145 private static class CopyWkspJob extends PrivilegedJob {
146
147 private RepositoryFactory repositoryFactory;
148 private Keyring keyring;
149 private Repository localRepository;
150 private String targetRepoPath;
151 private String srcWkspName;
152 private String targetWkspName;
153 private Display display;
154
155 public CopyWkspJob(RepositoryFactory repositoryFactory,
156 Keyring keyring, Repository localRepository,
157 String targetRepoPath, String srcWkspName,
158 String targetWkspName, Display display) {
159 super("Duplicate workspace");
160 this.repositoryFactory = repositoryFactory;
161 this.keyring = keyring;
162 this.localRepository = localRepository;
163 this.targetRepoPath = targetRepoPath;
164 this.srcWkspName = srcWkspName;
165 this.targetWkspName = targetWkspName;
166 this.display = display;
167 }
168
169 @Override
170 protected IStatus doRun(IProgressMonitor progressMonitor) {
171 long begin = System.currentTimeMillis();
172
173 ArgeoMonitor monitor = new EclipseArgeoMonitor(progressMonitor);
174 monitor.beginTask("Copy workspace", -1);
175 monitor.subTask("Copying nodes");
176
177 Session nodeSession = null;
178 Session srcSession = null;
179 Session newSession = null;
180 try {
181 nodeSession = localRepository.login();
182 Node repoNode = nodeSession.getNode(targetRepoPath);
183 Repository repository = RepoUtils.getRepository(
184 repositoryFactory, keyring, repoNode);
185 Credentials credentials = RepoUtils.getRepositoryCredentials(
186 keyring, repoNode);
187
188 srcSession = repository.login(credentials, srcWkspName);
189
190 // Create the workspace
191 srcSession.getWorkspace().createWorkspace(targetWkspName);
192 Node srcRootNode = srcSession.getRootNode();
193 // log in the newly created workspace
194 newSession = repository.login(credentials, targetWkspName);
195 Node newRootNode = newSession.getRootNode();
196 RepoUtils.copy(srcRootNode, newRootNode);
197 newSession.save();
198 JcrUtils.addPrivilege(newSession, "/", SlcConstants.ROLE_SLC,
199 Privilege.JCR_ALL);
200
201 display.asyncExec(new Runnable() {
202 public void run() {
203 CommandHelpers.callCommand(RefreshDistributionsView.ID);
204 }
205 });
206 monitor.worked(1);
207
208 } catch (RepositoryException re) {
209 throw new ArgeoException(
210 "Unexpected error while creating the new workspace.",
211 re);
212 } finally {
213 JcrUtils.logoutQuietly(newSession);
214 JcrUtils.logoutQuietly(srcSession);
215 JcrUtils.logoutQuietly(nodeSession);
216 }
217
218 monitor.done();
219 long duration = (System.currentTimeMillis() - begin) / 1000;// in
220 // s
221 if (log.isDebugEnabled())
222 log.debug("Created workspace " + targetWkspName + " in "
223 + (duration / 60) + "min " + (duration % 60) + "s");
224 return Status.OK_STATUS;
225 }
226
227
228 }
229
230 /* DEPENDENCY INJECTION */
231 public void setNodeRepository(Repository nodeRepository) {
232 this.nodeRepository = nodeRepository;
233 }
234
235 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
236 this.repositoryFactory = repositoryFactory;
237 }
238
239 public void setKeyring(Keyring keyring) {
240 this.keyring = keyring;
241 }
242 }