]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/NormalizeGroup.java
Work on the modular distribution - generate binaries, work on layout.
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / osgi / NormalizeGroup.java
index 144c15987f6e6f9c3a5cb6f3927757f6a690f3f6..34e1aee249e0d24fee5d3c76190bb59b64efc536 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2012 Mathieu Baudier
+ * Copyright (C) 2007-2012 Argeo GmbH
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -31,16 +31,18 @@ import javax.jcr.Session;
 import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.argeo.ArgeoMonitor;
 import org.argeo.jcr.JcrUtils;
 import org.argeo.slc.SlcException;
 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.RepoConstants;
 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;
 
@@ -49,24 +51,22 @@ import org.sonatype.aether.util.artifact.DefaultArtifact;
  * group of OSGi bundles.
  */
 public class NormalizeGroup implements Runnable, SlcNames {
-       public final static String BINARIES_ARTIFACT_ID = "binaries";
-       public final static String SOURCES_ARTIFACT_ID = "sources";
-       public final static String SDK_ARTIFACT_ID = "sdk";
-
        private final static Log log = LogFactory.getLog(NormalizeGroup.class);
 
        private Repository repository;
        private String workspace;
        private String groupId;
+       private Boolean overridePoms = false;
        private String artifactBasePath = "/";
-       private String version = null;// = "1.3.0";
-       private String parentPomCoordinates;// = "org.argeo:parent:1.3.0";
+       private String version = null;
+       private String parentPomCoordinates;
 
        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();
 
@@ -83,74 +83,140 @@ public class NormalizeGroup implements Runnable, SlcNames {
                Session session = null;
                try {
                        session = repository.login(workspace);
-
                        Node groupNode = session.getNode(MavenConventionsUtils.groupPath(
                                        artifactBasePath, groupId));
-                       // TODO factorize with a traverser pattern?
-                       for (NodeIterator artifactBases = groupNode.getNodes(); artifactBases
-                                       .hasNext();) {
-                               Node artifactBase = artifactBases.nextNode();
-                               if (artifactBase.isNodeType(SlcTypes.SLC_ARTIFACT_BASE)) {
-                                       for (NodeIterator artifactVersions = artifactBase
-                                                       .getNodes(); artifactVersions.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());
-                                                               }
+                       processGroupNode(groupNode, null);
+               } catch (Exception e) {
+                       throw new SlcException("Cannot normalize group " + groupId + " in "
+                                       + workspace, e);
+               } finally {
+                       JcrUtils.logoutQuietly(session);
+               }
+       }
+
+       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();
+               aBases: for (NodeIterator aBases = groupNode.getNodes(); aBases
+                               .hasNext();) {
+                       Node aBase = aBases.nextNode();
+                       if (aBase.isNodeType(SlcTypes.SLC_ARTIFACT_BASE)) {
+                               Node highestAVersion = null;
+                               for (NodeIterator aVersions = aBase.getNodes(); aVersions
+                                               .hasNext();) {
+                                       Node aVersion = aVersions.nextNode();
+                                       if (aVersion.isNodeType(SlcTypes.SLC_ARTIFACT_VERSION_BASE)) {
+                                               if (highestAVersion == null) {
+                                                       highestAVersion = aVersion;
+                                                       if (allArtifactsHighestVersion == null)
+                                                               allArtifactsHighestVersion = aVersion;
+
+                                                       // BS will fail if artifacts arrive in this order
+                                                       // Name1 - V1, name2 - V3, V1 will remain the
+                                                       // allArtifactsHighestVersion
+                                                       // Fixed below
+                                                       else {
+                                                               Version currVersion = extractOsgiVersion(aVersion);
+                                                               Version highestVersion = extractOsgiVersion(allArtifactsHighestVersion);
+                                                               if (currVersion.compareTo(highestVersion) > 0)
+                                                                       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());
-                       // }
-
-                       int bundleCount = symbolicNamesToNodes.size();
-                       if (log.isDebugEnabled())
-                               log.debug("Indexed " + bundleCount + " bundles");
-
-                       int count = 1;
-                       for (Node bundleNode : symbolicNamesToNodes.values()) {
-                               processBundleArtifact(bundleNode);
-                               bundleNode.getSession().save();
-                               if (log.isDebugEnabled())
-                                       log.debug(count + "/" + bundleCount + " Processed "
-                                                       + bundleNode.getName());
-                               count++;
-                       }
+               }
 
-                       // indexes
-                       Set<Artifact> indexes = new TreeSet<Artifact>(
-                                       new ArtifactIdComparator());
-                       Artifact indexArtifact = writeIndex(session, BINARIES_ARTIFACT_ID,
-                                       binaries);
-                       indexes.add(indexArtifact);
-                       indexArtifact = writeIndex(session, SOURCES_ARTIFACT_ID, sources);
-                       indexes.add(indexArtifact);
-                       // sdk
-                       writeIndex(session, SDK_ARTIFACT_ID, indexes);
-               } catch (Exception e) {
-                       throw new SlcException("Cannot normalize group " + groupId + " in "
-                                       + workspace, e);
-               } finally {
-                       JcrUtils.logoutQuietly(session);
+               // if version not set or empty, use the highest 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
+                               version = "0.0";
+//                             throw new SlcException("Group version " + version
+//                                             + " is empty.");
+
+               int bundleCount = symbolicNamesToNodes.size();
+               if (log.isDebugEnabled())
+                       log.debug("Indexed " + bundleCount + " bundles");
+
+               int count = 1;
+               for (Node bundleNode : symbolicNamesToNodes.values()) {
+                       processBundleArtifact(bundleNode);
+                       bundleNode.getSession().save();
+                       if (log.isDebugEnabled())
+                               log.debug(count + "/" + bundleCount + " Processed "
+                                               + bundleNode.getName());
+                       count++;
                }
+
+               // indexes
+               Set<Artifact> indexes = new TreeSet<Artifact>(
+                               new ArtifactIdComparator());
+               Artifact indexArtifact = writeIndex(session,
+                               RepoConstants.BINARIES_ARTIFACT_ID, binaries);
+               indexes.add(indexArtifact);
+               indexArtifact = writeIndex(session, RepoConstants.SOURCES_ARTIFACT_ID,
+                               sources);
+               indexes.add(indexArtifact);
+               // sdk
+               writeIndex(session, RepoConstants.SDK_ARTIFACT_ID, indexes);
+               if (monitor != null)
+                       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,
@@ -165,23 +231,23 @@ 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);
 
                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) {
@@ -206,6 +272,9 @@ public class NormalizeGroup implements Runnable, SlcNames {
                                return;// skip adding to binaries
                }
                binaries.add(RepoUtils.asArtifact(bundleNode));
+
+               if (bundleNode.getSession().hasPendingChanges())
+                       bundleNode.getSession().save();
        }
 
        protected void processBundleArtifact(Node bundleNode)
@@ -214,11 +283,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,
@@ -239,7 +310,6 @@ public class NormalizeGroup implements Runnable, SlcNames {
                p.append("<modelVersion>4.0.0</modelVersion>");
 
                // Artifact
-               // p.append("<parent><groupId>org.argeo</groupId><artifactId>parent</artifactId><version>1.2.0</version></parent>\n");
                p.append("<groupId>").append(JcrUtils.get(n, SLC_GROUP_ID))
                                .append("</groupId>\n");
                p.append("<artifactId>").append(JcrUtils.get(n, SLC_ARTIFACT_ID))
@@ -288,6 +358,8 @@ public class NormalizeGroup implements Runnable, SlcNames {
 
                // TODO require bundles
 
+               
+               
                List<Node> dependencyNodes = new ArrayList<Node>();
                for (String depSymbName : dependenciesSymbolicNames) {
                        if (depSymbName.equals(ownSymbolicName))
@@ -340,8 +412,9 @@ public class NormalizeGroup implements Runnable, SlcNames {
                p.append("<dependency>\n");
                p.append("\t<groupId>").append(groupId).append("</groupId>\n");
                p.append("\t<artifactId>")
-                               .append(ownSymbolicName.endsWith(".source") ? SOURCES_ARTIFACT_ID
-                                               : BINARIES_ARTIFACT_ID).append("</artifactId>\n");
+                               .append(ownSymbolicName.endsWith(".source") ? RepoConstants.SOURCES_ARTIFACT_ID
+                                               : RepoConstants.BINARIES_ARTIFACT_ID)
+                               .append("</artifactId>\n");
                p.append("\t<version>").append(version).append("</version>\n");
                p.append("\t<type>pom</type>\n");
                p.append("\t<scope>import</scope>\n");
@@ -381,4 +454,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;
+       }
 }