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