1 package org
.argeo
.slc
.repo
.osgi
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.ByteArrayOutputStream
;
5 import java
.util
.HashMap
;
7 import java
.util
.Properties
;
8 import java
.util
.zip
.ZipEntry
;
9 import java
.util
.zip
.ZipInputStream
;
11 import javax
.jcr
.Node
;
12 import javax
.jcr
.Property
;
13 import javax
.jcr
.Session
;
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
;
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);
29 private OsgiFactory osgiFactory
;
31 private String version
;
32 private String groupId
;
36 private Map
<String
, BndWrapper
> wrappers
= new HashMap
<String
, BndWrapper
>();
40 Session distSession
= null;
41 Session javaSession
= null;
42 ZipInputStream zin
= null;
44 javaSession
= osgiFactory
.openJavaSession();
45 distSession
= osgiFactory
.openDistSession();
47 if (log
.isDebugEnabled())
48 log
.debug("Wrapping " + uri
);
50 Node distNode
= osgiFactory
.getDist(distSession
, uri
);
51 zin
= new ZipInputStream(distNode
.getNode(Node
.JCR_CONTENT
)
52 .getProperty(Property
.JCR_DATA
).getBinary().getStream());
54 ZipEntry zentry
= null;
55 ByteArrayOutputStream out
= null;
56 ByteArrayInputStream in
= null;
57 while ((zentry
= zin
.getNextEntry()) != null) {
59 String name
= zentry
.getName();
60 // if (log.isDebugEnabled())
61 // log.debug("Scanning " + name);
62 if (wrappers
.containsKey(name
)) {
64 BndWrapper wrapper
= (BndWrapper
) wrappers
.get(name
);
65 if (wrapper
.getVersion() == null)
66 wrapper
.setVersion(version
);// FIXME stateful?
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
);
75 Artifact newArtifact
= new DefaultArtifact(groupId
,
76 wrapper
.getBsn(), "jar", wrapper
.getVersion());
77 Node newJarNode
= RepoUtils
.copyBytesAsArtifact(
78 javaSession
.getRootNode(), newArtifact
,
80 osgiFactory
.indexNode(newJarNode
);
81 if (log
.isDebugEnabled())
82 log
.debug("Wrapped " + name
+ " to "
83 + newJarNode
.getPath());
86 IOUtils
.closeQuietly(out
);
87 IOUtils
.closeQuietly(in
);
90 } catch (Exception e
) {
91 throw new SlcException("Cannot wrap distribution " + uri
, e
);
93 IOUtils
.closeQuietly(zin
);
94 JcrUtils
.logoutQuietly(distSession
);
95 JcrUtils
.logoutQuietly(javaSession
);
99 public void setVersion(String version
) {
100 this.version
= version
;
103 public void setUri(String uri
) {
107 public void setWrappers(Map
<String
, BndWrapper
> wrappers
) {
108 this.wrappers
= wrappers
;
111 public void setGroupId(String groupId
) {
112 this.groupId
= groupId
;
115 public void setOsgiFactory(OsgiFactory osgiFactory
) {
116 this.osgiFactory
= osgiFactory
;