]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/IndexDistribution.java
Put RAP upload and map widgets to org.argeo.tp.rap
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / maven / IndexDistribution.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.repo.maven;
17
18 import java.io.File;
19 import java.util.HashSet;
20 import java.util.Set;
21
22 import javax.jcr.Node;
23 import javax.jcr.Repository;
24 import javax.jcr.RepositoryException;
25 import javax.jcr.Session;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.argeo.jcr.JcrUtils;
30 import org.argeo.slc.SlcException;
31 import org.argeo.slc.jcr.SlcNames;
32 import org.argeo.slc.jcr.SlcTypes;
33 import org.argeo.slc.repo.RepoConstants;
34 import org.sonatype.aether.artifact.Artifact;
35
36 /** Create a distribution node from a set of artifacts */
37 public class IndexDistribution implements Runnable {
38 private final static Log log = LogFactory.getLog(IndexDistribution.class);
39 private Repository repository;
40 private String workspace;
41
42 private String artifactBasePath = RepoConstants.ARTIFACTS_BASE_PATH;
43 private String distributionsBasePath = RepoConstants.DISTRIBUTIONS_BASE_PATH;
44 private String distributionName;
45
46 public void run() {
47 // TODO populate
48 Set<Artifact> artifacts = new HashSet<Artifact>();
49
50 // sync
51 Session session = null;
52 try {
53 session = repository.login(workspace);
54 syncDistribution(session, artifacts);
55 } catch (Exception e) {
56 throw new SlcException("Cannot import distribution", e);
57 } finally {
58 JcrUtils.logoutQuietly(session);
59 }
60 }
61
62 protected void syncDistribution(Session jcrSession, Set<Artifact> artifacts) {
63 Long begin = System.currentTimeMillis();
64 try {
65 JcrUtils.mkdirs(jcrSession, distributionsBasePath + '/'
66 + distributionName);
67 artifacts: for (Artifact artifact : artifacts) {
68 File file = artifact.getFile();
69 if (file == null) {
70 file = MavenConventionsUtils.artifactToFile(artifact);
71 if (!file.exists()) {
72 log.warn("Generated file " + file + " for " + artifact
73 + " does not exist");
74 continue artifacts;
75 }
76 }
77
78 try {
79 String parentPath = artifactBasePath
80 + (artifactBasePath.endsWith("/") ? "" : "/")
81 + artifactParentPath(artifact);
82 Node parentNode = jcrSession.getNode(parentPath);
83 Node fileNode = parentNode.getNode(file.getName());
84
85 if (fileNode.hasProperty(SlcNames.SLC_SYMBOLIC_NAME)) {
86 String distPath = bundleDistributionPath(fileNode);
87 if (!jcrSession.itemExists(distPath)
88 && fileNode
89 .isNodeType(SlcTypes.SLC_BUNDLE_ARTIFACT))
90 jcrSession.getWorkspace().clone(
91 jcrSession.getWorkspace().getName(),
92 fileNode.getPath(), distPath, false);
93 if (log.isDebugEnabled())
94 log.debug("Indexed " + fileNode);
95 }
96 } catch (Exception e) {
97 log.error("Could not index " + artifact, e);
98 jcrSession.refresh(false);
99 throw e;
100 }
101 }
102
103 Long duration = (System.currentTimeMillis() - begin) / 1000;
104 if (log.isDebugEnabled())
105 log.debug("Indexed distribution in " + duration + "s");
106 } catch (Exception e) {
107 throw new SlcException("Cannot synchronize distribution", e);
108 }
109 }
110
111 private String artifactParentPath(Artifact artifact) {
112 return artifact.getGroupId().replace('.', '/') + '/'
113 + artifact.getArtifactId() + '/' + artifact.getVersion();
114 }
115
116 private String bundleDistributionPath(Node fileNode) {
117 try {
118 return distributionsBasePath
119 + '/'
120 + distributionName
121 + '/'
122 + fileNode.getProperty(SlcNames.SLC_SYMBOLIC_NAME)
123 .getString()
124 + '_'
125 + fileNode.getProperty(SlcNames.SLC_BUNDLE_VERSION)
126 .getString();
127 } catch (RepositoryException e) {
128 throw new SlcException("Cannot create distribution path for "
129 + fileNode, e);
130 }
131 }
132
133 public void setDistributionName(String distributionName) {
134 this.distributionName = distributionName;
135 }
136
137 public void setRepository(Repository repository) {
138 this.repository = repository;
139 }
140
141 public void setWorkspace(String workspace) {
142 this.workspace = workspace;
143 }
144
145 }