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