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
.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
.ModularDistributionIndexer
;
40 import org
.argeo
.slc
.repo
.JarFileIndexer
;
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
;
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
.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 ArtifactIndexer artifactIndexer
= new ArtifactIndexer();
79 private JarFileIndexer jarFileIndexer
= new JarFileIndexer();
80 private ModularDistributionIndexer distBundleIndexer
= new ModularDistributionIndexer();
81 private PdeSourcesIndexer pdeSourceIndexer
= new PdeSourcesIndexer();
83 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
84 String targetRepoPath
= event
.getParameter(PARAM_TARGET_REPO_PATH
);
85 String wkspName
= event
.getParameter(PARAM_WORKSPACE_NAME
);
87 Session currSession
= null;
90 String msg
= "Your are about to normalize workspace: "
92 + ".\nThis will index OSGi bundles and Maven artifacts, "
93 + "it will also convert Maven sources to PDE Sources if needed.\n"
94 + "Note that no information will be overwritten: "
95 + "all existing information are kept."
96 + "\n\n Do you really want to proceed ?";
98 if (!MessageDialog
.openConfirm(DistPlugin
.getDefault()
99 .getWorkbench().getDisplay().getActiveShell(),
100 "Confirm workspace normalization", msg
))
103 currSession
= repository
.login();
104 Node repoNode
= currSession
.getNode(targetRepoPath
);
105 Repository repository
= RepoUtils
.getRepository(repositoryFactory
,
107 Credentials credentials
= RepoUtils
.getRepositoryCredentials(
110 job
= new NormalizeJob(repository
.login(credentials
, wkspName
));
113 } catch (RepositoryException e
) {
114 throw new SlcException("Cannot normalize " + wkspName
, e
);
116 JcrUtils
.logoutQuietly(currSession
);
121 private class NormalizeJob
extends Job
{
122 private Session session
;
124 public NormalizeJob(Session session
) {
125 super("Normalize Distribution");
126 this.session
= session
;
130 protected IStatus
run(IProgressMonitor progressMonitor
) {
132 ArgeoMonitor monitor
= new EclipseArgeoMonitor(progressMonitor
);
133 // Normalize artifacts
134 Query countQuery
= session
137 .createQuery("select file from [nt:file] as file",
139 QueryResult result
= countQuery
.execute();
140 Long expectedCount
= result
.getNodes().getSize();
141 monitor
.beginTask("Normalize artifacts of "
142 + session
.getWorkspace().getName(),
143 expectedCount
.intValue());
144 NormalizingTraverser tiv
= new NormalizingTraverser(monitor
);
145 Node artifactBaseNode
= session
.getNode(artifactBasePath
);
146 artifactBaseNode
.accept(tiv
);
147 } catch (Exception e
) {
148 log
.error("Error normalizing workspace "
149 + session
.getWorkspace().getName() + ": "
151 if (log
.isErrorEnabled())
153 return new Status(IStatus
.ERROR
, DistPlugin
.ID
,
154 "Cannot normalize distribution "
155 + session
.getWorkspace().getName(), e
);
157 JcrUtils
.logoutQuietly(session
);
159 return Status
.OK_STATUS
;
163 private class NormalizingTraverser
extends TraversingItemVisitor
{
164 ArgeoMonitor monitor
;
166 public NormalizingTraverser(ArgeoMonitor monitor
) {
168 this.monitor
= monitor
;
172 protected void entering(Property property
, int level
)
173 throws RepositoryException
{
177 protected void entering(Node node
, int level
)
178 throws RepositoryException
{
179 if (node
.isNodeType(NodeType
.NT_FILE
)) {
180 if (node
.getName().endsWith("-sources.jar")) {
181 monitor
.subTask(node
.getName());
182 pdeSourceIndexer
.index(node
);
183 node
.getSession().save();
185 if (log
.isDebugEnabled())
186 log
.debug("Processed source artifact " + node
.getPath());
187 } else if (node
.getName().endsWith(".jar")) {
188 if (jarFileIndexer
.support(node
.getPath()))
189 if (artifactIndexer
.support(node
.getPath())) {
190 monitor
.subTask(node
.getName());
191 artifactIndexer
.index(node
);
192 jarFileIndexer
.index(node
);
193 distBundleIndexer
.index(node
);
194 if (node
.getSession().hasPendingChanges()) {
195 node
.getSession().save();
196 if (log
.isDebugEnabled())
197 log
.debug("Processed jar artifact "
202 } else if (node
.getName().endsWith(".pom")) {
203 if (distBundleIndexer
.support(node
.getPath()))
204 distBundleIndexer
.index(node
);
205 if (artifactIndexer
.support(node
.getPath()))
206 artifactIndexer
.index(node
);
207 if (node
.getSession().hasPendingChanges()) {
208 node
.getSession().save();
209 if (log
.isDebugEnabled())
210 log
.debug("Processed pom artifact "
221 protected void leaving(Property property
, int level
)
222 throws RepositoryException
{
226 protected void leaving(Node node
, int level
) throws RepositoryException
{
230 /* DEPENDENCY INJECTION */
231 public void setNodeRepository(Repository nodeRepository
) {
232 this.repository
= nodeRepository
;
235 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
236 this.repositoryFactory
= repositoryFactory
;
239 public void setKeyring(Keyring keyring
) {
240 this.keyring
= keyring
;