]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/UriWrapper.java
Improve URI wrapper
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / osgi / UriWrapper.java
1 package org.argeo.slc.repo.osgi;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.InputStream;
6 import java.util.List;
7 import java.util.zip.ZipOutputStream;
8
9 import javax.jcr.Node;
10 import javax.jcr.Property;
11 import javax.jcr.Session;
12
13 import org.apache.commons.io.IOUtils;
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.argeo.jcr.JcrUtils;
17 import org.argeo.slc.DefaultNameVersion;
18 import org.argeo.slc.SlcException;
19 import org.argeo.slc.repo.OsgiFactory;
20 import org.argeo.slc.repo.RepoUtils;
21 import org.sonatype.aether.artifact.Artifact;
22 import org.sonatype.aether.util.artifact.DefaultArtifact;
23
24 import aQute.lib.osgi.Jar;
25
26 public class UriWrapper extends BndWrapper implements Runnable {
27 private final static Log log = LogFactory.getLog(UriWrapper.class);
28
29 private String uri;
30 private String baseUri;
31 private String versionSeparator = "-";
32 private String extension = "jar";
33
34 private OsgiFactory osgiFactory;
35
36 private SourcesProvider sourcesProvider;
37
38 public UriWrapper() {
39 setFactory(this);
40 }
41
42 public void run() {
43 Session distSession = null;
44 Session javaSession = null;
45 InputStream in;
46 ByteArrayOutputStream out;
47 Jar jar = null;
48 try {
49 distSession = osgiFactory.openDistSession();
50 javaSession = osgiFactory.openJavaSession();
51 if (uri == null) {
52 uri = baseUri + '/' + getName() + versionSeparator
53 + getVersion() + "." + extension;
54 }
55 Node sourceArtifact = osgiFactory.getDist(distSession, uri);
56
57 // TODO factorize with Maven
58 in = sourceArtifact.getNode(Node.JCR_CONTENT)
59 .getProperty(Property.JCR_DATA).getBinary().getStream();
60 out=null;
61
62 out = new ByteArrayOutputStream();
63 wrapJar(in, out);
64 Node newJarNode = RepoUtils
65 .copyBytesAsArtifact(javaSession.getRootNode(),
66 getArtifact(), out.toByteArray());
67 osgiFactory.indexNode(newJarNode);
68 newJarNode.getSession().save();
69 if (log.isDebugEnabled())
70 log.debug("Wrapped " + uri + " to " + newJarNode.getPath());
71
72 // sources
73 if (sourcesProvider != null) {
74 IOUtils.closeQuietly(in);
75 in = new ByteArrayInputStream(out.toByteArray());
76 jar = new Jar(null, in);
77 List<String> packages = jar.getPackages();
78
79 IOUtils.closeQuietly(out);
80 out = new ByteArrayOutputStream();
81 sourcesProvider
82 .writeSources(packages, new ZipOutputStream(out));
83
84 IOUtils.closeQuietly(in);
85 in = new ByteArrayInputStream(out.toByteArray());
86 byte[] sourcesJar = RepoUtils.packageAsPdeSource(in,
87 new DefaultNameVersion(this));
88 Artifact sourcesArtifact = new DefaultArtifact(getArtifact()
89 .getGroupId(), getArtifact().getArtifactId()
90 + ".source", "jar", getArtifact().getVersion());
91 Node sourcesJarNode = RepoUtils.copyBytesAsArtifact(
92 javaSession.getRootNode(), sourcesArtifact, sourcesJar);
93 sourcesJarNode.getSession().save();
94
95 if (log.isDebugEnabled())
96 log.debug("Added sources " + sourcesArtifact
97 + " for bundle " + getArtifact());
98 }
99 } catch (Exception e) {
100 throw new SlcException("Cannot wrap URI " + uri, e);
101 } finally {
102 JcrUtils.logoutQuietly(distSession);
103 JcrUtils.logoutQuietly(javaSession);
104 if (jar != null)
105 jar.close();
106 }
107 }
108
109 public void setUri(String sourceCoords) {
110 this.uri = sourceCoords;
111 }
112
113 public void setOsgiFactory(OsgiFactory osgiFactory) {
114 this.osgiFactory = osgiFactory;
115 }
116
117 public void setBaseUri(String baseUri) {
118 this.baseUri = baseUri;
119 }
120
121 public void setVersionSeparator(String versionSeparator) {
122 this.versionSeparator = versionSeparator;
123 }
124
125 public void setSourcesProvider(SourcesProvider sourcesProvider) {
126 this.sourcesProvider = sourcesProvider;
127 }
128 }