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
.Node
;
19 import javax
.jcr
.Repository
;
20 import javax
.jcr
.RepositoryException
;
21 import javax
.jcr
.Session
;
22 import javax
.jcr
.security
.Privilege
;
24 import org
.apache
.commons
.logging
.Log
;
25 import org
.apache
.commons
.logging
.LogFactory
;
26 import org
.argeo
.ArgeoException
;
27 import org
.argeo
.ArgeoMonitor
;
28 import org
.argeo
.eclipse
.ui
.EclipseArgeoMonitor
;
29 import org
.argeo
.jcr
.JcrUtils
;
30 import org
.argeo
.slc
.SlcConstants
;
31 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
32 import org
.argeo
.slc
.client
.ui
.dist
.PrivilegedJob
;
33 import org
.argeo
.slc
.client
.ui
.dist
.utils
.CommandHelpers
;
34 import org
.argeo
.slc
.repo
.JavaRepoManager
;
35 import org
.argeo
.slc
.repo
.RepoUtils
;
36 import org
.eclipse
.core
.commands
.AbstractHandler
;
37 import org
.eclipse
.core
.commands
.ExecutionEvent
;
38 import org
.eclipse
.core
.commands
.ExecutionException
;
39 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
40 import org
.eclipse
.core
.runtime
.IStatus
;
41 import org
.eclipse
.core
.runtime
.Status
;
42 import org
.eclipse
.core
.runtime
.jobs
.Job
;
43 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
44 import org
.eclipse
.jface
.dialogs
.InputDialog
;
45 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
46 import org
.eclipse
.jface
.window
.Window
;
47 import org
.eclipse
.swt
.widgets
.Display
;
48 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
51 * Create a copy of the chosen workspace in the local Java repository using a
54 public class CopyLocalJavaWorkspace
extends AbstractHandler
{
55 private static final Log log
= LogFactory
56 .getLog(CopyLocalJavaWorkspace
.class);
58 public final static String ID
= DistPlugin
.ID
+ ".copyLocalJavaWorkspace";
59 public final static String DEFAULT_LABEL
= "Copy Java Workspace...";
60 public final static ImageDescriptor DEFAULT_ICON
= DistPlugin
61 .getImageDescriptor("icons/addItem.gif");
63 public final static String PARAM_SOURCE_WORKSPACE_NAME
= "srcWkspName";
65 // DEPENDENCY INJECTION
66 private Repository javaRepository
;
67 private JavaRepoManager javaRepoManager
;
69 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
70 String wkspName
= event
.getParameter(PARAM_SOURCE_WORKSPACE_NAME
);
72 InputDialog inputDialog
= new InputDialog(HandlerUtil
73 .getActiveWorkbenchWindow(event
).getShell(),
74 "New copy of workspace " + wkspName
,
75 "Choose a name for the workspace to create", "", null);
76 int result
= inputDialog
.open();
77 if (result
== Window
.OK
) {
78 String newWorkspaceName
= inputDialog
.getValue();
80 if (newWorkspaceName
== null || newWorkspaceName
.trim().equals("")
81 || newWorkspaceName
.trim().equals(wkspName
.trim())) {
83 .openError(HandlerUtil
.getActiveShell(event
),
84 "Non valid workspace name", newWorkspaceName
85 + " is not a valid workspace name.",
86 new Status(IStatus
.ERROR
, "not valid", 0,
90 Job copyWkspJob
= new CopyWkspJob(javaRepoManager
, javaRepository
,
91 wkspName
, newWorkspaceName
, HandlerUtil
92 .getActiveWorkbenchWindow(event
).getShell()
94 copyWkspJob
.setUser(true);
95 copyWkspJob
.schedule();
100 private static class CopyWkspJob
extends PrivilegedJob
{
102 private JavaRepoManager javaRepoManager
;
103 private Repository javaRepository
;
104 private String srcWkspName
;
105 private String targetWkspName
;
106 private Display display
;
108 public CopyWkspJob(JavaRepoManager javaRepoManager
,
109 Repository javaRepository
, String srcWkspName
,
110 String targetWkspName
, Display display
) {
111 super("Duplicate workspace");
112 this.javaRepoManager
= javaRepoManager
;
113 this.javaRepository
= javaRepository
;
114 this.srcWkspName
= srcWkspName
;
115 this.targetWkspName
= targetWkspName
;
116 this.display
= display
;
120 protected IStatus
doRun(IProgressMonitor progressMonitor
) {
121 long begin
= System
.currentTimeMillis();
123 ArgeoMonitor monitor
= new EclipseArgeoMonitor(progressMonitor
);
124 monitor
.beginTask("Copy workspace", -1);
125 monitor
.subTask("Copying nodes");
127 Session srcSession
= null;
128 Session targetSession
= null;
131 srcSession
= javaRepository
.login(srcWkspName
);
132 Node srcRootNode
= srcSession
.getRootNode();
134 // Create the workspace -
135 // FIXME will throw an error if workspace already exists
136 javaRepoManager
.createWorkspace(targetWkspName
);
137 targetSession
= javaRepository
.login(targetWkspName
);
138 Node newRootNode
= targetSession
.getRootNode();
140 RepoUtils
.copy(srcRootNode
, newRootNode
, monitor
);
141 targetSession
.save();
142 JcrUtils
.addPrivilege(targetSession
, "/",
143 SlcConstants
.ROLE_SLC
, Privilege
.JCR_ALL
);
146 display
.asyncExec(new Runnable() {
148 CommandHelpers
.callCommand(RefreshDistributionsView
.ID
);
152 } catch (RepositoryException re
) {
153 throw new ArgeoException(
154 "Unexpected error while creating the new workspace.",
157 JcrUtils
.logoutQuietly(srcSession
);
158 JcrUtils
.logoutQuietly(targetSession
);
162 long duration
= (System
.currentTimeMillis() - begin
) / 1000;// in
164 if (log
.isDebugEnabled())
165 log
.debug("Duplicated local java workspace " + srcWkspName
166 + " to workspace " + targetWkspName
+ " in "
167 + (duration
/ 60) + "min " + (duration
% 60) + "s");
168 return Status
.OK_STATUS
;
172 /* DEPENDENCY INJECTION */
173 public void setJavaRepository(Repository javaRepository
) {
174 this.javaRepository
= javaRepository
;
177 public void setJavaRepoManager(JavaRepoManager javaRepoManager
) {
178 this.javaRepoManager
= javaRepoManager
;