]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/commands/NormalizeWorkspace.java
Adapt to Commons 2.x and some subtle comments cleaning.
[gpl/argeo-slc.git] / org.argeo.slc.client.ui.dist / src / 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.JarFileIndexer;
40 import org.argeo.slc.repo.ModularDistributionIndexer;
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.PLUGIN_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 PdeSourcesIndexer pdeSourceIndexer = new PdeSourcesIndexer();
79 // WARNING Order is important: must be called in the following order.
80 private ModularDistributionIndexer modularDistributionIndexer = new ModularDistributionIndexer();
81 private JarFileIndexer jarFileIndexer = new JarFileIndexer();
82 private ArtifactIndexer artifactIndexer = new ArtifactIndexer();
83
84 public Object execute(ExecutionEvent event) throws ExecutionException {
85 String targetRepoPath = event.getParameter(PARAM_TARGET_REPO_PATH);
86 String wkspName = event.getParameter(PARAM_WORKSPACE_NAME);
87
88 Session currSession = null;
89 NormalizeJob job;
90 try {
91 String msg = "Your are about to normalize workspace: "
92 + wkspName
93 + ".\nThis will index OSGi bundles and Maven artifacts, "
94 + "it will also convert Maven sources to PDE Sources if needed.\n"
95 + "Note that no information will be overwritten: "
96 + "all existing information are kept."
97 + "\n\n Do you really want to proceed ?";
98
99 if (!MessageDialog.openConfirm(DistPlugin.getDefault()
100 .getWorkbench().getDisplay().getActiveShell(),
101 "Confirm workspace normalization", msg))
102 return null;
103
104 currSession = repository.login();
105 Node repoNode = currSession.getNode(targetRepoPath);
106 Repository repository = RepoUtils.getRepository(repositoryFactory,
107 keyring, repoNode);
108 Credentials credentials = RepoUtils.getRepositoryCredentials(
109 keyring, repoNode);
110
111 job = new NormalizeJob(repository.login(credentials, wkspName));
112 job.setUser(true);
113 job.schedule();
114 } catch (RepositoryException e) {
115 throw new SlcException("Cannot normalize " + wkspName, e);
116 } finally {
117 JcrUtils.logoutQuietly(currSession);
118 }
119 return null;
120 }
121
122 private class NormalizeJob extends Job {
123 private Session session;
124
125 public NormalizeJob(Session session) {
126 super("Normalize Distribution");
127 this.session = session;
128 }
129
130 @Override
131 protected IStatus run(IProgressMonitor progressMonitor) {
132 try {
133 ArgeoMonitor monitor = new EclipseArgeoMonitor(progressMonitor);
134 // Normalize artifacts
135 Query countQuery = session
136 .getWorkspace()
137 .getQueryManager()
138 .createQuery("select file from [nt:file] as file",
139 Query.JCR_SQL2);
140 QueryResult result = countQuery.execute();
141 Long expectedCount = result.getNodes().getSize();
142 monitor.beginTask("Normalize artifacts of "
143 + session.getWorkspace().getName(),
144 expectedCount.intValue());
145 NormalizingTraverser tiv = new NormalizingTraverser(monitor);
146 Node artifactBaseNode = session.getNode(artifactBasePath);
147 artifactBaseNode.accept(tiv);
148 } catch (Exception e) {
149 log.error("Error normalizing workspace "
150 + session.getWorkspace().getName() + ": "
151 + e.getMessage());
152 if (log.isErrorEnabled())
153 e.printStackTrace();
154 return new Status(IStatus.ERROR, DistPlugin.PLUGIN_ID,
155 "Cannot normalize distribution "
156 + session.getWorkspace().getName(), e);
157 } finally {
158 JcrUtils.logoutQuietly(session);
159 }
160 return Status.OK_STATUS;
161 }
162 }
163
164 private class NormalizingTraverser extends TraversingItemVisitor {
165 ArgeoMonitor monitor;
166
167 public NormalizingTraverser(ArgeoMonitor monitor) {
168 super();
169 this.monitor = monitor;
170 }
171
172 @Override
173 protected void entering(Property property, int level)
174 throws RepositoryException {
175 }
176
177 @Override
178 protected void entering(Node node, int level)
179 throws RepositoryException {
180 if (node.isNodeType(NodeType.NT_FILE)) {
181 if (node.getName().endsWith("-sources.jar")) {
182 monitor.subTask(node.getName());
183 pdeSourceIndexer.index(node);
184 node.getSession().save();
185 monitor.worked(1);
186 if (log.isDebugEnabled())
187 log.debug("Processed source artifact " + node.getPath());
188 } else if (node.getName().endsWith(".jar")) {
189 if (jarFileIndexer.support(node.getPath()))
190 if (artifactIndexer.support(node.getPath())) {
191 monitor.subTask(node.getName());
192 modularDistributionIndexer.index(node);
193 jarFileIndexer.index(node);
194 artifactIndexer.index(node);
195 if (node.getSession().hasPendingChanges()) {
196 node.getSession().save();
197 if (log.isDebugEnabled())
198 log.debug("Processed jar artifact "
199 + node.getPath());
200 }
201 monitor.worked(1);
202 }
203 } else if (node.getName().endsWith(".pom")) {
204 // Removed: we do not support binaries concept anymore.
205 // if (distBundleIndexer.support(node.getPath()))
206 // distBundleIndexer.index(node);
207 if (artifactIndexer.support(node.getPath()))
208 artifactIndexer.index(node);
209 if (node.getSession().hasPendingChanges()) {
210 node.getSession().save();
211 if (log.isDebugEnabled())
212 log.debug("Processed pom artifact "
213 + node.getPath());
214 }
215 monitor.worked(1);
216 } else {
217 monitor.worked(1);
218 }
219 }
220 }
221
222 @Override
223 protected void leaving(Property property, int level)
224 throws RepositoryException {
225 }
226
227 @Override
228 protected void leaving(Node node, int level) throws RepositoryException {
229 }
230 }
231
232 /* DEPENDENCY INJECTION */
233 public void setNodeRepository(Repository nodeRepository) {
234 this.repository = nodeRepository;
235 }
236
237 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
238 this.repositoryFactory = repositoryFactory;
239 }
240
241 public void setKeyring(Keyring keyring) {
242 this.keyring = keyring;
243 }
244 }