Fix normalization
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 12 Dec 2012 12:00:04 +0000 (12:00 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 12 Dec 2012 12:00:04 +0000 (12:00 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@5968 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/NormalizeDistribution.java
runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/NormalizeGroup.java

index 967ea770b5537190d968256e026f36611857497f..159f5faa26f7629d09064655b03f7379d4b30da3 100644 (file)
@@ -31,7 +31,6 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.ArgeoMonitor;
 import org.argeo.eclipse.ui.EclipseArgeoMonitor;
-import org.argeo.eclipse.ui.dialogs.SingleValue;
 import org.argeo.jcr.JcrUtils;
 import org.argeo.slc.NameVersion;
 import org.argeo.slc.SlcException;
@@ -51,6 +50,20 @@ import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.jface.dialogs.Dialog;
+import org.eclipse.jface.dialogs.IMessageProvider;
+import org.eclipse.jface.dialogs.TitleAreaDialog;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.handlers.HandlerUtil;
 import org.sonatype.aether.artifact.Artifact;
 import org.sonatype.aether.util.artifact.DefaultArtifact;
 
@@ -72,14 +85,18 @@ public class NormalizeDistribution extends AbstractHandler implements SlcNames {
 
        public Object execute(ExecutionEvent event) throws ExecutionException {
                String workspace = event.getParameter(PARAM_WORKSPACE);
-               String version = SingleValue.ask("Version",
-                               "Enter Distribution Version");
-               if (version == null)
+               NormalizationDialog dialog = new NormalizationDialog(
+                               HandlerUtil.getActiveShell(event));
+               if (dialog.open() != Dialog.OK)
                        return null;
 
+               String version = dialog.getVersion();
+               Boolean overridePoms = dialog.getOverridePoms();
+
                NormalizeJob job;
                try {
-                       job = new NormalizeJob(repository.login(workspace), version);
+                       job = new NormalizeJob(repository.login(workspace), version,
+                                       overridePoms);
                } catch (RepositoryException e) {
                        throw new SlcException("Cannot normalize " + workspace, e);
                }
@@ -153,11 +170,14 @@ public class NormalizeDistribution extends AbstractHandler implements SlcNames {
        private class NormalizeJob extends Job {
                private Session session;
                private String version;
+               private Boolean overridePoms;
 
-               public NormalizeJob(Session session, String version) {
+               public NormalizeJob(Session session, String version,
+                               Boolean overridePoms) {
                        super("Normalize Distribution");
                        this.session = session;
                        this.version = version;
+                       this.overridePoms = overridePoms;
                }
 
                @Override
@@ -191,10 +211,8 @@ public class NormalizeDistribution extends AbstractHandler implements SlcNames {
                                                + session.getWorkspace().getName(),
                                                (int) groups.getSize());
                                while (groups.hasNext()) {
-                                       NormalizeGroup normalizeGroup = new NormalizeGroup();
-                                       normalizeGroup.setArtifactBasePath(artifactBasePath);
-                                       normalizeGroup.processGroupNode(groups.nextNode(), version,
-                                                       monitor);
+                                       NormalizeGroup.processGroupNode(groups.nextNode(), version,
+                                                       overridePoms, monitor);
                                }
                        } catch (Exception e) {
                                return new Status(IStatus.ERROR, DistPlugin.ID,
@@ -260,4 +278,74 @@ public class NormalizeDistribution extends AbstractHandler implements SlcNames {
                }
 
        }
+
+       public class NormalizationDialog extends TitleAreaDialog {
+               private Text versionT;
+               private String version;
+               private Button overridePomsC;
+               private Boolean overridePoms;
+
+               public NormalizationDialog(Shell parentShell) {
+                       super(parentShell);
+               }
+
+               protected Point getInitialSize() {
+                       return new Point(300, 250);
+               }
+
+               protected Control createDialogArea(Composite parent) {
+                       Composite dialogarea = (Composite) super.createDialogArea(parent);
+                       dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
+                                       true));
+                       Composite composite = new Composite(dialogarea, SWT.NONE);
+                       composite.setLayout(new GridLayout(2, false));
+                       composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
+                                       false));
+                       versionT = createLT(composite, "Version");
+                       overridePomsC = createLC(composite, "Override POMs");
+                       setMessage("Configure normalization", IMessageProvider.NONE);
+
+                       parent.pack();
+                       return composite;
+               }
+
+               @Override
+               protected void okPressed() {
+                       version = versionT.getText();
+                       overridePoms = overridePomsC.getSelection();
+                       super.okPressed();
+               }
+
+               /** Creates label and text. */
+               protected Text createLT(Composite parent, String label) {
+                       new Label(parent, SWT.NONE).setText(label);
+                       Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
+                                       | SWT.NONE);
+                       text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+                       return text;
+               }
+
+               /** Creates label and check. */
+               protected Button createLC(Composite parent, String label) {
+                       new Label(parent, SWT.NONE).setText(label);
+                       Button check = new Button(parent, SWT.CHECK);
+                       check.setSelection(false);
+                       check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+                       return check;
+               }
+
+               protected void configureShell(Shell shell) {
+                       super.configureShell(shell);
+                       shell.setText("Configure Normalization");
+               }
+
+               public String getVersion() {
+                       return version;
+               }
+
+               public Boolean getOverridePoms() {
+                       return overridePoms;
+               }
+
+       }
 }
index 0d734335bce506fc0aff79ff2e295efd41287a6b..bc228285a0b88ad633a93c8a6993cf20075895a1 100644 (file)
@@ -38,10 +38,10 @@ import org.argeo.slc.aether.ArtifactIdComparator;
 import org.argeo.slc.jcr.SlcNames;
 import org.argeo.slc.jcr.SlcTypes;
 import org.argeo.slc.repo.ArtifactIndexer;
-import org.argeo.slc.repo.JarFileIndexer;
 import org.argeo.slc.repo.RepoUtils;
 import org.argeo.slc.repo.maven.MavenConventionsUtils;
 import org.osgi.framework.Constants;
+import org.osgi.framework.Version;
 import org.sonatype.aether.artifact.Artifact;
 import org.sonatype.aether.util.artifact.DefaultArtifact;
 
@@ -59,6 +59,7 @@ public class NormalizeGroup implements Runnable, SlcNames {
        private Repository repository;
        private String workspace;
        private String groupId;
+       private Boolean overridePoms = false;
        private String artifactBasePath = "/";
        private String version = null;
        private String parentPomCoordinates;
@@ -66,8 +67,9 @@ public class NormalizeGroup implements Runnable, SlcNames {
        private List<String> excludedSuffixes = new ArrayList<String>();
 
        private ArtifactIndexer artifactIndexer = new ArtifactIndexer();
-       private JarFileIndexer jarFileIndexer = new JarFileIndexer();
+       // private JarFileIndexer jarFileIndexer = new JarFileIndexer();
 
+       /** TODO make it more generic */
        private List<String> systemPackages = OsgiProfile.PROFILE_JAVA_SE_1_6
                        .getSystemPackages();
 
@@ -95,51 +97,81 @@ public class NormalizeGroup implements Runnable, SlcNames {
                }
        }
 
-       public synchronized void processGroupNode(Node groupNode, String version,
-                       ArgeoMonitor monitor) throws RepositoryException {
-               // FIXME better encapsulate
-               groupId = groupNode.getProperty(SlcNames.SLC_GROUP_BASE_ID).getString();
-               this.version = version;
-               processGroupNode(groupNode, monitor);
+       public static void processGroupNode(Node groupNode, String version,
+                       Boolean overridePoms, ArgeoMonitor monitor)
+                       throws RepositoryException {
+               // TODO set artifactsBase based on group node
+               NormalizeGroup ng = new NormalizeGroup();
+               String groupId = groupNode.getProperty(SlcNames.SLC_GROUP_BASE_ID)
+                               .getString();
+               ng.setGroupId(groupId);
+               ng.setVersion(version);
+               ng.setOverridePoms(overridePoms);
+               ng.processGroupNode(groupNode, monitor);
        }
 
        protected void processGroupNode(Node groupNode, ArgeoMonitor monitor)
                        throws RepositoryException {
                if (monitor != null)
                        monitor.subTask("Group " + groupId);
+               Node allArtifactsHighestVersion = null;
                Session session = groupNode.getSession();
-               for (NodeIterator artifactBases = groupNode.getNodes(); artifactBases
+               aBases: for (NodeIterator aBases = groupNode.getNodes(); aBases
                                .hasNext();) {
-                       Node artifactBase = artifactBases.nextNode();
-                       if (artifactBase.isNodeType(SlcTypes.SLC_ARTIFACT_BASE)) {
-                               for (NodeIterator artifactVersions = artifactBase.getNodes(); artifactVersions
+                       Node aBase = aBases.nextNode();
+                       if (aBase.isNodeType(SlcTypes.SLC_ARTIFACT_BASE)) {
+                               Node highestAVersion = null;
+                               for (NodeIterator aVersions = aBase.getNodes(); aVersions
                                                .hasNext();) {
-                                       Node artifactVersion = artifactVersions.nextNode();
-                                       if (artifactVersion
-                                                       .isNodeType(SlcTypes.SLC_ARTIFACT_VERSION_BASE))
-                                               for (NodeIterator files = artifactVersion.getNodes(); files
-                                                               .hasNext();) {
-                                                       Node file = files.nextNode();
-                                                       if (file.isNodeType(SlcTypes.SLC_BUNDLE_ARTIFACT)) {
-                                                               preProcessBundleArtifact(file);
-                                                               file.getSession().save();
-                                                               if (log.isDebugEnabled())
-                                                                       log.debug("Pre-processed " + file.getName());
+                                       Node aVersion = aVersions.nextNode();
+                                       if (aVersion.isNodeType(SlcTypes.SLC_ARTIFACT_VERSION_BASE)) {
+                                               if (highestAVersion == null) {
+                                                       highestAVersion = aVersion;
+                                                       if (allArtifactsHighestVersion == null)
+                                                               allArtifactsHighestVersion = aVersion;
+
+                                               } else {
+                                                       Version currVersion = extractOsgiVersion(aVersion);
+                                                       Version currentHighestVersion = extractOsgiVersion(highestAVersion);
+                                                       if (currVersion.compareTo(currentHighestVersion) > 0) {
+                                                               highestAVersion = aVersion;
+                                                       }
+                                                       if (currVersion
+                                                                       .compareTo(extractOsgiVersion(allArtifactsHighestVersion)) > 0) {
+                                                               allArtifactsHighestVersion = aVersion;
                                                        }
-
                                                }
+
+                                       }
+
+                               }
+                               if (highestAVersion == null)
+                                       continue aBases;
+                               for (NodeIterator files = highestAVersion.getNodes(); files
+                                               .hasNext();) {
+                                       Node file = files.nextNode();
+                                       if (file.isNodeType(SlcTypes.SLC_BUNDLE_ARTIFACT)) {
+                                               preProcessBundleArtifact(file);
+                                               file.getSession().save();
+                                               if (log.isDebugEnabled())
+                                                       log.debug("Pre-processed " + file.getName());
+                                       }
+
                                }
                        }
                }
-               // NodeIterator bundlesIt = listBundleArtifacts(session);
-               //
-               // while (bundlesIt.hasNext()) {
-               // Node bundleNode = bundlesIt.nextNode();
-               // preProcessBundleArtifact(bundleNode);
-               // bundleNode.getSession().save();
-               // if (log.isDebugEnabled())
-               // log.debug("Pre-processed " + bundleNode.getName());
-               // }
+
+               // if version not set or empty, use the highets version
+               // useful when indexing a product maven repository where
+               // all artifacts have the same version for a given release
+               // => the version can then be left empty
+               if (version == null || version.trim().equals(""))
+                       if (allArtifactsHighestVersion != null)
+                               version = allArtifactsHighestVersion.getProperty(
+                                               SLC_ARTIFACT_VERSION).getString();
+                       else
+                               throw new SlcException("Group version " + version
+                                               + " is empty.");
 
                int bundleCount = symbolicNamesToNodes.size();
                if (log.isDebugEnabled())
@@ -169,6 +201,14 @@ public class NormalizeGroup implements Runnable, SlcNames {
                        monitor.worked(1);
        }
 
+       private Version extractOsgiVersion(Node artifactVersion)
+                       throws RepositoryException {
+               String rawVersion = artifactVersion.getProperty(SLC_ARTIFACT_VERSION)
+                               .getString();
+               String cleanVersion = rawVersion.replace("-SNAPSHOT", ".SNAPSHOT");
+               return new Version(cleanVersion);
+       }
+
        private Artifact writeIndex(Session session, String artifactId,
                        Set<Artifact> artifacts) throws RepositoryException {
                Artifact artifact = new DefaultArtifact(groupId, artifactId, "pom",
@@ -181,23 +221,27 @@ public class NormalizeGroup implements Runnable, SlcNames {
                                session.getNode(artifactBasePath), artifact, pom.getBytes());
                artifactIndexer.index(node);
 
-               // FIXME factorize
+               // TODO factorize
                String pomSha = JcrUtils.checksumFile(node, "SHA-1");
                JcrUtils.copyBytesAsFile(node.getParent(), node.getName() + ".sha1",
                                pomSha.getBytes());
+               String pomMd5 = JcrUtils.checksumFile(node, "MD5");
+               JcrUtils.copyBytesAsFile(node.getParent(), node.getName() + ".md5",
+                               pomMd5.getBytes());
                session.save();
                return artifact;
        }
 
        protected void preProcessBundleArtifact(Node bundleNode)
                        throws RepositoryException {
-               artifactIndexer.index(bundleNode);
-               jarFileIndexer.index(bundleNode);
+               // we assume nodes are already indexed
+               // artifactIndexer.index(bundleNode);
+               // jarFileIndexer.index(bundleNode);
 
                String symbolicName = JcrUtils.get(bundleNode, SLC_SYMBOLIC_NAME);
 
                if (symbolicName.endsWith(".source")) {
-                       // TODO make a shared node with classifier 'sources'
+                       // TODO make a shared node with classifier 'sources'?
                        String bundleName = RepoUtils
                                        .extractBundleNameFromSourceName(symbolicName);
                        for (String excludedSuffix : excludedSuffixes) {
@@ -233,11 +277,13 @@ public class NormalizeGroup implements Runnable, SlcNames {
                String baseName = FilenameUtils.getBaseName(bundleNode.getName());
 
                // pom
-               String pom = generatePomForBundle(bundleNode);
                String pomName = baseName + ".pom";
+               if (artifactFolder.hasNode(pomName) && !overridePoms)
+                       return;// skip
+
+               String pom = generatePomForBundle(bundleNode);
                Node pomNode = JcrUtils.copyBytesAsFile(artifactFolder, pomName,
                                pom.getBytes());
-
                // checksum
                String bundleSha = JcrUtils.checksumFile(bundleNode, "SHA-1");
                JcrUtils.copyBytesAsFile(artifactFolder,
@@ -400,4 +446,11 @@ public class NormalizeGroup implements Runnable, SlcNames {
                this.excludedSuffixes = excludedSuffixes;
        }
 
+       public void setOverridePoms(Boolean overridePoms) {
+               this.overridePoms = overridePoms;
+       }
+
+       public void setArtifactIndexer(ArtifactIndexer artifactIndexer) {
+               this.artifactIndexer = artifactIndexer;
+       }
 }