]> 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/MergeWorkspaces.java
Do not clean OSGi runtime
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / commands / MergeWorkspaces.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.NoSuchWorkspaceException;
20 import javax.jcr.Node;
21 import javax.jcr.Repository;
22 import javax.jcr.RepositoryException;
23 import javax.jcr.RepositoryFactory;
24 import javax.jcr.Session;
25 import javax.jcr.query.Query;
26 import javax.jcr.query.QueryResult;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.argeo.ArgeoMonitor;
31 import org.argeo.eclipse.ui.EclipseArgeoMonitor;
32 import org.argeo.jcr.JcrUtils;
33 import org.argeo.slc.SlcException;
34 import org.argeo.slc.client.ui.dist.DistPlugin;
35 import org.argeo.slc.repo.RepoUtils;
36 import org.argeo.util.security.Keyring;
37 import org.eclipse.core.commands.AbstractHandler;
38 import org.eclipse.core.commands.ExecutionEvent;
39 import org.eclipse.core.commands.ExecutionException;
40 import org.eclipse.core.runtime.IProgressMonitor;
41 import org.eclipse.core.runtime.IStatus;
42 import org.eclipse.core.runtime.Status;
43 import org.eclipse.core.runtime.jobs.Job;
44
45 /**
46 * Merge two workspaces
47 */
48
49 public class MergeWorkspaces extends AbstractHandler {
50 private final static Log log = LogFactory.getLog(MergeWorkspaces.class);
51
52 public final static String ID = DistPlugin.ID + ".mergeWorkspaces";
53 public final static String DEFAULT_LABEL = "Merge";
54 public final static String PARAM_SOURCE_WORKSPACE_NAME = "srcWkspName";
55 public final static String PARAM_SOURCE_REPO_PATH = "srcRepoPath";
56 public final static String PARAM_TARGET_WORKSPACE_NAME = "targetWkspName";
57 public final static String PARAM_TARGET_REPO_PATH = "targetRepoPath";
58
59 // DEPENDENCY INJECTION
60 private RepositoryFactory repositoryFactory;
61 private Keyring keyring;
62 private Repository nodeRepository;
63
64 public Object execute(ExecutionEvent event) throws ExecutionException {
65
66 String targetRepoPath = event.getParameter(PARAM_TARGET_REPO_PATH);
67 String targetWkspName = event.getParameter(PARAM_TARGET_WORKSPACE_NAME);
68 String sourceRepoPath = event.getParameter(PARAM_SOURCE_REPO_PATH);
69 String sourceWkspName = event.getParameter(PARAM_SOURCE_WORKSPACE_NAME);
70
71 Session nodeSession = null;
72 try {
73 nodeSession = nodeRepository.login();
74 Node srcRepoNode = nodeSession.getNode(sourceRepoPath);
75 Repository srcRepository = RepoUtils.getRepository(
76 repositoryFactory, keyring, srcRepoNode);
77 Credentials srcCredentials = RepoUtils.getRepositoryCredentials(
78 keyring, srcRepoNode);
79
80 Node targetRepoNode = nodeSession.getNode(targetRepoPath);
81 Repository targetRepository = RepoUtils.getRepository(
82 repositoryFactory, keyring, targetRepoNode);
83 Credentials targetCredentials = RepoUtils.getRepositoryCredentials(
84 keyring, targetRepoNode);
85
86 // String msg = "Are you sure you want to merge distribution ["
87 // + sourceWkspName + "] in distribution [" + targetWkspName
88 // + "] ?";
89 //
90 // boolean result = MessageDialog.openConfirm(
91 // HandlerUtil.getActiveShell(event), "Confirm Merge", msg);
92
93 // if (result) {
94 // Open sessions here since the background thread
95 // won't necessarily be authenticated.
96 // Job should close the sessions.
97 Session sourceSession = srcRepository.login(srcCredentials,
98 sourceWkspName);
99 Session targetSession;
100 try {
101 targetSession = targetRepository.login(targetCredentials,
102 targetWkspName);
103 } catch (NoSuchWorkspaceException e) {
104 Session defaultSession = targetRepository
105 .login(targetCredentials);
106 try {
107 defaultSession.getWorkspace().createWorkspace(
108 targetWkspName);
109 } catch (Exception e1) {
110 throw new SlcException("Cannot create new workspace "
111 + targetWkspName, e);
112 } finally {
113 JcrUtils.logoutQuietly(defaultSession);
114 }
115 targetSession = targetRepository.login(targetCredentials,
116 targetWkspName);
117 }
118
119 Job workspaceMergeJob = new WorkspaceMergeJob(sourceSession,
120 targetSession);
121 workspaceMergeJob.setUser(true);
122 workspaceMergeJob.schedule();
123 } catch (RepositoryException re) {
124 throw new SlcException(
125 "Unexpected error while merging workspaces.", re);
126 } finally {
127 JcrUtils.logoutQuietly(nodeSession);
128 }
129 return null;
130 }
131
132 private static class WorkspaceMergeJob extends Job {
133 private Session sourceSession;
134 private Session targetSession;
135
136 public WorkspaceMergeJob(Session sourceSession, Session targetSession) {
137 super("Workspace merge");
138 this.sourceSession = sourceSession;
139 this.targetSession = targetSession;
140 }
141
142 @Override
143 protected IStatus run(IProgressMonitor eclipseMonitor) {
144 long begin = System.currentTimeMillis();
145 try {
146 Query countQuery = sourceSession
147 .getWorkspace()
148 .getQueryManager()
149 .createQuery("select file from [nt:file] as file",
150 Query.JCR_SQL2);
151 QueryResult result = countQuery.execute();
152 Long expectedCount = result.getNodes().getSize();
153 if (log.isDebugEnabled())
154 log.debug("Will copy " + expectedCount + " files...");
155
156 ArgeoMonitor monitor = new EclipseArgeoMonitor(eclipseMonitor);
157 eclipseMonitor
158 .beginTask("Copy files", expectedCount.intValue());
159
160 Long count = JcrUtils.copyFiles(sourceSession.getRootNode(),
161 targetSession.getRootNode(), true, monitor);
162
163 monitor.done();
164 long duration = (System.currentTimeMillis() - begin) / 1000;// in
165 // s
166 if (log.isDebugEnabled())
167 log.debug("Copied " + count + " files in "
168 + (duration / 60) + "min " + (duration % 60) + "s");
169
170 return Status.OK_STATUS;
171 } catch (RepositoryException e) {
172 return new Status(IStatus.ERROR, DistPlugin.ID, "Cannot merge",
173 e);
174 } finally {
175 JcrUtils.logoutQuietly(sourceSession);
176 JcrUtils.logoutQuietly(targetSession);
177 }
178 }
179 }
180
181 /* DEPENDENCY INJECTION */
182 public void setNodeRepository(Repository nodeRepository) {
183 this.nodeRepository = nodeRepository;
184 }
185
186 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
187 this.repositoryFactory = repositoryFactory;
188 }
189
190 public void setKeyring(Keyring keyring) {
191 this.keyring = keyring;
192 }
193 }