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;
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());
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);
}
}
this.rootCoordinates = rootCoordinates;
}
+ public void setJcrSession(Session jcrSession) {
+ this.jcrSession = jcrSession;
+ }
+
}