1 package org
.argeo
.slc
.repo
.osgi
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.ByteArrayOutputStream
;
5 import java
.io
.InputStream
;
7 import java
.util
.zip
.ZipOutputStream
;
10 import javax
.jcr
.Property
;
11 import javax
.jcr
.Session
;
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
;
24 import aQute
.lib
.osgi
.Jar
;
26 public class UriWrapper
extends BndWrapper
implements Runnable
{
27 private final static Log log
= LogFactory
.getLog(UriWrapper
.class);
30 private String baseUri
;
31 private String versionSeparator
= "-";
32 private String extension
= "jar";
34 private OsgiFactory osgiFactory
;
36 private SourcesProvider sourcesProvider
;
43 Session distSession
= null;
44 Session javaSession
= null;
46 ByteArrayOutputStream out
;
49 distSession
= osgiFactory
.openDistSession();
50 javaSession
= osgiFactory
.openJavaSession();
52 uri
= baseUri
+ '/' + getName() + versionSeparator
53 + getVersion() + "." + extension
;
55 Node sourceArtifact
= osgiFactory
.getDist(distSession
, uri
);
57 // TODO factorize with Maven
58 in
= sourceArtifact
.getNode(Node
.JCR_CONTENT
)
59 .getProperty(Property
.JCR_DATA
).getBinary().getStream();
62 out
= new ByteArrayOutputStream();
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());
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();
79 IOUtils
.closeQuietly(out
);
80 out
= new ByteArrayOutputStream();
82 .writeSources(packages
, new ZipOutputStream(out
));
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();
95 if (log
.isDebugEnabled())
96 log
.debug("Added sources " + sourcesArtifact
97 + " for bundle " + getArtifact());
99 } catch (Exception e
) {
100 throw new SlcException("Cannot wrap URI " + uri
, e
);
102 JcrUtils
.logoutQuietly(distSession
);
103 JcrUtils
.logoutQuietly(javaSession
);
109 public void setUri(String sourceCoords
) {
110 this.uri
= sourceCoords
;
113 public void setOsgiFactory(OsgiFactory osgiFactory
) {
114 this.osgiFactory
= osgiFactory
;
117 public void setBaseUri(String baseUri
) {
118 this.baseUri
= baseUri
;
121 public void setVersionSeparator(String versionSeparator
) {
122 this.versionSeparator
= versionSeparator
;
125 public void setSourcesProvider(SourcesProvider sourcesProvider
) {
126 this.sourcesProvider
= sourcesProvider
;