Clean repo indexer, update modular distribution management, remove binaries concepts.
authorBruno Sinou <bsinou@argeo.org>
Tue, 17 Jun 2014 12:05:49 +0000 (12:05 +0000)
committerBruno Sinou <bsinou@argeo.org>
Tue, 17 Jun 2014 12:05:49 +0000 (12:05 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@7065 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/NormalizeWorkspace.java
runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/ArtifactIndexer.java
runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/JarFileIndexer.java
runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/ModularDistributionIndexer.java
runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ProcessDistribution.java

index 7f3f3b5a3968d090ee9c649eff823b4a21c25bf1..7212756ce1f34b8319f182e26e7ea90a05effe3e 100644 (file)
@@ -36,8 +36,8 @@ import org.argeo.slc.SlcException;
 import org.argeo.slc.client.ui.dist.DistPlugin;
 import org.argeo.slc.jcr.SlcNames;
 import org.argeo.slc.repo.ArtifactIndexer;
-import org.argeo.slc.repo.ModularDistributionIndexer;
 import org.argeo.slc.repo.JarFileIndexer;
+import org.argeo.slc.repo.ModularDistributionIndexer;
 import org.argeo.slc.repo.PdeSourcesIndexer;
 import org.argeo.slc.repo.RepoConstants;
 import org.argeo.slc.repo.RepoUtils;
@@ -75,10 +75,11 @@ public class NormalizeWorkspace extends AbstractHandler implements SlcNames {
        private Repository repository;
 
        // Relevant default node indexers
-       private ArtifactIndexer artifactIndexer = new ArtifactIndexer();
-       private JarFileIndexer jarFileIndexer = new JarFileIndexer();
+       // WARNING Order call is important.
        private ModularDistributionIndexer distBundleIndexer = new ModularDistributionIndexer();
+       private JarFileIndexer jarFileIndexer = new JarFileIndexer();
        private PdeSourcesIndexer pdeSourceIndexer = new PdeSourcesIndexer();
+       private ArtifactIndexer artifactIndexer = new ArtifactIndexer();
 
        public Object execute(ExecutionEvent event) throws ExecutionException {
                String targetRepoPath = event.getParameter(PARAM_TARGET_REPO_PATH);
@@ -188,9 +189,9 @@ public class NormalizeWorkspace extends AbstractHandler implements SlcNames {
                                        if (jarFileIndexer.support(node.getPath()))
                                                if (artifactIndexer.support(node.getPath())) {
                                                        monitor.subTask(node.getName());
-                                                       artifactIndexer.index(node);
-                                                       jarFileIndexer.index(node);
                                                        distBundleIndexer.index(node);
+                                                       jarFileIndexer.index(node);
+                                                       artifactIndexer.index(node);
                                                        if (node.getSession().hasPendingChanges()) {
                                                                node.getSession().save();
                                                                if (log.isDebugEnabled())
@@ -200,8 +201,9 @@ public class NormalizeWorkspace extends AbstractHandler implements SlcNames {
                                                        monitor.worked(1);
                                                }
                                } else if (node.getName().endsWith(".pom")) {
-                                       if (distBundleIndexer.support(node.getPath()))
-                                               distBundleIndexer.index(node);
+                                       // Removed: we do not support binaries concept anymore.
+                                       // if (distBundleIndexer.support(node.getPath()))
+                                       // distBundleIndexer.index(node);
                                        if (artifactIndexer.support(node.getPath()))
                                                artifactIndexer.index(node);
                                        if (node.getSession().hasPendingChanges()) {
index 6de832b91a6e323e265f2ff713f838439ad08b02..27c6806a3e98d062c43462e5ffcf67c95f8e6dbb 100644 (file)
@@ -16,6 +16,7 @@
 package org.argeo.slc.repo;
 
 import javax.jcr.Node;
+import javax.jcr.NodeIterator;
 import javax.jcr.RepositoryException;
 import javax.jcr.nodetype.NodeType;
 
@@ -97,24 +98,25 @@ public class ArtifactIndexer implements NodeIndexer, SlcNames {
                                                md5.getBytes());
                        }
 
-                       // Create a default pom only with artifact coordinates if none
-                       // already exist
+                       // Create a default pom if none already exist
                        String fileNodeName = fileNode.getName();
-                       String pomName= null;
+                       String pomName = null;
                        if (fileNodeName.endsWith(".jar"))
-                               pomName = fileNodeName.substring(0, fileNodeName.length()-".jar".length()) + ".pom";
-                       
+                               pomName = fileNodeName.substring(0, fileNodeName.length()
+                                               - ".jar".length())
+                                               + ".pom";
+
                        if (pomName != null && !fileNode.getParent().hasNode(pomName)) {
                                String pom = generatePomForBundle(fileNode);
                                Node pomNode = JcrUtils.copyBytesAsFile(fileNode.getParent(),
                                                pomName, pom.getBytes());
                                // corresponding check sums
                                String sha = JcrUtils.checksumFile(pomNode, "SHA-1");
-                               JcrUtils.copyBytesAsFile(fileNode.getParent(), pomName +  ".sha1",                                              
-                                               sha.getBytes());
+                               JcrUtils.copyBytesAsFile(fileNode.getParent(), pomName
+                                               + ".sha1", sha.getBytes());
                                String md5 = JcrUtils.checksumFile(fileNode, "MD5");
-                               JcrUtils.copyBytesAsFile(fileNode.getParent(), pomName +".md5",
-                                               md5.getBytes());
+                               JcrUtils.copyBytesAsFile(fileNode.getParent(),
+                                               pomName + ".md5", md5.getBytes());
                        }
 
                        // set higher levels
@@ -182,20 +184,19 @@ public class ArtifactIndexer implements NodeIndexer, SlcNames {
 
        private String generatePomForBundle(Node n) throws RepositoryException {
                StringBuffer p = new StringBuffer();
-
-               // 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("<modelVersion>4.0.0</modelVersion>");
 
-               // Artifact
+               // Categorized name version
                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");
+               // TODO make it more generic
+               p.append("<packaging>jar</packaging>\n");
                if (n.hasProperty(SLC_ + Constants.BUNDLE_NAME))
                        p.append("<name>")
                                        .append(JcrUtils.get(n, SLC_ + Constants.BUNDLE_NAME))
@@ -205,7 +206,66 @@ public class ArtifactIndexer implements NodeIndexer, SlcNames {
                                        .append(JcrUtils
                                                        .get(n, SLC_ + Constants.BUNDLE_DESCRIPTION))
                                        .append("</description>\n");
+
+               // Dependencies in case of a distribution
+               if (n.isNodeType(SlcTypes.SLC_MODULAR_DISTRIBUTION)) {
+                       p.append(getDependenciesSnippet(n.getNode(SlcNames.SLC_MODULES)
+                                       .getNodes()));
+                       p.append(getDependencyManagementSnippet(n.getNode(
+                                       SlcNames.SLC_MODULES).getNodes()));
+               }
                p.append("</project>\n");
                return p.toString();
        }
+
+       private String getDependenciesSnippet(NodeIterator nit)
+                       throws RepositoryException {
+               StringBuilder b = new StringBuilder();
+               b.append("<dependencies>\n");
+               while (nit.hasNext()) {
+                       Node currModule = nit.nextNode();
+                       if (currModule.isNodeType(SlcTypes.SLC_MODULE_COORDINATES)) {
+                               b.append(getDependencySnippet(
+                                               currModule.getProperty(SlcNames.SLC_CATEGORY)
+                                                               .getString(),
+                                               currModule.getProperty(SlcNames.SLC_NAME).getString(),
+                                               null));
+                       }
+               }
+               b.append("</dependencies>\n");
+               return b.toString();
+       }
+
+       private String getDependencyManagementSnippet(NodeIterator nit)
+                       throws RepositoryException {
+               StringBuilder b = new StringBuilder();
+               b.append("<dependencyManagement>\n");
+               b.append("<dependencies>\n");
+               while (nit.hasNext()) {
+                       Node currModule = nit.nextNode();
+                       if (currModule.isNodeType(SlcTypes.SLC_MODULE_COORDINATES)) {
+                               b.append(getDependencySnippet(
+                                               currModule.getProperty(SlcNames.SLC_CATEGORY)
+                                                               .getString(),
+                                               currModule.getProperty(SlcNames.SLC_NAME).getString(),
+                                               currModule.getProperty(SlcNames.SLC_VERSION)
+                                                               .getString()));
+                       }
+               }
+               b.append("</dependencies>\n");
+               b.append("</dependencyManagement>\n");
+               return b.toString();
+       }
+
+       private String getDependencySnippet(String category, String name,
+                       String version) {
+               StringBuilder b = new StringBuilder();
+               b.append("<dependency>\n");
+               b.append("\t<groupId>").append(category).append("</groupId>\n");
+               b.append("\t<artifactId>").append(name).append("</artifactId>\n");
+               if (version != null)
+                       b.append("\t<version>").append(version).append("</version>\n");
+               b.append("</dependency>\n");
+               return b.toString();
+       }
 }
\ No newline at end of file
index 6ade4464bb1a5c8188752cd80de2bc020460c774..b07cade3367474c79e311e723687cc122d5a8821 100644 (file)
@@ -157,8 +157,8 @@ public class JarFileIndexer implements NodeIndexer, SlcNames {
                try {
                        jarIn = new JarInputStream(fileBinary.getStream());
                        String bundleLocalization = null;
-                       
-                       String blKey =  Constants.BUNDLE_LOCALIZATION; //"Bundle-Localization";
+
+                       String blKey = Constants.BUNDLE_LOCALIZATION; // "Bundle-Localization";
                        Name blkName = new Name(blKey);
 
                        browse: for (Object obj : attrs.keySet()) {
@@ -231,6 +231,15 @@ public class JarFileIndexer implements NodeIndexer, SlcNames {
 
        protected void addOsgiMetadata(Node fileNode, Attributes attrs)
                        throws RepositoryException {
+
+               // TODO remove this ?
+               // Compulsory for the time being, because bundle artifact extends
+               // artifact
+               if (!fileNode.isNodeType(SlcTypes.SLC_ARTIFACT)) {
+                       ArtifactIndexer indexer = new ArtifactIndexer();
+                       indexer.index(fileNode);
+               }
+
                fileNode.addMixin(SlcTypes.SLC_BUNDLE_ARTIFACT);
 
                // symbolic name
index 3070b9b757ef909038c8345c17de2c9795d2ac16..3f9f7dcc5e25150ab7cb5dcf4c6afe855ae18fd8 100644 (file)
@@ -1,13 +1,10 @@
 package org.argeo.slc.repo;
 
 import java.io.BufferedReader;
-import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Properties;
 import java.util.StringTokenizer;
 import java.util.jar.JarEntry;
 import java.util.jar.JarInputStream;
@@ -18,8 +15,6 @@ import javax.jcr.Node;
 import javax.jcr.Property;
 import javax.jcr.RepositoryException;
 import javax.jcr.nodetype.NodeType;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
 
 import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.io.IOUtils;
@@ -37,17 +32,14 @@ import org.argeo.slc.jcr.SlcTypes;
 import org.osgi.framework.Constants;
 import org.sonatype.aether.artifact.Artifact;
 import org.sonatype.aether.util.artifact.DefaultArtifact;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.NodeList;
 
 /**
  * Create or update JCR meta-data for an SLC Modular Distribution
  * 
  * Currently, following types are managed: <list><li>* .jar: dependency
- * artifacts with csv index</li> <li>.pom: artifact (binaries) that indexes a
- * group, the .pom file contains a tag "dependencyManagement" that list all
- * modules</li> </list>
+ * artifacts with csv index</li> <li>@Deprecated : .pom: artifact (binaries)
+ * that indexes a group, the .pom file contains a tag "dependencyManagement"
+ * that list all modules</li> </list>
  */
 public class ModularDistributionIndexer implements NodeIndexer, SlcNames {
        private final static Log log = LogFactory
@@ -57,28 +49,26 @@ public class ModularDistributionIndexer implements NodeIndexer, SlcNames {
        private final static String INDEX_FILE_NAME = "modularDistribution.csv";
        private String separator = ",";
 
-       // Artifact indexing
-       private final static List<String> BINARIES_ARTIFACTS_NAME;
-       static {
-               List<String> tmpList = new ArrayList<String>();
-               tmpList.add(RepoConstants.BINARIES_ARTIFACT_ID);
-               // tmpList.add(RepoConstants.SOURCES_ARTIFACT_ID);
-               // tmpList.add(RepoConstants.SDK_ARTIFACT_ID);
-               BINARIES_ARTIFACTS_NAME = Collections.unmodifiableList(tmpList);
-       }
+       // TODO remove this: binaries have been replaced by modular distribution
+       // // Artifact indexing
+       // private final static List<String> BINARIES_ARTIFACTS_NAME;
+       // static {
+       // List<String> tmpList = new ArrayList<String>();
+       // tmpList.add(RepoConstants.BINARIES_ARTIFACT_ID);
+       // // tmpList.add(RepoConstants.SOURCES_ARTIFACT_ID);
+       // // tmpList.add(RepoConstants.SDK_ARTIFACT_ID);
+       // BINARIES_ARTIFACTS_NAME = Collections.unmodifiableList(tmpList);
+       // }
 
        private Manifest manifest;
 
-       // private Comparator<Artifact> artifactComparator = new
-       // ArtifactIdComparator();
-
        public Boolean support(String path) {
                if (FilenameUtils.getExtension(path).equals("jar"))
                        return true;
-               if (FilenameUtils.getExtension(path).equals("pom")
-                               && BINARIES_ARTIFACTS_NAME.contains(FilenameUtils.getName(path)
-                                               .split("-")[0]))
-                       return true;
+               // if (FilenameUtils.getExtension(path).equals("pom")
+               // && BINARIES_ARTIFACTS_NAME.contains(FilenameUtils.getName(path)
+               // .split("-")[0]))
+               // return true;
                return false;
        }
 
@@ -99,9 +89,9 @@ public class ModularDistributionIndexer implements NodeIndexer, SlcNames {
                        MyModularDistribution currDist = null;
                        if (FilenameUtils.getExtension(fileNode.getPath()).equals("jar"))
                                currDist = listModulesFromCsvIndex(fileNode, fileBinary);
-                       else if (FilenameUtils.getExtension(fileNode.getPath()).equals(
-                                       "pom"))
-                               currDist = listModulesFromPomIndex(fileNode, fileBinary);
+                       // else if (FilenameUtils.getExtension(fileNode.getPath()).equals(
+                       // "pom"))
+                       // currDist = listModulesFromPomIndex(fileNode, fileBinary);
 
                        if (fileNode.isNodeType(SlcTypes.SLC_MODULAR_DISTRIBUTION)
                                        || currDist == null || !currDist.nameVersions().hasNext())
@@ -174,7 +164,7 @@ public class ModularDistributionIndexer implements NodeIndexer, SlcNames {
                        String version = manifest.getMainAttributes().getValue(
                                        Constants.BUNDLE_VERSION);
 
-                       Artifact distribution = new DefaultArtifact(category, name, "pom",
+                       Artifact distribution = new DefaultArtifact(category, name, "jar",
                                        version);
                        // Retrieve the index file
                        JarEntry indexEntry;
@@ -192,7 +182,7 @@ public class ModularDistributionIndexer implements NodeIndexer, SlcNames {
                                }
                        }
                        if (indexEntry == null)
-                               return null; // Not a modular definition artifact
+                               return null; // Not a modular definition
 
                        // Process the index
                        reader = new BufferedReader(new InputStreamReader(jarIn));
@@ -217,66 +207,67 @@ public class ModularDistributionIndexer implements NodeIndexer, SlcNames {
                }
        }
 
-       private MyModularDistribution listModulesFromPomIndex(Node fileNode,
-                       Binary fileBinary) {
-               InputStream input = null;
-               List<CategorizedNameVersion> modules = new ArrayList<CategorizedNameVersion>();
-               try {
-                       input = fileBinary.getStream();
-
-                       DocumentBuilder documentBuilder = DocumentBuilderFactory
-                                       .newInstance().newDocumentBuilder();
-                       Document doc = documentBuilder.parse(input);
-                       // properties
-                       Properties props = new Properties();
-                       // props.setProperty("project.version",
-                       // pomArtifact.getBaseVersion());
-                       NodeList properties = doc.getElementsByTagName("properties");
-                       if (properties.getLength() > 0) {
-                               NodeList propertiesElems = properties.item(0).getChildNodes();
-                               for (int i = 0; i < propertiesElems.getLength(); i++) {
-                                       if (propertiesElems.item(i) instanceof Element) {
-                                               Element property = (Element) propertiesElems.item(i);
-                                               props.put(property.getNodeName(),
-                                                               property.getTextContent());
-                                       }
-                               }
-                       }
-
-                       // full coordinates are under <dependencyManagement><dependencies>
-                       NodeList dependencies = ((Element) doc.getElementsByTagName(
-                                       "dependencyManagement").item(0))
-                                       .getElementsByTagName("dependency");
-                       for (int i = 0; i < dependencies.getLength(); i++) {
-                               Element dependency = (Element) dependencies.item(i);
-                               String groupId = dependency.getElementsByTagName("groupId")
-                                               .item(0).getTextContent().trim();
-                               String artifactId = dependency
-                                               .getElementsByTagName("artifactId").item(0)
-                                               .getTextContent().trim();
-                               String version = dependency.getElementsByTagName("version")
-                                               .item(0).getTextContent().trim();
-                               modules.add(new MyCategorizedNameVersion(groupId, artifactId,
-                                               version));
-                       }
-
-                       String groupId = doc.getElementsByTagName("groupId").item(0)
-                                       .getTextContent().trim();
-                       String artifactId = doc.getElementsByTagName("artifactId").item(0)
-                                       .getTextContent().trim();
-                       String version = doc.getElementsByTagName("version").item(0)
-                                       .getTextContent().trim();
-
-                       Artifact currDist = new DefaultArtifact(groupId, artifactId, "pom",
-                                       version);
-
-                       return new MyModularDistribution(currDist, modules);
-               } catch (Exception e) {
-                       throw new SlcException("Cannot process pom " + fileNode, e);
-               } finally {
-                       IOUtils.closeQuietly(input);
-               }
-       }
+       // private MyModularDistribution listModulesFromPomIndex(Node fileNode,
+       // Binary fileBinary) {
+       // InputStream input = null;
+       // List<CategorizedNameVersion> modules = new
+       // ArrayList<CategorizedNameVersion>();
+       // try {
+       // input = fileBinary.getStream();
+       //
+       // DocumentBuilder documentBuilder = DocumentBuilderFactory
+       // .newInstance().newDocumentBuilder();
+       // Document doc = documentBuilder.parse(input);
+       // // properties
+       // Properties props = new Properties();
+       // // props.setProperty("project.version",
+       // // pomArtifact.getBaseVersion());
+       // NodeList properties = doc.getElementsByTagName("properties");
+       // if (properties.getLength() > 0) {
+       // NodeList propertiesElems = properties.item(0).getChildNodes();
+       // for (int i = 0; i < propertiesElems.getLength(); i++) {
+       // if (propertiesElems.item(i) instanceof Element) {
+       // Element property = (Element) propertiesElems.item(i);
+       // props.put(property.getNodeName(),
+       // property.getTextContent());
+       // }
+       // }
+       // }
+       //
+       // // full coordinates are under <dependencyManagement><dependencies>
+       // NodeList dependencies = ((Element) doc.getElementsByTagName(
+       // "dependencyManagement").item(0))
+       // .getElementsByTagName("dependency");
+       // for (int i = 0; i < dependencies.getLength(); i++) {
+       // Element dependency = (Element) dependencies.item(i);
+       // String groupId = dependency.getElementsByTagName("groupId")
+       // .item(0).getTextContent().trim();
+       // String artifactId = dependency
+       // .getElementsByTagName("artifactId").item(0)
+       // .getTextContent().trim();
+       // String version = dependency.getElementsByTagName("version")
+       // .item(0).getTextContent().trim();
+       // modules.add(new MyCategorizedNameVersion(groupId, artifactId,
+       // version));
+       // }
+       //
+       // String groupId = doc.getElementsByTagName("groupId").item(0)
+       // .getTextContent().trim();
+       // String artifactId = doc.getElementsByTagName("artifactId").item(0)
+       // .getTextContent().trim();
+       // String version = doc.getElementsByTagName("version").item(0)
+       // .getTextContent().trim();
+       //
+       // Artifact currDist = new DefaultArtifact(groupId, artifactId, "pom",
+       // version);
+       //
+       // return new MyModularDistribution(currDist, modules);
+       // } catch (Exception e) {
+       // throw new SlcException("Cannot process pom " + fileNode, e);
+       // } finally {
+       // IOUtils.closeQuietly(input);
+       // }
+       // }
 
        /** The created modular distribution */
        private static class MyCategorizedNameVersion extends DefaultNameVersion
@@ -328,5 +319,4 @@ public class ModularDistributionIndexer implements NodeIndexer, SlcNames {
        public void setSeparator(String modulesUrlSeparator) {
                this.separator = modulesUrlSeparator;
        }
-
 }
\ No newline at end of file
index d852fb623b249fe9a41312797c484b5db4a139b8..544b1c634aa38e2fd6539be2850395ee580fcbd3 100644 (file)
@@ -37,9 +37,11 @@ public class ProcessDistribution implements Runnable {
                        ModularDistributionFactory mdf = new ModularDistributionFactory(
                                        javaSession, osgiDistribution);
                        mdf.run();
-                       // javaSession.save();
 
+                       // TODO why is the created distribution not automatically indexed?
                        // osgiFactory.indexNode(node);
+                       // javaSession.save();
+
                        // Node artifact = createDistributionArtifact(javaSession,
                        // osgiDistribution);