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