]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.repo/src/org/argeo/slc/repo/osgi/NormalizeGroup.java
Massive Argeo APIs refactoring
[gpl/argeo-slc.git] / org.argeo.slc.repo / src / org / argeo / slc / repo / osgi / NormalizeGroup.java
index 7a25c4bffc0bdbbc94f816d12f7a4ad1f75a2222..8679c8ce8a4d5e5776587a6948bc92e5c1fd8165 100644 (file)
@@ -15,8 +15,7 @@ import javax.jcr.RepositoryException;
 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.api.cms.CmsLog;
 import org.argeo.jcr.JcrMonitor;
 import org.argeo.jcr.JcrUtils;
 import org.argeo.slc.SlcException;
@@ -41,7 +40,7 @@ import org.osgi.framework.Version;
  */
 @Deprecated
 public class NormalizeGroup implements Runnable, SlcNames {
-       private final static Log log = LogFactory.getLog(NormalizeGroup.class);
+       private final static CmsLog log = CmsLog.getLog(NormalizeGroup.class);
 
        private Repository repository;
        private String workspace;
@@ -57,59 +56,49 @@ public class NormalizeGroup implements Runnable, SlcNames {
        // private JarFileIndexer jarFileIndexer = new JarFileIndexer();
 
        /** TODO make it more generic */
-       private List<String> systemPackages = OsgiProfile.PROFILE_JAVA_SE_1_6
-                       .getSystemPackages();
+       private List<String> systemPackages = OsgiProfile.PROFILE_JAVA_SE_1_6.getSystemPackages();
 
        // indexes
        private Map<String, String> packagesToSymbolicNames = new HashMap<String, String>();
        private Map<String, Node> symbolicNamesToNodes = new HashMap<String, Node>();
 
-       private Set<Artifact> binaries = new TreeSet<Artifact>(
-                       new ArtifactIdComparator());
-       private Set<Artifact> sources = new TreeSet<Artifact>(
-                       new ArtifactIdComparator());
+       private Set<Artifact> binaries = new TreeSet<Artifact>(new ArtifactIdComparator());
+       private Set<Artifact> sources = new TreeSet<Artifact>(new ArtifactIdComparator());
 
        public void run() {
                Session session = null;
                try {
                        session = repository.login(workspace);
-                       Node groupNode = session.getNode(MavenConventionsUtils.groupPath(
-                                       artifactBasePath, groupId));
+                       Node groupNode = session.getNode(MavenConventionsUtils.groupPath(artifactBasePath, groupId));
                        processGroupNode(groupNode, null);
                } catch (Exception e) {
-                       throw new SlcException("Cannot normalize group " + groupId + " in "
-                                       + workspace, 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, JcrMonitor monitor)
+       public static void processGroupNode(Node groupNode, String version, Boolean overridePoms, JcrMonitor monitor)
                        throws RepositoryException {
                // TODO set artifactsBase based on group node
                NormalizeGroup ng = new NormalizeGroup();
-               String groupId = groupNode.getProperty(SlcNames.SLC_GROUP_BASE_ID)
-                               .getString();
+               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, JcrMonitor monitor)
-                       throws RepositoryException {
+       protected void processGroupNode(Node groupNode, JcrMonitor 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();) {
+               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();) {
+                               for (NodeIterator aVersions = aBase.getNodes(); aVersions.hasNext();) {
                                        Node aVersion = aVersions.nextNode();
                                        if (aVersion.isNodeType(SlcTypes.SLC_ARTIFACT_VERSION_BASE)) {
                                                if (highestAVersion == null) {
@@ -134,8 +123,7 @@ public class NormalizeGroup implements Runnable, SlcNames {
                                                        if (currVersion.compareTo(currentHighestVersion) > 0) {
                                                                highestAVersion = aVersion;
                                                        }
-                                                       if (currVersion
-                                                                       .compareTo(extractOsgiVersion(allArtifactsHighestVersion)) > 0) {
+                                                       if (currVersion.compareTo(extractOsgiVersion(allArtifactsHighestVersion)) > 0) {
                                                                allArtifactsHighestVersion = aVersion;
                                                        }
                                                }
@@ -145,8 +133,7 @@ public class NormalizeGroup implements Runnable, SlcNames {
                                }
                                if (highestAVersion == null)
                                        continue aBases;
-                               for (NodeIterator files = highestAVersion.getNodes(); files
-                                               .hasNext();) {
+                               for (NodeIterator files = highestAVersion.getNodes(); files.hasNext();) {
                                        Node file = files.nextNode();
                                        if (file.isNodeType(SlcTypes.SLC_BUNDLE_ARTIFACT)) {
                                                preProcessBundleArtifact(file);
@@ -165,8 +152,7 @@ public class NormalizeGroup implements Runnable, SlcNames {
                // => the version can then be left empty
                if (version == null || version.trim().equals(""))
                        if (allArtifactsHighestVersion != null)
-                               version = allArtifactsHighestVersion.getProperty(
-                                               SLC_ARTIFACT_VERSION).getString();
+                               version = allArtifactsHighestVersion.getProperty(SLC_ARTIFACT_VERSION).getString();
                        else
                                version = "0.0";
                // throw new SlcException("Group version " + version
@@ -181,19 +167,15 @@ public class NormalizeGroup implements Runnable, SlcNames {
                        processBundleArtifact(bundleNode);
                        bundleNode.getSession().save();
                        if (log.isDebugEnabled())
-                               log.debug(count + "/" + bundleCount + " Processed "
-                                               + bundleNode.getName());
+                               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);
+               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);
+               indexArtifact = writeIndex(session, RepoConstants.SOURCES_ARTIFACT_ID, sources);
                indexes.add(indexArtifact);
                // sdk
                writeIndex(session, RepoConstants.SDK_ARTIFACT_ID, indexes);
@@ -201,10 +183,8 @@ public class NormalizeGroup implements Runnable, SlcNames {
                        monitor.worked(1);
        }
 
-       private Version extractOsgiVersion(Node artifactVersion)
-                       throws RepositoryException {
-               String rawVersion = artifactVersion.getProperty(SLC_ARTIFACT_VERSION)
-                               .getString();
+       private Version extractOsgiVersion(Node artifactVersion) throws RepositoryException {
+               String rawVersion = artifactVersion.getProperty(SLC_ARTIFACT_VERSION).getString();
                String cleanVersion = rawVersion.replace("-SNAPSHOT", ".SNAPSHOT");
                Version osgiVersion = null;
                // log invalid version value to enable tracking them
@@ -248,37 +228,29 @@ public class NormalizeGroup implements Runnable, SlcNames {
                return tmpVersion;
        }
 
-       private Artifact writeIndex(Session session, String artifactId,
-                       Set<Artifact> artifacts) throws RepositoryException {
-               Artifact artifact = new DefaultArtifact(groupId, artifactId, "pom",
-                               version);
-               Artifact parentArtifact = parentPomCoordinates != null ? new DefaultArtifact(
-                               parentPomCoordinates) : null;
-               String pom = MavenConventionsUtils.artifactsAsDependencyPom(artifact,
-                               artifacts, parentArtifact);
-               Node node = RepoUtils.copyBytesAsArtifact(
-                               session.getNode(artifactBasePath), artifact, pom.getBytes());
+       private Artifact writeIndex(Session session, String artifactId, Set<Artifact> artifacts)
+                       throws RepositoryException {
+               Artifact artifact = new DefaultArtifact(groupId, artifactId, "pom", version);
+               Artifact parentArtifact = parentPomCoordinates != null ? new DefaultArtifact(parentPomCoordinates) : null;
+               String pom = MavenConventionsUtils.artifactsAsDependencyPom(artifact, artifacts, parentArtifact);
+               Node node = RepoUtils.copyBytesAsArtifact(session.getNode(artifactBasePath), artifact, pom.getBytes());
                artifactIndexer.index(node);
 
                // TODO factorize
                String pomSha = JcrUtils.checksumFile(node, "SHA-1");
-               JcrUtils.copyBytesAsFile(node.getParent(), node.getName() + ".sha1",
-                               pomSha.getBytes());
+               JcrUtils.copyBytesAsFile(node.getParent(), node.getName() + ".sha1", pomSha.getBytes());
                String pomMd5 = JcrUtils.checksumFile(node, "MD5");
-               JcrUtils.copyBytesAsFile(node.getParent(), node.getName() + ".md5",
-                               pomMd5.getBytes());
+               JcrUtils.copyBytesAsFile(node.getParent(), node.getName() + ".md5", pomMd5.getBytes());
                session.save();
                return artifact;
        }
 
-       protected void preProcessBundleArtifact(Node bundleNode)
-                       throws RepositoryException {
+       protected void preProcessBundleArtifact(Node bundleNode) throws RepositoryException {
 
                String symbolicName = JcrUtils.get(bundleNode, SLC_SYMBOLIC_NAME);
                if (symbolicName.endsWith(".source")) {
                        // TODO make a shared node with classifier 'sources'?
-                       String bundleName = RepoUtils
-                                       .extractBundleNameFromSourceName(symbolicName);
+                       String bundleName = RepoUtils.extractBundleNameFromSourceName(symbolicName);
                        for (String excludedSuffix : excludedSuffixes) {
                                if (bundleName.endsWith(excludedSuffix))
                                        return;// skip adding to sources
@@ -287,8 +259,7 @@ public class NormalizeGroup implements Runnable, SlcNames {
                        return;
                }
 
-               NodeIterator exportPackages = bundleNode.getNodes(SLC_
-                               + Constants.EXPORT_PACKAGE);
+               NodeIterator exportPackages = bundleNode.getNodes(SLC_ + Constants.EXPORT_PACKAGE);
                while (exportPackages.hasNext()) {
                        Node exportPackage = exportPackages.nextNode();
                        String pkg = JcrUtils.get(exportPackage, SLC_NAME);
@@ -306,8 +277,7 @@ public class NormalizeGroup implements Runnable, SlcNames {
                        bundleNode.getSession().save();
        }
 
-       protected void processBundleArtifact(Node bundleNode)
-                       throws RepositoryException {
+       protected void processBundleArtifact(Node bundleNode) throws RepositoryException {
                Node artifactFolder = bundleNode.getParent();
                String baseName = FilenameUtils.getBaseName(bundleNode.getName());
 
@@ -317,15 +287,12 @@ public class NormalizeGroup implements Runnable, SlcNames {
                        return;// skip
 
                String pom = generatePomForBundle(bundleNode);
-               Node pomNode = JcrUtils.copyBytesAsFile(artifactFolder, pomName,
-                               pom.getBytes());
+               Node pomNode = JcrUtils.copyBytesAsFile(artifactFolder, pomName, pom.getBytes());
                // checksum
                String bundleSha = JcrUtils.checksumFile(bundleNode, "SHA-1");
-               JcrUtils.copyBytesAsFile(artifactFolder,
-                               bundleNode.getName() + ".sha1", bundleSha.getBytes());
+               JcrUtils.copyBytesAsFile(artifactFolder, bundleNode.getName() + ".sha1", bundleSha.getBytes());
                String pomSha = JcrUtils.checksumFile(pomNode, "SHA-1");
-               JcrUtils.copyBytesAsFile(artifactFolder, pomNode.getName() + ".sha1",
-                               pomSha.getBytes());
+               JcrUtils.copyBytesAsFile(artifactFolder, pomNode.getName() + ".sha1", pomSha.getBytes());
        }
 
        private String generatePomForBundle(Node n) throws RepositoryException {
@@ -335,53 +302,42 @@ public class NormalizeGroup implements Runnable, SlcNames {
 
                // XML header
                p.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
-               p.append("<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n");
+               p.append(
+                               "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n");
                p.append("<modelVersion>4.0.0</modelVersion>");
 
                // Artifact
-               p.append("<groupId>").append(JcrUtils.get(n, SLC_GROUP_ID))
-                               .append("</groupId>\n");
-               p.append("<artifactId>").append(JcrUtils.get(n, SLC_ARTIFACT_ID))
-                               .append("</artifactId>\n");
-               p.append("<version>").append(JcrUtils.get(n, SLC_ARTIFACT_VERSION))
-                               .append("</version>\n");
+               p.append("<groupId>").append(JcrUtils.get(n, SLC_GROUP_ID)).append("</groupId>\n");
+               p.append("<artifactId>").append(JcrUtils.get(n, SLC_ARTIFACT_ID)).append("</artifactId>\n");
+               p.append("<version>").append(JcrUtils.get(n, SLC_ARTIFACT_VERSION)).append("</version>\n");
                p.append("<packaging>pom</packaging>\n");
                if (n.hasProperty(SLC_ + Constants.BUNDLE_NAME))
-                       p.append("<name>")
-                                       .append(JcrUtils.get(n, SLC_ + Constants.BUNDLE_NAME))
-                                       .append("</name>\n");
+                       p.append("<name>").append(JcrUtils.get(n, SLC_ + Constants.BUNDLE_NAME)).append("</name>\n");
                if (n.hasProperty(SLC_ + Constants.BUNDLE_DESCRIPTION))
-                       p.append("<description>")
-                                       .append(JcrUtils
-                                                       .get(n, SLC_ + Constants.BUNDLE_DESCRIPTION))
+                       p.append("<description>").append(JcrUtils.get(n, SLC_ + Constants.BUNDLE_DESCRIPTION))
                                        .append("</description>\n");
 
                // Dependencies
                Set<String> dependenciesSymbolicNames = new TreeSet<String>();
                Set<String> optionalSymbolicNames = new TreeSet<String>();
-               NodeIterator importPackages = n.getNodes(SLC_
-                               + Constants.IMPORT_PACKAGE);
+               NodeIterator importPackages = n.getNodes(SLC_ + Constants.IMPORT_PACKAGE);
                while (importPackages.hasNext()) {
                        Node importPackage = importPackages.nextNode();
                        String pkg = JcrUtils.get(importPackage, SLC_NAME);
                        if (packagesToSymbolicNames.containsKey(pkg)) {
-                               String dependencySymbolicName = packagesToSymbolicNames
-                                               .get(pkg);
+                               String dependencySymbolicName = packagesToSymbolicNames.get(pkg);
                                if (JcrUtils.check(importPackage, SLC_OPTIONAL))
                                        optionalSymbolicNames.add(dependencySymbolicName);
                                else
                                        dependenciesSymbolicNames.add(dependencySymbolicName);
                        } else {
-                               if (!JcrUtils.check(importPackage, SLC_OPTIONAL)
-                                               && !systemPackages.contains(pkg))
+                               if (!JcrUtils.check(importPackage, SLC_OPTIONAL) && !systemPackages.contains(pkg))
                                        log.warn("No bundle found for pkg " + pkg);
                        }
                }
 
                if (n.hasNode(SLC_ + Constants.FRAGMENT_HOST)) {
-                       String fragmentHost = JcrUtils.get(
-                                       n.getNode(SLC_ + Constants.FRAGMENT_HOST),
-                                       SLC_SYMBOLIC_NAME);
+                       String fragmentHost = JcrUtils.get(n.getNode(SLC_ + Constants.FRAGMENT_HOST), SLC_SYMBOLIC_NAME);
                        dependenciesSymbolicNames.add(fragmentHost);
                }
 
@@ -400,8 +356,7 @@ public class NormalizeGroup implements Runnable, SlcNames {
                List<Node> optionalDependencyNodes = new ArrayList<Node>();
                for (String depSymbName : optionalSymbolicNames) {
                        if (symbolicNamesToNodes.containsKey(depSymbName))
-                               optionalDependencyNodes.add(symbolicNamesToNodes
-                                               .get(depSymbName));
+                               optionalDependencyNodes.add(symbolicNamesToNodes.get(depSymbName));
                        else
                                log.warn("Could not find node for " + depSymbName);
                }
@@ -409,12 +364,8 @@ public class NormalizeGroup implements Runnable, SlcNames {
                p.append("<dependencies>\n");
                for (Node dependencyNode : dependencyNodes) {
                        p.append("<dependency>\n");
-                       p.append("\t<groupId>")
-                                       .append(JcrUtils.get(dependencyNode, SLC_GROUP_ID))
-                                       .append("</groupId>\n");
-                       p.append("\t<artifactId>")
-                                       .append(JcrUtils.get(dependencyNode, SLC_ARTIFACT_ID))
-                                       .append("</artifactId>\n");
+                       p.append("\t<groupId>").append(JcrUtils.get(dependencyNode, SLC_GROUP_ID)).append("</groupId>\n");
+                       p.append("\t<artifactId>").append(JcrUtils.get(dependencyNode, SLC_ARTIFACT_ID)).append("</artifactId>\n");
                        p.append("</dependency>\n");
                }
 
@@ -422,12 +373,8 @@ public class NormalizeGroup implements Runnable, SlcNames {
                        p.append("<!-- OPTIONAL -->\n");
                for (Node dependencyNode : optionalDependencyNodes) {
                        p.append("<dependency>\n");
-                       p.append("\t<groupId>")
-                                       .append(JcrUtils.get(dependencyNode, SLC_GROUP_ID))
-                                       .append("</groupId>\n");
-                       p.append("\t<artifactId>")
-                                       .append(JcrUtils.get(dependencyNode, SLC_ARTIFACT_ID))
-                                       .append("</artifactId>\n");
+                       p.append("\t<groupId>").append(JcrUtils.get(dependencyNode, SLC_GROUP_ID)).append("</groupId>\n");
+                       p.append("\t<artifactId>").append(JcrUtils.get(dependencyNode, SLC_ARTIFACT_ID)).append("</artifactId>\n");
                        p.append("\t<optional>true</optional>\n");
                        p.append("</dependency>\n");
                }
@@ -438,10 +385,8 @@ public class NormalizeGroup implements Runnable, SlcNames {
                p.append("<dependencies>\n");
                p.append("<dependency>\n");
                p.append("\t<groupId>").append(groupId).append("</groupId>\n");
-               p.append("\t<artifactId>")
-                               .append(ownSymbolicName.endsWith(".source") ? RepoConstants.SOURCES_ARTIFACT_ID
-                                               : RepoConstants.BINARIES_ARTIFACT_ID)
-                               .append("</artifactId>\n");
+               p.append("\t<artifactId>").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");