]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ArchiveWrapper.java
Work on the binaries indexer.
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / osgi / ArchiveWrapper.java
1 package org.argeo.slc.repo.osgi;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Set;
11 import java.util.TreeSet;
12 import java.util.zip.ZipEntry;
13 import java.util.zip.ZipInputStream;
14
15 import javax.jcr.Node;
16 import javax.jcr.Property;
17 import javax.jcr.RepositoryException;
18 import javax.jcr.Session;
19
20 import org.apache.commons.io.IOUtils;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.argeo.jcr.JcrUtils;
24 import org.argeo.slc.ModuleSet;
25 import org.argeo.slc.NameVersion;
26 import org.argeo.slc.SlcException;
27 import org.argeo.slc.aether.ArtifactIdComparator;
28 import org.argeo.slc.build.Distribution;
29 import org.argeo.slc.repo.OsgiFactory;
30 import org.argeo.slc.repo.RepoUtils;
31 import org.sonatype.aether.artifact.Artifact;
32 import org.sonatype.aether.util.artifact.DefaultArtifact;
33 import org.springframework.util.AntPathMatcher;
34 import org.springframework.util.PathMatcher;
35
36 /**
37 * Download a software distribution and generates the related OSGi bundles from
38 * the jars, or import them directly if they are already OSGi bundles and don't
39 * need further modification.
40 */
41 public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
42 private final static Log log = LogFactory.getLog(ArchiveWrapper.class);
43
44 private OsgiFactory osgiFactory;
45 private String version;
46
47 private String uri;
48
49 // jars to wrap as OSGi bundles
50 private Map<String, BndWrapper> wrappers = new HashMap<String, BndWrapper>();
51
52 // pattern of OSGi bundles to import
53 private PathMatcher pathMatcher = new AntPathMatcher();
54 private Map<String, String> includes = new HashMap<String, String>();
55 private List<String> excludes = new ArrayList<String>();
56
57 private Boolean mavenGroupIndexes = false;
58
59 public void init() {
60 if (version != null)
61 for (BndWrapper wrapper : wrappers.values()) {
62 if (wrapper.getVersion() == null)
63 wrapper.setVersion(version);
64 }
65 }
66
67 public void destroy() {
68
69 }
70
71 public String getDistributionId() {
72 return uri;
73 }
74
75 public Iterator<? extends NameVersion> nameVersions() {
76 return wrappers.values().iterator();
77 }
78
79 public void run() {
80 if (mavenGroupIndexes && (version == null))
81 throw new SlcException(
82 "'mavenGroupIndexes' requires 'version' to be set");
83
84 Map<String, Set<Artifact>> binaries = new HashMap<String, Set<Artifact>>();
85 Map<String, Set<Artifact>> sources = new HashMap<String, Set<Artifact>>();
86
87 Session distSession = null;
88 Session javaSession = null;
89 ZipInputStream zin = null;
90 try {
91 javaSession = osgiFactory.openJavaSession();
92 distSession = osgiFactory.openDistSession();
93
94 if (log.isDebugEnabled())
95 log.debug("Wrapping " + uri);
96
97 Node distNode = osgiFactory.getDist(distSession, uri);
98 zin = new ZipInputStream(distNode.getNode(Node.JCR_CONTENT)
99 .getProperty(Property.JCR_DATA).getBinary().getStream());
100
101 ZipEntry zentry = null;
102 entries: while ((zentry = zin.getNextEntry()) != null) {
103 String name = zentry.getName();
104 if (wrappers.containsKey(name)) {
105 BndWrapper wrapper = (BndWrapper) wrappers.get(name);
106 // we must copy since the stream is closed by BND
107 byte[] sourceJarBytes = IOUtils.toByteArray(zin);
108 Artifact artifact = wrapZipEntry(javaSession, zentry,
109 sourceJarBytes, wrapper);
110 addArtifactToIndex(binaries, wrapper.getGroupId(), artifact);
111 } else {
112 for (String wrapperKey : wrappers.keySet())
113 if (pathMatcher.match(wrapperKey, name)) {
114 // first matched is taken
115 BndWrapper wrapper = (BndWrapper) wrappers
116 .get(wrapperKey);
117 // we must copy since the stream is closed by BND
118 byte[] sourceJarBytes = IOUtils.toByteArray(zin);
119 Artifact artifact = wrapZipEntry(javaSession,
120 zentry, sourceJarBytes, wrapper);
121 addArtifactToIndex(binaries, wrapper.getGroupId(),
122 artifact);
123 continue entries;
124 } else {
125 if (log.isTraceEnabled())
126 log.trace(name + " not matched by "
127 + wrapperKey);
128 }
129
130 for (String exclude : excludes)
131 if (pathMatcher.match(exclude, name))
132 continue entries;
133
134 for (String include : includes.keySet()) {
135 if (pathMatcher.match(include, name)) {
136 String groupId = includes.get(include);
137 byte[] sourceJarBytes = IOUtils.toByteArray(zin);
138 Artifact artifact = importZipEntry(javaSession,
139 zentry, sourceJarBytes, groupId);
140 if (artifact.getArtifactId().endsWith(".source"))
141 addArtifactToIndex(sources, groupId, artifact);
142 else
143 addArtifactToIndex(binaries, groupId, artifact);
144 }
145 }
146 }
147 }
148
149 // indexes
150 if (mavenGroupIndexes && version != null) {
151 for (String groupId : binaries.keySet()) {
152 RepoUtils.writeGroupIndexes(javaSession, "/", groupId,
153 version, binaries.get(groupId),
154 sources.containsKey(groupId) ? sources.get(groupId)
155 : null);
156 }
157 }
158
159 } catch (Exception e) {
160 throw new SlcException("Cannot wrap distribution " + uri, e);
161 } finally {
162 IOUtils.closeQuietly(zin);
163 JcrUtils.logoutQuietly(distSession);
164 JcrUtils.logoutQuietly(javaSession);
165 }
166 }
167
168 protected Artifact wrapZipEntry(Session javaSession, ZipEntry zentry,
169 byte[] sourceJarBytes, BndWrapper wrapper)
170 throws RepositoryException {
171 ByteArrayOutputStream out = null;
172 ByteArrayInputStream in = null;
173 Node newJarNode;
174 try {
175 out = new ByteArrayOutputStream((int) zentry.getSize());
176 in = new ByteArrayInputStream(sourceJarBytes);
177 wrapper.wrapJar(in, out);
178
179 Artifact artifact = wrapper.getArtifact();
180 newJarNode = RepoUtils.copyBytesAsArtifact(
181 javaSession.getRootNode(), artifact, out.toByteArray());
182 osgiFactory.indexNode(newJarNode);
183 newJarNode.getSession().save();
184 if (log.isDebugEnabled())
185 log.debug("Wrapped jar " + zentry.getName() + " to "
186 + newJarNode.getPath());
187 return artifact;
188 } finally {
189 IOUtils.closeQuietly(in);
190 IOUtils.closeQuietly(out);
191 }
192 }
193
194 protected Artifact importZipEntry(Session javaSession, ZipEntry zentry,
195 byte[] sourceJarBytes, String groupId) throws RepositoryException {
196 ByteArrayInputStream in = null;
197 Node newJarNode;
198 try {
199 in = new ByteArrayInputStream(sourceJarBytes);
200 NameVersion nameVersion = RepoUtils.readNameVersion(in);
201 Artifact artifact = new DefaultArtifact(groupId,
202 nameVersion.getName(), "jar", nameVersion.getVersion());
203 newJarNode = RepoUtils.copyBytesAsArtifact(
204 javaSession.getRootNode(), artifact, sourceJarBytes);
205 osgiFactory.indexNode(newJarNode);
206 newJarNode.getSession().save();
207 if (log.isDebugEnabled())
208 log.debug("Imported OSGi bundle " + zentry.getName() + " to "
209 + newJarNode.getPath());
210 return artifact;
211 } finally {
212 IOUtils.closeQuietly(in);
213 }
214 }
215
216 private void addArtifactToIndex(Map<String, Set<Artifact>> index,
217 String groupId, Artifact artifact) {
218 if (!index.containsKey(groupId))
219 index.put(groupId,
220 new TreeSet<Artifact>(new ArtifactIdComparator()));
221 index.get(groupId).add(artifact);
222 }
223
224 public void setUri(String uri) {
225 this.uri = uri;
226 }
227
228 public void setWrappers(Map<String, BndWrapper> wrappers) {
229 this.wrappers = wrappers;
230 }
231
232 public void setOsgiFactory(OsgiFactory osgiFactory) {
233 this.osgiFactory = osgiFactory;
234 }
235
236 public void setVersion(String version) {
237 this.version = version;
238 }
239
240 public void setPathMatcher(PathMatcher pathMatcher) {
241 this.pathMatcher = pathMatcher;
242 }
243
244 public void setIncludes(Map<String, String> includes) {
245 this.includes = includes;
246 }
247
248 public void setExcludes(List<String> excludes) {
249 this.excludes = excludes;
250 }
251
252 public void setMavenGroupIndexes(Boolean mavenGroupIndexes) {
253 this.mavenGroupIndexes = mavenGroupIndexes;
254 }
255
256 }