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