]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/DistributionWrapper.java
Improve SLC Build framework
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / osgi / DistributionWrapper.java
1 package org.argeo.slc.repo.osgi;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.Properties;
8 import java.util.zip.ZipEntry;
9 import java.util.zip.ZipInputStream;
10
11 import javax.jcr.Node;
12 import javax.jcr.Property;
13 import javax.jcr.Session;
14
15 import org.apache.commons.io.IOUtils;
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.argeo.jcr.JcrUtils;
19 import org.argeo.slc.SlcException;
20 import org.argeo.slc.repo.OsgiFactory;
21 import org.argeo.slc.repo.RepoUtils;
22 import org.sonatype.aether.artifact.Artifact;
23 import org.sonatype.aether.util.artifact.DefaultArtifact;
24
25 /** Download a software distribution and generates the related OSGi bundles. */
26 public class DistributionWrapper implements Runnable {
27 private final static Log log = LogFactory.getLog(DistributionWrapper.class);
28
29 private OsgiFactory osgiFactory;
30
31 private String version;
32 private String groupId;
33
34 private String uri;
35
36 private Map<String, BndWrapper> wrappers = new HashMap<String, BndWrapper>();
37
38 public void run() {
39
40 Session distSession = null;
41 Session javaSession = null;
42 ZipInputStream zin = null;
43 try {
44 javaSession = osgiFactory.openJavaSession();
45 distSession = osgiFactory.openDistSession();
46
47 if (log.isDebugEnabled())
48 log.debug("Wrapping " + uri);
49
50 Node distNode = osgiFactory.getDist(distSession, uri);
51 zin = new ZipInputStream(distNode.getNode(Node.JCR_CONTENT)
52 .getProperty(Property.JCR_DATA).getBinary().getStream());
53
54 ZipEntry zentry = null;
55 ByteArrayOutputStream out = null;
56 ByteArrayInputStream in = null;
57 while ((zentry = zin.getNextEntry()) != null) {
58 try {
59 String name = zentry.getName();
60 // if (log.isDebugEnabled())
61 // log.debug("Scanning " + name);
62 if (wrappers.containsKey(name)) {
63
64 BndWrapper wrapper = (BndWrapper) wrappers.get(name);
65 if (wrapper.getVersion() == null)
66 wrapper.setVersion(version);// FIXME stateful?
67
68 out = new ByteArrayOutputStream((int) zentry.getSize());
69 // we must copy since the stream is closed by BND
70 byte[] sourceJarBytes = IOUtils.toByteArray(zin);
71 in = new ByteArrayInputStream(sourceJarBytes);
72 Properties properties = new Properties();
73 wrapper.wrapJar(properties, in, out);
74
75 Artifact newArtifact = new DefaultArtifact(groupId,
76 wrapper.getBsn(), "jar", wrapper.getVersion());
77 Node newJarNode = RepoUtils.copyBytesAsArtifact(
78 javaSession.getRootNode(), newArtifact,
79 out.toByteArray());
80 osgiFactory.indexNode(newJarNode);
81 if (log.isDebugEnabled())
82 log.debug("Wrapped " + name + " to "
83 + newJarNode.getPath());
84 }
85 } finally {
86 IOUtils.closeQuietly(out);
87 IOUtils.closeQuietly(in);
88 }
89 }
90 } catch (Exception e) {
91 throw new SlcException("Cannot wrap distribution " + uri, e);
92 } finally {
93 IOUtils.closeQuietly(zin);
94 JcrUtils.logoutQuietly(distSession);
95 JcrUtils.logoutQuietly(javaSession);
96 }
97 }
98
99 public void setVersion(String version) {
100 this.version = version;
101 }
102
103 public void setUri(String uri) {
104 this.uri = uri;
105 }
106
107 public void setWrappers(Map<String, BndWrapper> wrappers) {
108 this.wrappers = wrappers;
109 }
110
111 public void setGroupId(String groupId) {
112 this.groupId = groupId;
113 }
114
115 public void setOsgiFactory(OsgiFactory osgiFactory) {
116 this.osgiFactory = osgiFactory;
117 }
118
119 }