2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.client
.ui
.dist
.commands
;
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
;
30 import org
.apache
.commons
.logging
.Log
;
31 import org
.apache
.commons
.logging
.LogFactory
;
32 import org
.argeo
.eclipse
.ui
.EclipseJcrMonitor
;
33 import org
.argeo
.jcr
.JcrMonitor
;
34 import org
.argeo
.jcr
.JcrUtils
;
35 import org
.argeo
.node
.security
.Keyring
;
36 import org
.argeo
.slc
.SlcException
;
37 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
38 import org
.argeo
.slc
.jcr
.SlcNames
;
39 import org
.argeo
.slc
.repo
.ArtifactIndexer
;
40 import org
.argeo
.slc
.repo
.JarFileIndexer
;
41 import org
.argeo
.slc
.repo
.ModularDistributionIndexer
;
42 import org
.argeo
.slc
.repo
.PdeSourcesIndexer
;
43 import org
.argeo
.slc
.repo
.RepoConstants
;
44 import org
.argeo
.slc
.repo
.RepoUtils
;
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
;
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
60 public class NormalizeWorkspace
extends AbstractHandler
implements SlcNames
{
61 private final static Log log
= LogFactory
.getLog(NormalizeWorkspace
.class);
63 public final static String ID
= DistPlugin
.PLUGIN_ID
+ ".normalizeWorkspace";
64 public final static ImageDescriptor DEFAULT_ICON
= DistPlugin
65 .getImageDescriptor("icons/normalize.gif");
67 public final static String PARAM_WORKSPACE_NAME
= "workspaceName";
68 public final static String PARAM_TARGET_REPO_PATH
= "targetRepoPath";
70 private String artifactBasePath
= RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
;
72 // DEPENDENCY INJECTION
73 private RepositoryFactory repositoryFactory
;
74 private Keyring keyring
;
75 private Repository repository
;
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();
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
);
88 Session currSession
= null;
91 String msg
= "Your are about to normalize workspace: "
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 ?";
99 if (!MessageDialog
.openConfirm(DistPlugin
.getDefault()
100 .getWorkbench().getDisplay().getActiveShell(),
101 "Confirm workspace normalization", msg
))
104 currSession
= repository
.login();
105 Node repoNode
= currSession
.getNode(targetRepoPath
);
106 Repository repository
= RepoUtils
.getRepository(repositoryFactory
,
108 Credentials credentials
= RepoUtils
.getRepositoryCredentials(
111 job
= new NormalizeJob(repository
.login(credentials
, wkspName
));
114 } catch (RepositoryException e
) {
115 throw new SlcException("Cannot normalize " + wkspName
, e
);
117 JcrUtils
.logoutQuietly(currSession
);
122 private class NormalizeJob
extends Job
{
123 private Session session
;
125 public NormalizeJob(Session session
) {
126 super("Normalize Distribution");
127 this.session
= session
;
131 protected IStatus
run(IProgressMonitor progressMonitor
) {
133 JcrMonitor monitor
= new EclipseJcrMonitor(progressMonitor
);
134 // Normalize artifacts
135 Query countQuery
= session
138 .createQuery("select file from [nt:file] as file",
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() + ": "
152 if (log
.isErrorEnabled())
154 return new Status(IStatus
.ERROR
, DistPlugin
.PLUGIN_ID
,
155 "Cannot normalize distribution "
156 + session
.getWorkspace().getName(), e
);
158 JcrUtils
.logoutQuietly(session
);
160 return Status
.OK_STATUS
;
164 private class NormalizingTraverser
extends TraversingItemVisitor
{
167 public NormalizingTraverser(JcrMonitor monitor
) {
169 this.monitor
= monitor
;
173 protected void entering(Property property
, int level
)
174 throws RepositoryException
{
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();
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 "
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 "
223 protected void leaving(Property property
, int level
)
224 throws RepositoryException
{
228 protected void leaving(Node node
, int level
) throws RepositoryException
{
232 /* DEPENDENCY INJECTION */
233 public void setNodeRepository(Repository nodeRepository
) {
234 this.repository
= nodeRepository
;
237 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
238 this.repositoryFactory
= repositoryFactory
;
241 public void setKeyring(Keyring keyring
) {
242 this.keyring
= keyring
;