]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/ImportMavenDependencies.java
Improve packaging (esp. security)
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / maven / ImportMavenDependencies.java
index 8aff3de08f21beba8d54e10cccd87097bde7c841..c4020d92f344c522d8227cc244e8f5718ff3f5ee 100644 (file)
@@ -2,26 +2,29 @@ package org.argeo.slc.repo.maven;
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
-import java.io.StringReader;
-import java.io.StringWriter;
+import java.io.FileInputStream;
 import java.util.Comparator;
 import java.util.HashSet;
 import java.util.Properties;
 import java.util.Set;
 import java.util.TreeSet;
 
+import javax.jcr.Binary;
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.Session;
+import javax.jcr.nodetype.NodeType;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.argeo.jcr.JcrUtils;
 import org.argeo.slc.SlcException;
 import org.argeo.slc.aether.AetherTemplate;
-import org.argeo.slc.aether.AetherUtils;
 import org.sonatype.aether.artifact.Artifact;
 import org.sonatype.aether.graph.DependencyNode;
 import org.sonatype.aether.util.artifact.DefaultArtifact;
-import org.sonatype.aether.util.graph.PreorderNodeListGenerator;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
@@ -34,23 +37,17 @@ public class ImportMavenDependencies implements Runnable {
        private String rootCoordinates;
        private Set<String> excludedArtifacts = new HashSet<String>();
 
+       private Session jcrSession;
+       private String artifactBasePath = "/slc/repo/artifacts";
+
        public void run() {
+               Set<Artifact> artifacts = resolveDistribution();
+               syncDistribution(artifacts);
+       }
+
+       public Set<Artifact> resolveDistribution() {
                try {
                        Artifact pomArtifact = new DefaultArtifact(rootCoordinates);
-
-                       // {
-                       // DependencyNode node = aetherTemplate
-                       // .resolveDependencies(pomArtifact);
-                       //
-                       // PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
-                       // node.accept(nlg);
-                       //
-                       // for (Artifact artifact : nlg.getArtifacts(true)) {
-                       // log.debug(artifact);
-                       // }
-                       // AetherUtils.logDependencyNode(0, node);
-                       // }
-
                        Comparator<Artifact> artifactComparator = new Comparator<Artifact>() {
                                public int compare(Artifact o1, Artifact o2) {
                                        return o1.getArtifactId().compareTo(o2.getArtifactId());
@@ -93,8 +90,64 @@ public class ImportMavenDependencies implements Runnable {
                        distributionDescriptor.store(out, "");
                        log.debug(new String(out.toByteArray()));
                        out.close();
+
+                       return artifacts;
                } catch (Exception e) {
-                       throw new SlcException("Cannot resolve", e);
+                       throw new SlcException("Cannot resolve distribution", e);
+               }
+       }
+
+       protected void syncDistribution(Set<Artifact> artifacts) {
+               Long begin = System.currentTimeMillis();
+               try {
+                       JcrUtils.mkdirs(jcrSession, artifactBasePath);
+                       for (Artifact artifact : artifacts) {
+                               String parentPath = artifactBasePath + '/'
+                                               + artifactParentPath(artifact);
+                               Node parentNode;
+                               if (!jcrSession.itemExists(parentPath)) {
+                                       parentNode = JcrUtils.mkdirs(jcrSession, parentPath,
+                                                       NodeType.NT_FOLDER, false);
+                               } else {
+                                       parentNode = jcrSession.getNode(parentPath);
+                               }
+
+                               File file = artifact.getFile();
+                               Node fileNode;
+                               if (!parentNode.hasNode(file.getName())) {
+                                       fileNode = createFileNode(parentNode, file);
+                               } else {
+                                       fileNode = parentNode.getNode(file.getName());
+                               }
+                       }
+
+                       Long duration = (System.currentTimeMillis() - begin) / 1000;
+                       if (log.isDebugEnabled())
+                               log.debug("Synchronized distribution in " + duration + "s");
+               } catch (Exception e) {
+                       throw new SlcException("Cannot synchronize distribution", e);
+               }
+       }
+
+       protected String artifactParentPath(Artifact artifact) {
+               return artifact.getGroupId().replace('.', '/') + '/'
+                               + artifact.getArtifactId() + '/' + artifact.getVersion();
+       }
+
+       protected Node createFileNode(Node parentNode, File file) {
+               try {
+                       Node fileNode = parentNode
+                                       .addNode(file.getName(), NodeType.NT_FILE);
+                       Node contentNode = fileNode.addNode(Node.JCR_CONTENT,
+                                       NodeType.NT_RESOURCE);
+                       Binary binary = jcrSession.getValueFactory().createBinary(
+                                       new FileInputStream(file));
+                       contentNode.setProperty(Property.JCR_DATA, binary);
+                       binary.dispose();
+                       return fileNode;
+               } catch (Exception e) {
+                       throw new SlcException("Cannot create file node based on " + file
+                                       + " under " + parentNode, e);
                }
        }
 
@@ -197,4 +250,8 @@ public class ImportMavenDependencies implements Runnable {
                this.rootCoordinates = rootCoordinates;
        }
 
+       public void setJcrSession(Session jcrSession) {
+               this.jcrSession = jcrSession;
+       }
+
 }