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
.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
;
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
59 public class NormalizeWorkspace
extends AbstractHandler
implements SlcNames
{
60 private final static Log log
= LogFactory
.getLog(NormalizeWorkspace
.class);
62 public final static String ID
= DistPlugin
.ID
+ ".normalizeWorkspace";
63 public final static ImageDescriptor DEFAULT_ICON
= DistPlugin
64 .getImageDescriptor("icons/normalize.gif");
66 public final static String PARAM_WORKSPACE_NAME
= "workspaceName";
67 public final static String PARAM_TARGET_REPO_PATH
= "targetRepoPath";
69 private String artifactBasePath
= RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
;
71 // DEPENDENCY INJECTION
72 private RepositoryFactory repositoryFactory
;
73 private Keyring keyring
;
74 private Repository repository
;
76 private ArtifactIndexer artifactIndexer
= new ArtifactIndexer();
77 private JarFileIndexer jarFileIndexer
= new JarFileIndexer();
78 private PdeSourcesIndexer pdeSourceIndexer
= new PdeSourcesIndexer(
79 artifactIndexer
, jarFileIndexer
);
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
);
85 Session currSession
= null;
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 ?";
95 if (!MessageDialog
.openConfirm(DistPlugin
.getDefault()
96 .getWorkbench().getDisplay().getActiveShell(),
97 "Confirm workspace normalization", msg
))
100 currSession
= repository
.login();
101 Node repoNode
= currSession
.getNode(targetRepoPath
);
102 Repository repository
= RepoUtils
.getRepository(repositoryFactory
,
104 Credentials credentials
= RepoUtils
.getRepositoryCredentials(
107 job
= new NormalizeJob(repository
.login(credentials
, wkspName
));
110 } catch (RepositoryException e
) {
111 throw new SlcException("Cannot normalize " + wkspName
, e
);
113 JcrUtils
.logoutQuietly(currSession
);
118 private class NormalizeJob
extends Job
{
119 private Session session
;
121 public NormalizeJob(Session session
) {
122 super("Normalize Distribution");
123 this.session
= session
;
127 protected IStatus
run(IProgressMonitor progressMonitor
) {
129 ArgeoMonitor monitor
= new EclipseArgeoMonitor(progressMonitor
);
130 // Normalize artifacts
131 Query countQuery
= session
134 .createQuery("select file from [nt:file] as file",
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
);
148 JcrUtils
.logoutQuietly(session
);
150 return Status
.OK_STATUS
;
154 private class NormalizingTraverser
extends TraversingItemVisitor
{
155 ArgeoMonitor monitor
;
157 public NormalizingTraverser(ArgeoMonitor monitor
) {
159 this.monitor
= monitor
;
163 protected void entering(Property property
, int level
)
164 throws RepositoryException
{
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();
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();
186 if (log
.isDebugEnabled())
187 log
.debug("Processed artifact "
197 protected void leaving(Property property
, int level
)
198 throws RepositoryException
{
202 protected void leaving(Node node
, int level
) throws RepositoryException
{
206 /* DEPENDENCY INJECTION */
207 public void setNodeRepository(Repository nodeRepository
) {
208 this.repository
= nodeRepository
;
211 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
212 this.repositoryFactory
= repositoryFactory
;
215 public void setKeyring(Keyring keyring
) {
216 this.keyring
= keyring
;