2 * Copyright (C) 2007-2012 Mathieu Baudier
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
.Node
;
19 import javax
.jcr
.Property
;
20 import javax
.jcr
.Repository
;
21 import javax
.jcr
.RepositoryException
;
22 import javax
.jcr
.Session
;
23 import javax
.jcr
.nodetype
.NodeType
;
24 import javax
.jcr
.util
.TraversingItemVisitor
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.argeo
.jcr
.JcrUtils
;
29 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
30 import org
.argeo
.slc
.jcr
.SlcNames
;
31 import org
.argeo
.slc
.repo
.ArtifactIndexer
;
32 import org
.argeo
.slc
.repo
.JarFileIndexer
;
33 import org
.eclipse
.core
.commands
.AbstractHandler
;
34 import org
.eclipse
.core
.commands
.ExecutionEvent
;
35 import org
.eclipse
.core
.commands
.ExecutionException
;
36 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
37 import org
.eclipse
.core
.runtime
.IStatus
;
38 import org
.eclipse
.core
.runtime
.Status
;
39 import org
.eclipse
.core
.runtime
.jobs
.Job
;
41 /** Make sure than Maven and OSGi metadata are consistent */
42 public class NormalizeDistribution
extends AbstractHandler
implements SlcNames
{
43 public final static String ID
= DistPlugin
.ID
+ ".normalizeDistribution";
44 public final static String PARAM_WORKSPACE
= "workspace";
45 public final static String DEFAULT_LABEL
= "Normalize";
46 public final static String DEFAULT_ICON_PATH
= "icons/addItem.gif";
48 private final static Log log
= LogFactory
49 .getLog(NormalizeDistribution
.class);
51 private Repository repository
;
52 private String artifactBasePath
= "/";
54 private ArtifactIndexer artifactIndexer
= new ArtifactIndexer();
55 private JarFileIndexer jarFileIndexer
= new JarFileIndexer();
57 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
58 String workspace
= event
.getParameter(PARAM_WORKSPACE
);
59 NormalizeJob job
= new NormalizeJob(workspace
);
65 public void setRepository(Repository repository
) {
66 this.repository
= repository
;
69 private class NormalizeJob
extends Job
{
70 private String workspace
;
72 public NormalizeJob(String workspace
) {
73 super("Normalize Distribution");
74 this.workspace
= workspace
;
78 protected IStatus
run(IProgressMonitor monitor
) {
79 Session session
= null;
81 session
= repository
.login(workspace
);
82 // QueryManager qm = session.getWorkspace().getQueryManager();
85 // "select * from [nt:file] where NAME([nt:file]) like '%.jar'",
87 // // Query query = qm.createQuery("//*jar", Query.XPATH);
88 // long count = query.execute().getRows().getSize();
89 // if (log.isDebugEnabled())
90 // log.debug("Count: " + count);
91 // long count = query.execute().getRows().nextRow()
92 // .getValue("count").getLong();
93 monitor
.beginTask("Normalize " + workspace
, -1);
94 NormalizingTraverser tiv
= new NormalizingTraverser(monitor
);
95 session
.getNode(artifactBasePath
).accept(tiv
);
96 } catch (Exception e
) {
97 return new Status(IStatus
.ERROR
, DistPlugin
.ID
,
98 "Cannot normalize distribution " + workspace
, e
);
100 JcrUtils
.logoutQuietly(session
);
102 return Status
.OK_STATUS
;
107 private class NormalizingTraverser
extends TraversingItemVisitor
{
108 IProgressMonitor monitor
;
110 public NormalizingTraverser(IProgressMonitor monitor
) {
112 this.monitor
= monitor
;
116 protected void entering(Property property
, int level
)
117 throws RepositoryException
{
121 protected void entering(Node node
, int level
)
122 throws RepositoryException
{
123 if (node
.isNodeType(NodeType
.NT_FILE
)) {
124 if (jarFileIndexer
.support(node
.getPath()))
125 if (artifactIndexer
.support(node
.getPath())) {
126 monitor
.subTask(node
.getName());
127 artifactIndexer
.index(node
);
128 jarFileIndexer
.index(node
);
129 node
.getSession().save();
131 if (log
.isDebugEnabled())
132 log
.debug("Processed " + node
);
138 protected void leaving(Property property
, int level
)
139 throws RepositoryException
{
143 protected void leaving(Node node
, int level
) throws RepositoryException
{