]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.repo/src/org/argeo/slc/repo/maven/IndexDistribution.java
Merge remote-tracking branch 'origin/master' into testing
[gpl/argeo-slc.git] / org.argeo.slc.repo / src / org / argeo / slc / repo / maven / IndexDistribution.java
1 package org.argeo.slc.repo.maven;
2
3 import java.io.File;
4 import java.util.HashSet;
5 import java.util.Set;
6
7 import javax.jcr.Node;
8 import javax.jcr.Repository;
9 import javax.jcr.RepositoryException;
10 import javax.jcr.Session;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.argeo.jcr.JcrUtils;
15 import org.argeo.slc.SlcException;
16 import org.argeo.slc.SlcNames;
17 import org.argeo.slc.SlcTypes;
18 import org.argeo.slc.repo.RepoConstants;
19 import org.eclipse.aether.artifact.Artifact;
20
21 /** Create a distribution node from a set of artifacts */
22 public class IndexDistribution implements Runnable {
23 private final static Log log = LogFactory.getLog(IndexDistribution.class);
24 private Repository repository;
25 private String workspace;
26
27 private String artifactBasePath = RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH;
28 private String distributionsBasePath = RepoConstants.DISTRIBUTIONS_BASE_PATH;
29 private String distributionName;
30
31 public void run() {
32 // TODO populate
33 Set<Artifact> artifacts = new HashSet<Artifact>();
34
35 // sync
36 Session session = null;
37 try {
38 session = repository.login(workspace);
39 syncDistribution(session, artifacts);
40 } catch (Exception e) {
41 throw new SlcException("Cannot import distribution", e);
42 } finally {
43 JcrUtils.logoutQuietly(session);
44 }
45 }
46
47 protected void syncDistribution(Session jcrSession, Set<Artifact> artifacts) {
48 Long begin = System.currentTimeMillis();
49 try {
50 JcrUtils.mkdirs(jcrSession, distributionsBasePath + '/'
51 + distributionName);
52 artifacts: for (Artifact artifact : artifacts) {
53 File file = artifact.getFile();
54 if (file == null) {
55 file = MavenConventionsUtils.artifactToFile(artifact);
56 if (!file.exists()) {
57 log.warn("Generated file " + file + " for " + artifact
58 + " does not exist");
59 continue artifacts;
60 }
61 }
62
63 try {
64 String parentPath = artifactBasePath
65 + (artifactBasePath.endsWith("/") ? "" : "/")
66 + artifactParentPath(artifact);
67 Node parentNode = jcrSession.getNode(parentPath);
68 Node fileNode = parentNode.getNode(file.getName());
69
70 if (fileNode.hasProperty(SlcNames.SLC_SYMBOLIC_NAME)) {
71 String distPath = bundleDistributionPath(fileNode);
72 if (!jcrSession.itemExists(distPath)
73 && fileNode
74 .isNodeType(SlcTypes.SLC_BUNDLE_ARTIFACT))
75 jcrSession.getWorkspace().clone(
76 jcrSession.getWorkspace().getName(),
77 fileNode.getPath(), distPath, false);
78 if (log.isDebugEnabled())
79 log.debug("Indexed " + fileNode);
80 }
81 } catch (Exception e) {
82 log.error("Could not index " + artifact, e);
83 jcrSession.refresh(false);
84 throw e;
85 }
86 }
87
88 Long duration = (System.currentTimeMillis() - begin) / 1000;
89 if (log.isDebugEnabled())
90 log.debug("Indexed distribution in " + duration + "s");
91 } catch (Exception e) {
92 throw new SlcException("Cannot synchronize distribution", e);
93 }
94 }
95
96 private String artifactParentPath(Artifact artifact) {
97 return artifact.getGroupId().replace('.', '/') + '/'
98 + artifact.getArtifactId() + '/' + artifact.getVersion();
99 }
100
101 private String bundleDistributionPath(Node fileNode) {
102 try {
103 return distributionsBasePath
104 + '/'
105 + distributionName
106 + '/'
107 + fileNode.getProperty(SlcNames.SLC_SYMBOLIC_NAME)
108 .getString()
109 + '_'
110 + fileNode.getProperty(SlcNames.SLC_BUNDLE_VERSION)
111 .getString();
112 } catch (RepositoryException e) {
113 throw new SlcException("Cannot create distribution path for "
114 + fileNode, e);
115 }
116 }
117
118 public void setDistributionName(String distributionName) {
119 this.distributionName = distributionName;
120 }
121
122 public void setRepository(Repository repository) {
123 this.repository = repository;
124 }
125
126 public void setWorkspace(String workspace) {
127 this.workspace = workspace;
128 }
129
130 }