1 package org
.argeo
.slc
.client
.ui
.dist
.commands
;
3 import javax
.jcr
.Credentials
;
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
;
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
;
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
45 public class NormalizeWorkspace
extends AbstractHandler
implements SlcNames
{
46 private final static Log log
= LogFactory
.getLog(NormalizeWorkspace
.class);
48 public final static String ID
= DistPlugin
.PLUGIN_ID
+ ".normalizeWorkspace";
49 public final static ImageDescriptor DEFAULT_ICON
= DistPlugin
.getImageDescriptor("icons/normalize.gif");
51 public final static String PARAM_WORKSPACE_NAME
= "workspaceName";
52 public final static String PARAM_TARGET_REPO_PATH
= "targetRepoPath";
54 private String artifactBasePath
= RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
;
56 // DEPENDENCY INJECTION
57 private RepositoryFactory repositoryFactory
;
58 private Keyring keyring
;
59 private Repository repository
;
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();
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
);
72 Session currSession
= null;
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 ?";
81 if (!MessageDialog
.openConfirm(DistPlugin
.getDefault().getWorkbench().getDisplay().getActiveShell(),
82 "Confirm workspace normalization", msg
))
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
);
90 job
= new NormalizeJob(repository
.login(credentials
, wkspName
));
93 } catch (RepositoryException e
) {
94 throw new SlcException("Cannot normalize " + wkspName
, e
);
96 JcrUtils
.logoutQuietly(currSession
);
101 private class NormalizeJob
extends Job
{
102 private Session session
;
104 public NormalizeJob(Session session
) {
105 super("Normalize Distribution");
106 this.session
= session
;
110 protected IStatus
run(IProgressMonitor progressMonitor
) {
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())
127 return new Status(IStatus
.ERROR
, DistPlugin
.PLUGIN_ID
,
128 "Cannot normalize distribution " + session
.getWorkspace().getName(), e
);
130 JcrUtils
.logoutQuietly(session
);
132 return Status
.OK_STATUS
;
136 private class NormalizingTraverser
extends TraversingItemVisitor
{
139 public NormalizingTraverser(JcrMonitor monitor
) {
141 this.monitor
= monitor
;
145 protected void entering(Property property
, int level
) throws RepositoryException
{
149 protected void entering(Node node
, int level
) throws RepositoryException
{
150 if (node
.getPath().startsWith(RepoConstants
.DIST_DOWNLOAD_BASEPATH
))
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();
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());
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());
197 protected void leaving(Property property
, int level
) throws RepositoryException
{
201 protected void leaving(Node node
, int level
) throws RepositoryException
{
205 /* DEPENDENCY INJECTION */
206 public void setNodeRepository(Repository nodeRepository
) {
207 this.repository
= nodeRepository
;
210 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
211 this.repositoryFactory
= repositoryFactory
;
214 public void setKeyring(Keyring keyring
) {
215 this.keyring
= keyring
;