]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/NormalizeDistribution.java
e10e75f506207b79a832b129ce9d8e62f07dee8e
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / commands / NormalizeDistribution.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.slc.client.ui.dist.commands;
17
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;
25
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;
40
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";
47
48 private final static Log log = LogFactory
49 .getLog(NormalizeDistribution.class);
50
51 private Repository repository;
52 private String artifactBasePath = "/";
53
54 private ArtifactIndexer artifactIndexer = new ArtifactIndexer();
55 private JarFileIndexer jarFileIndexer = new JarFileIndexer();
56
57 public Object execute(ExecutionEvent event) throws ExecutionException {
58 String workspace = event.getParameter(PARAM_WORKSPACE);
59 NormalizeJob job = new NormalizeJob(workspace);
60 job.setUser(true);
61 job.schedule();
62 return null;
63 }
64
65 public void setRepository(Repository repository) {
66 this.repository = repository;
67 }
68
69 private class NormalizeJob extends Job {
70 private String workspace;
71
72 public NormalizeJob(String workspace) {
73 super("Normalize Distribution");
74 this.workspace = workspace;
75 }
76
77 @Override
78 protected IStatus run(IProgressMonitor monitor) {
79 Session session = null;
80 try {
81 session = repository.login(workspace);
82 // QueryManager qm = session.getWorkspace().getQueryManager();
83 // Query query = qm
84 // .createQuery(
85 // "select * from [nt:file] where NAME([nt:file]) like '%.jar'",
86 // Query.JCR_SQL2);
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);
99 } finally {
100 JcrUtils.logoutQuietly(session);
101 }
102 return Status.OK_STATUS;
103 }
104
105 }
106
107 private class NormalizingTraverser extends TraversingItemVisitor {
108 IProgressMonitor monitor;
109
110 public NormalizingTraverser(IProgressMonitor monitor) {
111 super();
112 this.monitor = monitor;
113 }
114
115 @Override
116 protected void entering(Property property, int level)
117 throws RepositoryException {
118 }
119
120 @Override
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();
130 monitor.worked(1);
131 if (log.isDebugEnabled())
132 log.debug("Processed " + node);
133 }
134 }
135 }
136
137 @Override
138 protected void leaving(Property property, int level)
139 throws RepositoryException {
140 }
141
142 @Override
143 protected void leaving(Node node, int level) throws RepositoryException {
144 }
145
146 }
147 }