]> 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
+ Add repo informations
[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 Argeo GmbH
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.Binary;
19 import javax.jcr.Node;
20 import javax.jcr.NodeIterator;
21 import javax.jcr.Property;
22 import javax.jcr.Repository;
23 import javax.jcr.RepositoryException;
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;
29
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.NameVersion;
36 import org.argeo.slc.SlcException;
37 import org.argeo.slc.aether.AetherUtils;
38 import org.argeo.slc.client.ui.dist.DistPlugin;
39 import org.argeo.slc.jcr.SlcNames;
40 import org.argeo.slc.jcr.SlcTypes;
41 import org.argeo.slc.repo.ArtifactIndexer;
42 import org.argeo.slc.repo.JarFileIndexer;
43 import org.argeo.slc.repo.RepoUtils;
44 import org.argeo.slc.repo.maven.MavenConventionsUtils;
45 import org.argeo.slc.repo.osgi.NormalizeGroup;
46 import org.eclipse.core.commands.AbstractHandler;
47 import org.eclipse.core.commands.ExecutionEvent;
48 import org.eclipse.core.commands.ExecutionException;
49 import org.eclipse.core.runtime.IProgressMonitor;
50 import org.eclipse.core.runtime.IStatus;
51 import org.eclipse.core.runtime.Status;
52 import org.eclipse.core.runtime.jobs.Job;
53 import org.eclipse.jface.dialogs.Dialog;
54 import org.eclipse.jface.dialogs.IMessageProvider;
55 import org.eclipse.jface.dialogs.TitleAreaDialog;
56 import org.eclipse.swt.SWT;
57 import org.eclipse.swt.graphics.Point;
58 import org.eclipse.swt.layout.GridData;
59 import org.eclipse.swt.layout.GridLayout;
60 import org.eclipse.swt.widgets.Button;
61 import org.eclipse.swt.widgets.Composite;
62 import org.eclipse.swt.widgets.Control;
63 import org.eclipse.swt.widgets.Label;
64 import org.eclipse.swt.widgets.Shell;
65 import org.eclipse.swt.widgets.Text;
66 import org.eclipse.ui.handlers.HandlerUtil;
67 import org.sonatype.aether.artifact.Artifact;
68 import org.sonatype.aether.util.artifact.DefaultArtifact;
69
70 /** Make sure than Maven and OSGi metadata are consistent */
71 public class NormalizeDistribution extends AbstractHandler implements SlcNames {
72 public final static String ID = DistPlugin.ID + ".normalizeDistribution";
73 public final static String PARAM_WORKSPACE = "workspace";
74 public final static String DEFAULT_LABEL = "Normalize...";
75 public final static String DEFAULT_ICON_PATH = "icons/normalize.gif";
76
77 private final static Log log = LogFactory
78 .getLog(NormalizeDistribution.class);
79
80 private Repository repository;
81 private String artifactBasePath = "/";
82
83 private ArtifactIndexer artifactIndexer = new ArtifactIndexer();
84 private JarFileIndexer jarFileIndexer = new JarFileIndexer();
85
86 public Object execute(ExecutionEvent event) throws ExecutionException {
87 String workspace = event.getParameter(PARAM_WORKSPACE);
88 NormalizationDialog dialog = new NormalizationDialog(
89 HandlerUtil.getActiveShell(event));
90 if (dialog.open() != Dialog.OK)
91 return null;
92
93 String version = dialog.getVersion();
94 Boolean overridePoms = dialog.getOverridePoms();
95
96 NormalizeJob job;
97 try {
98 job = new NormalizeJob(repository.login(workspace), version,
99 overridePoms);
100 } catch (RepositoryException e) {
101 throw new SlcException("Cannot normalize " + workspace, e);
102 }
103 job.setUser(true);
104 job.schedule();
105 return null;
106 }
107
108 protected void packageSourcesAsPdeSource(Node sourcesNode) {
109 Binary origBinary = null;
110 Binary osgiBinary = null;
111 try {
112 Session session = sourcesNode.getSession();
113 Artifact sourcesArtifact = AetherUtils.convertPathToArtifact(
114 sourcesNode.getPath(), null);
115
116 // read name version from manifest
117 Artifact osgiArtifact = new DefaultArtifact(
118 sourcesArtifact.getGroupId(),
119 sourcesArtifact.getArtifactId(),
120 sourcesArtifact.getExtension(),
121 sourcesArtifact.getVersion());
122 String osgiPath = MavenConventionsUtils.artifactPath(
123 artifactBasePath, osgiArtifact);
124 osgiBinary = session.getNode(osgiPath).getNode(Node.JCR_CONTENT)
125 .getProperty(Property.JCR_DATA).getBinary();
126
127 NameVersion nameVersion = RepoUtils.readNameVersion(osgiBinary
128 .getStream());
129
130 // create PDe sources artifact
131 Artifact pdeSourceArtifact = new DefaultArtifact(
132 sourcesArtifact.getGroupId(),
133 sourcesArtifact.getArtifactId() + ".source",
134 sourcesArtifact.getExtension(),
135 sourcesArtifact.getVersion());
136 String targetSourceParentPath = MavenConventionsUtils
137 .artifactParentPath(artifactBasePath, pdeSourceArtifact);
138 String targetSourceFileName = MavenConventionsUtils
139 .artifactFileName(pdeSourceArtifact);
140 String targetSourceJarPath = targetSourceParentPath + '/'
141 + targetSourceFileName;
142
143 Node targetSourceParentNode = JcrUtils.mkfolders(session,
144 targetSourceParentPath);
145 origBinary = sourcesNode.getNode(Node.JCR_CONTENT)
146 .getProperty(Property.JCR_DATA).getBinary();
147 byte[] targetJarBytes = RepoUtils.packageAsPdeSource(
148 origBinary.getStream(), nameVersion);
149 JcrUtils.copyBytesAsFile(targetSourceParentNode,
150 targetSourceFileName, targetJarBytes);
151
152 // reindex
153 Node targetSourceJarNode = session.getNode(targetSourceJarPath);
154 artifactIndexer.index(targetSourceJarNode);
155 jarFileIndexer.index(targetSourceJarNode);
156 } catch (RepositoryException e) {
157 throw new SlcException("Cannot add PDE sources for " + sourcesNode,
158 e);
159 } finally {
160 JcrUtils.closeQuietly(origBinary);
161 JcrUtils.closeQuietly(osgiBinary);
162 }
163
164 }
165
166 public void setRepository(Repository repository) {
167 this.repository = repository;
168 }
169
170 private class NormalizeJob extends Job {
171 private Session session;
172 private String version;
173 private Boolean overridePoms;
174
175 public NormalizeJob(Session session, String version,
176 Boolean overridePoms) {
177 super("Normalize Distribution");
178 this.session = session;
179 this.version = version;
180 this.overridePoms = overridePoms;
181 }
182
183 @Override
184 protected IStatus run(IProgressMonitor progressMonitor) {
185
186 try {
187 ArgeoMonitor monitor = new EclipseArgeoMonitor(progressMonitor);
188 // normalize artifacts
189 Query countQuery = session
190 .getWorkspace()
191 .getQueryManager()
192 .createQuery("select file from [nt:file] as file",
193 Query.JCR_SQL2);
194 QueryResult result = countQuery.execute();
195 Long expectedCount = result.getNodes().getSize();
196 monitor.beginTask("Normalize artifacts of "
197 + session.getWorkspace().getName(),
198 expectedCount.intValue());
199 NormalizingTraverser tiv = new NormalizingTraverser(monitor);
200 session.getNode(artifactBasePath).accept(tiv);
201
202 // normalize groups
203 Query groupQuery = session
204 .getWorkspace()
205 .getQueryManager()
206 .createQuery(
207 "select group from [" + SlcTypes.SLC_GROUP_BASE
208 + "] as group", Query.JCR_SQL2);
209 NodeIterator groups = groupQuery.execute().getNodes();
210 monitor.beginTask("Normalize groups of "
211 + session.getWorkspace().getName(),
212 (int) groups.getSize());
213 while (groups.hasNext()) {
214 NormalizeGroup.processGroupNode(groups.nextNode(), version,
215 overridePoms, monitor);
216 }
217 } catch (Exception e) {
218 return new Status(IStatus.ERROR, DistPlugin.ID,
219 "Cannot normalize distribution "
220 + session.getWorkspace().getName(), e);
221 } finally {
222 JcrUtils.logoutQuietly(session);
223 }
224 return Status.OK_STATUS;
225 }
226
227 }
228
229 private class NormalizingTraverser extends TraversingItemVisitor {
230 ArgeoMonitor monitor;
231
232 public NormalizingTraverser(ArgeoMonitor monitor) {
233 super();
234 this.monitor = monitor;
235 }
236
237 @Override
238 protected void entering(Property property, int level)
239 throws RepositoryException {
240 }
241
242 @Override
243 protected void entering(Node node, int level)
244 throws RepositoryException {
245 if (node.isNodeType(NodeType.NT_FILE)) {
246 if (node.getName().endsWith("-sources.jar")) {
247 monitor.subTask(node.getName());
248 packageSourcesAsPdeSource(node);
249 node.getSession().save();
250 monitor.worked(1);
251 if (log.isDebugEnabled())
252 log.debug("Processed source artifact " + node.getPath());
253 } else if (node.getName().endsWith(".jar")) {
254 if (jarFileIndexer.support(node.getPath()))
255 if (artifactIndexer.support(node.getPath())) {
256 monitor.subTask(node.getName());
257 artifactIndexer.index(node);
258 jarFileIndexer.index(node);
259 node.getSession().save();
260 monitor.worked(1);
261 if (log.isDebugEnabled())
262 log.debug("Processed artifact "
263 + node.getPath());
264 }
265 } else {
266 monitor.worked(1);
267 }
268 }
269 }
270
271 @Override
272 protected void leaving(Property property, int level)
273 throws RepositoryException {
274 }
275
276 @Override
277 protected void leaving(Node node, int level) throws RepositoryException {
278 }
279
280 }
281
282 public class NormalizationDialog extends TitleAreaDialog {
283 private Text versionT;
284 private String version;
285 private Button overridePomsC;
286 private Boolean overridePoms;
287
288 public NormalizationDialog(Shell parentShell) {
289 super(parentShell);
290 }
291
292 protected Point getInitialSize() {
293 return new Point(300, 250);
294 }
295
296 protected Control createDialogArea(Composite parent) {
297 Composite dialogarea = (Composite) super.createDialogArea(parent);
298 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
299 true));
300 Composite composite = new Composite(dialogarea, SWT.NONE);
301 composite.setLayout(new GridLayout(2, false));
302 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
303 false));
304 versionT = createLT(composite, "Version");
305 overridePomsC = createLC(composite, "Override POMs");
306 setMessage("Configure normalization", IMessageProvider.NONE);
307
308 parent.pack();
309 return composite;
310 }
311
312 @Override
313 protected void okPressed() {
314 version = versionT.getText();
315 overridePoms = overridePomsC.getSelection();
316 super.okPressed();
317 }
318
319 /** Creates label and text. */
320 protected Text createLT(Composite parent, String label) {
321 new Label(parent, SWT.NONE).setText(label);
322 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
323 | SWT.NONE);
324 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
325 return text;
326 }
327
328 /** Creates label and check. */
329 protected Button createLC(Composite parent, String label) {
330 new Label(parent, SWT.NONE).setText(label);
331 Button check = new Button(parent, SWT.CHECK);
332 check.setSelection(false);
333 check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
334 return check;
335 }
336
337 protected void configureShell(Shell shell) {
338 super.configureShell(shell);
339 shell.setText("Normalize...");
340 }
341
342 public String getVersion() {
343 return version;
344 }
345
346 public Boolean getOverridePoms() {
347 return overridePoms;
348 }
349
350 }
351 }