]> 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/NormalizeWorkspace.java
Fix single sourcing issue on file download
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / commands / NormalizeWorkspace.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.Property;
21 import javax.jcr.Repository;
22 import javax.jcr.RepositoryException;
23 import javax.jcr.RepositoryFactory;
24 import javax.jcr.Session;
25 import javax.jcr.nodetype.NodeType;
26 import javax.jcr.query.Query;
27 import javax.jcr.query.QueryResult;
28 import javax.jcr.util.TraversingItemVisitor;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.argeo.ArgeoMonitor;
33 import org.argeo.eclipse.ui.EclipseArgeoMonitor;
34 import org.argeo.jcr.JcrUtils;
35 import org.argeo.slc.SlcException;
36 import org.argeo.slc.client.ui.dist.DistPlugin;
37 import org.argeo.slc.jcr.SlcNames;
38 import org.argeo.slc.repo.ArtifactIndexer;
39 import org.argeo.slc.repo.ModularDistributionIndexer;
40 import org.argeo.slc.repo.JarFileIndexer;
41 import org.argeo.slc.repo.PdeSourcesIndexer;
42 import org.argeo.slc.repo.RepoConstants;
43 import org.argeo.slc.repo.RepoUtils;
44 import org.argeo.util.security.Keyring;
45 import org.eclipse.core.commands.AbstractHandler;
46 import org.eclipse.core.commands.ExecutionEvent;
47 import org.eclipse.core.commands.ExecutionException;
48 import org.eclipse.core.runtime.IProgressMonitor;
49 import org.eclipse.core.runtime.IStatus;
50 import org.eclipse.core.runtime.Status;
51 import org.eclipse.core.runtime.jobs.Job;
52 import org.eclipse.jface.dialogs.MessageDialog;
53 import org.eclipse.jface.resource.ImageDescriptor;
54
55 /**
56 * Force the indexing of a given workspace by making sure than Maven and OSGi
57 * metadata are consistent. This mechanism normally relies on JCR Listeners but
58 * must sometimes be triggered manually
59 */
60 public class NormalizeWorkspace extends AbstractHandler implements SlcNames {
61 private final static Log log = LogFactory.getLog(NormalizeWorkspace.class);
62
63 public final static String ID = DistPlugin.ID + ".normalizeWorkspace";
64 public final static ImageDescriptor DEFAULT_ICON = DistPlugin
65 .getImageDescriptor("icons/normalize.gif");
66
67 public final static String PARAM_WORKSPACE_NAME = "workspaceName";
68 public final static String PARAM_TARGET_REPO_PATH = "targetRepoPath";
69
70 private String artifactBasePath = RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH;
71
72 // DEPENDENCY INJECTION
73 private RepositoryFactory repositoryFactory;
74 private Keyring keyring;
75 private Repository repository;
76
77 // Relevant default node indexers
78 private ArtifactIndexer artifactIndexer = new ArtifactIndexer();
79 private JarFileIndexer jarFileIndexer = new JarFileIndexer();
80 private ModularDistributionIndexer distBundleIndexer = new ModularDistributionIndexer();
81 private PdeSourcesIndexer pdeSourceIndexer = new PdeSourcesIndexer();
82
83 public Object execute(ExecutionEvent event) throws ExecutionException {
84 String targetRepoPath = event.getParameter(PARAM_TARGET_REPO_PATH);
85 String wkspName = event.getParameter(PARAM_WORKSPACE_NAME);
86
87 Session currSession = null;
88 NormalizeJob job;
89 try {
90 String msg = "Your are about to normalize workspace: "
91 + wkspName
92 + ".\nThis will index OSGi bundles and Maven artifacts, "
93 + "it will also convert Maven sources to PDE Sources if needed.\n"
94 + "Note that no information will be overwritten: "
95 + "all existing information are kept."
96 + "\n\n Do you really want to proceed ?";
97
98 if (!MessageDialog.openConfirm(DistPlugin.getDefault()
99 .getWorkbench().getDisplay().getActiveShell(),
100 "Confirm workspace normalization", msg))
101 return null;
102
103 currSession = repository.login();
104 Node repoNode = currSession.getNode(targetRepoPath);
105 Repository repository = RepoUtils.getRepository(repositoryFactory,
106 keyring, repoNode);
107 Credentials credentials = RepoUtils.getRepositoryCredentials(
108 keyring, repoNode);
109
110 job = new NormalizeJob(repository.login(credentials, wkspName));
111 job.setUser(true);
112 job.schedule();
113 } catch (RepositoryException e) {
114 throw new SlcException("Cannot normalize " + wkspName, e);
115 } finally {
116 JcrUtils.logoutQuietly(currSession);
117 }
118 return null;
119 }
120
121 private class NormalizeJob extends Job {
122 private Session session;
123
124 public NormalizeJob(Session session) {
125 super("Normalize Distribution");
126 this.session = session;
127 }
128
129 @Override
130 protected IStatus run(IProgressMonitor progressMonitor) {
131 try {
132 ArgeoMonitor monitor = new EclipseArgeoMonitor(progressMonitor);
133 // Normalize artifacts
134 Query countQuery = session
135 .getWorkspace()
136 .getQueryManager()
137 .createQuery("select file from [nt:file] as file",
138 Query.JCR_SQL2);
139 QueryResult result = countQuery.execute();
140 Long expectedCount = result.getNodes().getSize();
141 monitor.beginTask("Normalize artifacts of "
142 + session.getWorkspace().getName(),
143 expectedCount.intValue());
144 NormalizingTraverser tiv = new NormalizingTraverser(monitor);
145 Node artifactBaseNode = session.getNode(artifactBasePath);
146 artifactBaseNode.accept(tiv);
147 } catch (Exception e) {
148 log.error("Error normalizing workspace "
149 + session.getWorkspace().getName() + ": "
150 + e.getMessage());
151 if (log.isErrorEnabled())
152 e.printStackTrace();
153 return new Status(IStatus.ERROR, DistPlugin.ID,
154 "Cannot normalize distribution "
155 + session.getWorkspace().getName(), e);
156 } finally {
157 JcrUtils.logoutQuietly(session);
158 }
159 return Status.OK_STATUS;
160 }
161 }
162
163 private class NormalizingTraverser extends TraversingItemVisitor {
164 ArgeoMonitor monitor;
165
166 public NormalizingTraverser(ArgeoMonitor monitor) {
167 super();
168 this.monitor = monitor;
169 }
170
171 @Override
172 protected void entering(Property property, int level)
173 throws RepositoryException {
174 }
175
176 @Override
177 protected void entering(Node node, int level)
178 throws RepositoryException {
179 if (node.isNodeType(NodeType.NT_FILE)) {
180 if (node.getName().endsWith("-sources.jar")) {
181 monitor.subTask(node.getName());
182 pdeSourceIndexer.index(node);
183 node.getSession().save();
184 monitor.worked(1);
185 if (log.isDebugEnabled())
186 log.debug("Processed source artifact " + node.getPath());
187 } else if (node.getName().endsWith(".jar")) {
188 if (jarFileIndexer.support(node.getPath()))
189 if (artifactIndexer.support(node.getPath())) {
190 monitor.subTask(node.getName());
191 artifactIndexer.index(node);
192 jarFileIndexer.index(node);
193 distBundleIndexer.index(node);
194 if (node.getSession().hasPendingChanges()) {
195 node.getSession().save();
196 if (log.isDebugEnabled())
197 log.debug("Processed jar artifact "
198 + node.getPath());
199 }
200 monitor.worked(1);
201 }
202 } else if (node.getName().endsWith(".pom")) {
203 if (distBundleIndexer.support(node.getPath()))
204 distBundleIndexer.index(node);
205 if (artifactIndexer.support(node.getPath()))
206 artifactIndexer.index(node);
207 if (node.getSession().hasPendingChanges()) {
208 node.getSession().save();
209 if (log.isDebugEnabled())
210 log.debug("Processed pom artifact "
211 + node.getPath());
212 }
213 monitor.worked(1);
214 } else {
215 monitor.worked(1);
216 }
217 }
218 }
219
220 @Override
221 protected void leaving(Property property, int level)
222 throws RepositoryException {
223 }
224
225 @Override
226 protected void leaving(Node node, int level) throws RepositoryException {
227 }
228 }
229
230 /* DEPENDENCY INJECTION */
231 public void setNodeRepository(Repository nodeRepository) {
232 this.repository = 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 }