X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.repo%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Frepo%2Fmaven%2FImportMavenDependencies.java;h=c4020d92f344c522d8227cc244e8f5718ff3f5ee;hb=9485fdca99c408ae16fd077577973b80b7dde81f;hp=8aff3de08f21beba8d54e10cccd87097bde7c841;hpb=6a0f4cf1c0a1b49004b8c3dbc075f7202f7536f1;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/ImportMavenDependencies.java b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/ImportMavenDependencies.java index 8aff3de08..c4020d92f 100644 --- a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/ImportMavenDependencies.java +++ b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/ImportMavenDependencies.java @@ -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 excludedArtifacts = new HashSet(); + private Session jcrSession; + private String artifactBasePath = "/slc/repo/artifacts"; + public void run() { + Set artifacts = resolveDistribution(); + syncDistribution(artifacts); + } + + public Set 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 artifactComparator = new Comparator() { 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 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; + } + }