1 package org
.argeo
.slc
.repo
.osgi
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.ByteArrayOutputStream
;
5 import java
.util
.ArrayList
;
6 import java
.util
.HashMap
;
7 import java
.util
.Iterator
;
11 import java
.util
.TreeSet
;
12 import java
.util
.zip
.ZipEntry
;
13 import java
.util
.zip
.ZipInputStream
;
15 import javax
.jcr
.Node
;
16 import javax
.jcr
.Property
;
17 import javax
.jcr
.RepositoryException
;
18 import javax
.jcr
.Session
;
20 import org
.apache
.commons
.io
.IOUtils
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.argeo
.jcr
.JcrUtils
;
24 import org
.argeo
.slc
.ModuleSet
;
25 import org
.argeo
.slc
.NameVersion
;
26 import org
.argeo
.slc
.SlcException
;
27 import org
.argeo
.slc
.aether
.ArtifactIdComparator
;
28 import org
.argeo
.slc
.build
.Distribution
;
29 import org
.argeo
.slc
.repo
.OsgiFactory
;
30 import org
.argeo
.slc
.repo
.RepoUtils
;
31 import org
.sonatype
.aether
.artifact
.Artifact
;
32 import org
.sonatype
.aether
.util
.artifact
.DefaultArtifact
;
33 import org
.springframework
.util
.AntPathMatcher
;
34 import org
.springframework
.util
.PathMatcher
;
37 * Download a software distribution and generates the related OSGi bundles from
38 * the jars, or import them directly if they are already OSGi bundles and don't
39 * need further modification.
41 public class ArchiveWrapper
implements Runnable
, ModuleSet
, Distribution
{
42 private final static Log log
= LogFactory
.getLog(ArchiveWrapper
.class);
44 private OsgiFactory osgiFactory
;
45 private String version
;
49 // jars to wrap as OSGi bundles
50 private Map
<String
, BndWrapper
> wrappers
= new HashMap
<String
, BndWrapper
>();
52 // pattern of OSGi bundles to import
53 private PathMatcher pathMatcher
= new AntPathMatcher();
54 private Map
<String
, String
> includes
= new HashMap
<String
, String
>();
55 private List
<String
> excludes
= new ArrayList
<String
>();
57 private Boolean mavenGroupIndexes
= false;
61 for (BndWrapper wrapper
: wrappers
.values()) {
62 if (wrapper
.getVersion() == null)
63 wrapper
.setVersion(version
);
67 public void destroy() {
71 public String
getDistributionId() {
75 public Iterator
<?
extends NameVersion
> nameVersions() {
76 return wrappers
.values().iterator();
80 if (mavenGroupIndexes
&& (version
== null))
81 throw new SlcException(
82 "'mavenGroupIndexes' requires 'version' to be set");
84 Map
<String
, Set
<Artifact
>> binaries
= new HashMap
<String
, Set
<Artifact
>>();
85 Map
<String
, Set
<Artifact
>> sources
= new HashMap
<String
, Set
<Artifact
>>();
87 Session distSession
= null;
88 Session javaSession
= null;
89 ZipInputStream zin
= null;
91 javaSession
= osgiFactory
.openJavaSession();
92 distSession
= osgiFactory
.openDistSession();
94 if (log
.isDebugEnabled())
95 log
.debug("Wrapping " + uri
);
97 Node distNode
= osgiFactory
.getDist(distSession
, uri
);
98 zin
= new ZipInputStream(distNode
.getNode(Node
.JCR_CONTENT
)
99 .getProperty(Property
.JCR_DATA
).getBinary().getStream());
101 ZipEntry zentry
= null;
102 entries
: while ((zentry
= zin
.getNextEntry()) != null) {
103 String name
= zentry
.getName();
104 if (wrappers
.containsKey(name
)) {
105 BndWrapper wrapper
= (BndWrapper
) wrappers
.get(name
);
106 // we must copy since the stream is closed by BND
107 byte[] sourceJarBytes
= IOUtils
.toByteArray(zin
);
108 Artifact artifact
= wrapZipEntry(javaSession
, zentry
,
109 sourceJarBytes
, wrapper
);
110 addArtifactToIndex(binaries
, wrapper
.getGroupId(), artifact
);
112 for (String wrapperKey
: wrappers
.keySet())
113 if (pathMatcher
.match(wrapperKey
, name
)) {
114 // first matched is taken
115 BndWrapper wrapper
= (BndWrapper
) wrappers
117 // we must copy since the stream is closed by BND
118 byte[] sourceJarBytes
= IOUtils
.toByteArray(zin
);
119 Artifact artifact
= wrapZipEntry(javaSession
,
120 zentry
, sourceJarBytes
, wrapper
);
121 addArtifactToIndex(binaries
, wrapper
.getGroupId(),
125 if (log
.isTraceEnabled())
126 log
.trace(name
+ " not matched by "
130 for (String exclude
: excludes
)
131 if (pathMatcher
.match(exclude
, name
))
134 for (String include
: includes
.keySet()) {
135 if (pathMatcher
.match(include
, name
)) {
136 String groupId
= includes
.get(include
);
137 byte[] sourceJarBytes
= IOUtils
.toByteArray(zin
);
138 Artifact artifact
= importZipEntry(javaSession
,
139 zentry
, sourceJarBytes
, groupId
);
140 if (artifact
.getArtifactId().endsWith(".source"))
141 addArtifactToIndex(sources
, groupId
, artifact
);
143 addArtifactToIndex(binaries
, groupId
, artifact
);
150 if (mavenGroupIndexes
&& version
!= null) {
151 for (String groupId
: binaries
.keySet()) {
152 RepoUtils
.writeGroupIndexes(javaSession
, "/", groupId
,
153 version
, binaries
.get(groupId
),
154 sources
.containsKey(groupId
) ? sources
.get(groupId
)
159 } catch (Exception e
) {
160 throw new SlcException("Cannot wrap distribution " + uri
, e
);
162 IOUtils
.closeQuietly(zin
);
163 JcrUtils
.logoutQuietly(distSession
);
164 JcrUtils
.logoutQuietly(javaSession
);
168 protected Artifact
wrapZipEntry(Session javaSession
, ZipEntry zentry
,
169 byte[] sourceJarBytes
, BndWrapper wrapper
)
170 throws RepositoryException
{
171 ByteArrayOutputStream out
= null;
172 ByteArrayInputStream in
= null;
175 out
= new ByteArrayOutputStream((int) zentry
.getSize());
176 in
= new ByteArrayInputStream(sourceJarBytes
);
177 wrapper
.wrapJar(in
, out
);
179 Artifact artifact
= wrapper
.getArtifact();
180 newJarNode
= RepoUtils
.copyBytesAsArtifact(
181 javaSession
.getRootNode(), artifact
, out
.toByteArray());
182 osgiFactory
.indexNode(newJarNode
);
183 newJarNode
.getSession().save();
184 if (log
.isDebugEnabled())
185 log
.debug("Wrapped jar " + zentry
.getName() + " to "
186 + newJarNode
.getPath());
189 IOUtils
.closeQuietly(in
);
190 IOUtils
.closeQuietly(out
);
194 protected Artifact
importZipEntry(Session javaSession
, ZipEntry zentry
,
195 byte[] sourceJarBytes
, String groupId
) throws RepositoryException
{
196 ByteArrayInputStream in
= null;
199 in
= new ByteArrayInputStream(sourceJarBytes
);
200 NameVersion nameVersion
= RepoUtils
.readNameVersion(in
);
201 Artifact artifact
= new DefaultArtifact(groupId
,
202 nameVersion
.getName(), "jar", nameVersion
.getVersion());
203 newJarNode
= RepoUtils
.copyBytesAsArtifact(
204 javaSession
.getRootNode(), artifact
, sourceJarBytes
);
205 osgiFactory
.indexNode(newJarNode
);
206 newJarNode
.getSession().save();
207 if (log
.isDebugEnabled())
208 log
.debug("Imported OSGi bundle " + zentry
.getName() + " to "
209 + newJarNode
.getPath());
212 IOUtils
.closeQuietly(in
);
216 private void addArtifactToIndex(Map
<String
, Set
<Artifact
>> index
,
217 String groupId
, Artifact artifact
) {
218 if (!index
.containsKey(groupId
))
220 new TreeSet
<Artifact
>(new ArtifactIdComparator()));
221 index
.get(groupId
).add(artifact
);
224 public void setUri(String uri
) {
228 public void setWrappers(Map
<String
, BndWrapper
> wrappers
) {
229 this.wrappers
= wrappers
;
232 public void setOsgiFactory(OsgiFactory osgiFactory
) {
233 this.osgiFactory
= osgiFactory
;
236 public void setVersion(String version
) {
237 this.version
= version
;
240 public void setPathMatcher(PathMatcher pathMatcher
) {
241 this.pathMatcher
= pathMatcher
;
244 public void setIncludes(Map
<String
, String
> includes
) {
245 this.includes
= includes
;
248 public void setExcludes(List
<String
> excludes
) {
249 this.excludes
= excludes
;
252 public void setMavenGroupIndexes(Boolean mavenGroupIndexes
) {
253 this.mavenGroupIndexes
= mavenGroupIndexes
;