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