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
.eclipse
.aether
.artifact
.Artifact
;
22 import org
.eclipse
.aether
.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;
45 InputStream in
= null;
46 ByteArrayOutputStream out
= null;
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();
60 out
= new ByteArrayOutputStream();
62 Node newJarNode
= RepoUtils
63 .copyBytesAsArtifact(javaSession
.getRootNode(),
64 getArtifact(), out
.toByteArray());
65 osgiFactory
.indexNode(newJarNode
);
66 newJarNode
.getSession().save();
67 if (log
.isDebugEnabled())
68 log
.debug("Wrapped " + uri
+ " to " + newJarNode
.getPath());
71 if (sourcesProvider
!= null) {
72 IOUtils
.closeQuietly(in
);
73 in
= new ByteArrayInputStream(out
.toByteArray());
74 jar
= new Jar(null, in
);
75 List
<String
> packages
= jar
.getPackages();
77 IOUtils
.closeQuietly(out
);
78 out
= new ByteArrayOutputStream();
80 .writeSources(packages
, new ZipOutputStream(out
));
82 IOUtils
.closeQuietly(in
);
83 in
= new ByteArrayInputStream(out
.toByteArray());
84 byte[] sourcesJar
= RepoUtils
.packageAsPdeSource(in
,
85 new DefaultNameVersion(this));
86 Artifact sourcesArtifact
= new DefaultArtifact(getArtifact()
87 .getGroupId(), getArtifact().getArtifactId()
88 + ".source", "jar", getArtifact().getVersion());
89 Node sourcesJarNode
= RepoUtils
.copyBytesAsArtifact(
90 javaSession
.getRootNode(), sourcesArtifact
, sourcesJar
);
91 sourcesJarNode
.getSession().save();
93 if (log
.isDebugEnabled())
94 log
.debug("Added sources " + sourcesArtifact
95 + " for bundle " + getArtifact());
97 } catch (Exception e
) {
98 throw new SlcException("Cannot wrap URI " + uri
, e
);
100 IOUtils
.closeQuietly(in
);
101 IOUtils
.closeQuietly(out
);
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
;