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