1 package org
.argeo
.slc
.client
.ui
.dist
.commands
;
4 import javax
.jcr
.Repository
;
5 import javax
.jcr
.RepositoryException
;
6 import javax
.jcr
.Session
;
7 import javax
.jcr
.security
.Privilege
;
9 import org
.apache
.commons
.logging
.Log
;
10 import org
.apache
.commons
.logging
.LogFactory
;
11 import org
.argeo
.cms
.ui
.workbench
.util
.PrivilegedJob
;
12 import org
.argeo
.eclipse
.ui
.EclipseJcrMonitor
;
13 import org
.argeo
.jcr
.JcrMonitor
;
14 import org
.argeo
.jcr
.JcrUtils
;
15 import org
.argeo
.slc
.SlcConstants
;
16 import org
.argeo
.slc
.SlcException
;
17 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
18 import org
.argeo
.slc
.client
.ui
.dist
.utils
.CommandHelpers
;
19 import org
.argeo
.slc
.repo
.JavaRepoManager
;
20 import org
.argeo
.slc
.repo
.RepoUtils
;
21 import org
.eclipse
.core
.commands
.AbstractHandler
;
22 import org
.eclipse
.core
.commands
.ExecutionEvent
;
23 import org
.eclipse
.core
.commands
.ExecutionException
;
24 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
25 import org
.eclipse
.core
.runtime
.IStatus
;
26 import org
.eclipse
.core
.runtime
.Status
;
27 import org
.eclipse
.core
.runtime
.jobs
.Job
;
28 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
29 import org
.eclipse
.jface
.dialogs
.InputDialog
;
30 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
31 import org
.eclipse
.jface
.window
.Window
;
32 import org
.eclipse
.swt
.widgets
.Display
;
33 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
36 * Create a copy of the chosen workspace in the local Java repository using a
39 public class CopyLocalJavaWorkspace
extends AbstractHandler
{
40 private static final Log log
= LogFactory
41 .getLog(CopyLocalJavaWorkspace
.class);
43 public final static String ID
= DistPlugin
.PLUGIN_ID
+ ".copyLocalJavaWorkspace";
44 public final static String DEFAULT_LABEL
= "Copy Java Workspace...";
45 public final static ImageDescriptor DEFAULT_ICON
= DistPlugin
46 .getImageDescriptor("icons/addItem.gif");
48 public final static String PARAM_SOURCE_WORKSPACE_NAME
= "srcWkspName";
50 // DEPENDENCY INJECTION
51 private Repository javaRepository
;
52 private JavaRepoManager javaRepoManager
;
54 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
55 String wkspName
= event
.getParameter(PARAM_SOURCE_WORKSPACE_NAME
);
57 InputDialog inputDialog
= new InputDialog(HandlerUtil
58 .getActiveWorkbenchWindow(event
).getShell(),
59 "New copy of workspace " + wkspName
,
60 "Choose a name for the workspace to create", "", null);
61 int result
= inputDialog
.open();
62 if (result
== Window
.OK
) {
63 String newWorkspaceName
= inputDialog
.getValue();
65 if (newWorkspaceName
== null || newWorkspaceName
.trim().equals("")
66 || newWorkspaceName
.trim().equals(wkspName
.trim())) {
68 .openError(HandlerUtil
.getActiveShell(event
),
69 "Non valid workspace name", newWorkspaceName
70 + " is not a valid workspace name.",
71 new Status(IStatus
.ERROR
, "not valid", 0,
75 Job copyWkspJob
= new CopyWkspJob(javaRepoManager
, javaRepository
,
76 wkspName
, newWorkspaceName
, HandlerUtil
77 .getActiveWorkbenchWindow(event
).getShell()
79 copyWkspJob
.setUser(true);
80 copyWkspJob
.schedule();
85 private static class CopyWkspJob
extends PrivilegedJob
{
87 private JavaRepoManager javaRepoManager
;
88 private Repository javaRepository
;
89 private String srcWkspName
;
90 private String targetWkspName
;
91 private Display display
;
93 public CopyWkspJob(JavaRepoManager javaRepoManager
,
94 Repository javaRepository
, String srcWkspName
,
95 String targetWkspName
, Display display
) {
96 super("Duplicate workspace");
97 this.javaRepoManager
= javaRepoManager
;
98 this.javaRepository
= javaRepository
;
99 this.srcWkspName
= srcWkspName
;
100 this.targetWkspName
= targetWkspName
;
101 this.display
= display
;
105 protected IStatus
doRun(IProgressMonitor progressMonitor
) {
106 long begin
= System
.currentTimeMillis();
108 JcrMonitor monitor
= new EclipseJcrMonitor(progressMonitor
);
109 monitor
.beginTask("Copy workspace", -1);
110 monitor
.subTask("Copying nodes");
112 Session srcSession
= null;
113 Session targetSession
= null;
116 srcSession
= javaRepository
.login(srcWkspName
);
117 Node srcRootNode
= srcSession
.getRootNode();
119 // Create the workspace -
120 // FIXME will throw an error if workspace already exists
121 javaRepoManager
.createWorkspace(targetWkspName
);
122 targetSession
= javaRepository
.login(targetWkspName
);
123 Node newRootNode
= targetSession
.getRootNode();
125 RepoUtils
.copy(srcRootNode
, newRootNode
, monitor
);
126 targetSession
.save();
127 JcrUtils
.addPrivilege(targetSession
, "/",
128 SlcConstants
.ROLE_SLC
, Privilege
.JCR_ALL
);
131 display
.asyncExec(new Runnable() {
133 CommandHelpers
.callCommand(RefreshDistributionsView
.ID
);
137 } catch (RepositoryException re
) {
138 throw new SlcException(
139 "Unexpected error while creating the new workspace.",
142 JcrUtils
.logoutQuietly(srcSession
);
143 JcrUtils
.logoutQuietly(targetSession
);
147 long duration
= (System
.currentTimeMillis() - begin
) / 1000;// in
149 if (log
.isDebugEnabled())
150 log
.debug("Duplicated local java workspace " + srcWkspName
151 + " to workspace " + targetWkspName
+ " in "
152 + (duration
/ 60) + "min " + (duration
% 60) + "s");
153 return Status
.OK_STATUS
;
157 /* DEPENDENCY INJECTION */
158 public void setJavaRepository(Repository javaRepository
) {
159 this.javaRepository
= javaRepository
;
162 public void setJavaRepoManager(JavaRepoManager javaRepoManager
) {
163 this.javaRepoManager
= javaRepoManager
;