1 package org
.argeo
.slc
.repo
.osgi
;
3 import java
.io
.ByteArrayInputStream
;
5 import java
.util
.ArrayList
;
7 import java
.util
.jar
.JarInputStream
;
8 import java
.util
.jar
.Manifest
;
9 import java
.util
.zip
.ZipEntry
;
10 import java
.util
.zip
.ZipInputStream
;
12 import javax
.jcr
.Node
;
13 import javax
.jcr
.Repository
;
14 import javax
.jcr
.Session
;
16 import org
.apache
.commons
.io
.IOUtils
;
17 import org
.apache
.commons
.logging
.Log
;
18 import org
.apache
.commons
.logging
.LogFactory
;
19 import org
.argeo
.jcr
.JcrUtils
;
20 import org
.argeo
.slc
.NameVersion
;
21 import org
.argeo
.slc
.SlcException
;
22 import org
.argeo
.slc
.repo
.ArtifactIndexer
;
23 import org
.argeo
.slc
.repo
.JarFileIndexer
;
24 import org
.argeo
.slc
.repo
.RepoUtils
;
25 import org
.sonatype
.aether
.artifact
.Artifact
;
26 import org
.sonatype
.aether
.util
.artifact
.DefaultArtifact
;
29 * Import all bundles in a zip file (typically an Eclipse distribution) into the
32 public class ImportBundlesZip
implements Runnable
{
33 private final static Log log
= LogFactory
.getLog(ImportBundlesZip
.class);
34 private Repository repository
;
35 private String workspace
;
36 private String groupId
;
37 private String artifactBasePath
= "/";
39 private ArtifactIndexer artifactIndexer
= new ArtifactIndexer();
40 private JarFileIndexer jarFileIndexer
= new JarFileIndexer();
42 private String zipFile
;
44 private List
<String
> excludedBundles
= new ArrayList
<String
>();
47 ZipInputStream zipIn
= null;
48 JarInputStream jarIn
= null;
49 Session session
= null;
51 URL url
= new URL(zipFile
);
52 session
= repository
.login(workspace
);
55 // String groupPath = MavenConventionsUtils.groupPath(
56 // artifactBasePath, groupId);
57 // if (session.itemExists(groupPath)) {
58 // session.getNode(groupPath).remove();
60 // if (log.isDebugEnabled())
61 // log.debug("Cleared " + groupPath);
64 zipIn
= new ZipInputStream(url
.openStream());
65 ZipEntry zipEntry
= null;
66 entries
: while ((zipEntry
= zipIn
.getNextEntry()) != null) {
67 String entryName
= zipEntry
.getName();
68 if (!entryName
.endsWith(".jar")
69 || entryName
.contains("feature"))
70 continue entries
;// skip
71 byte[] jarBytes
= IOUtils
.toByteArray(zipIn
);
73 jarIn
= new JarInputStream(new ByteArrayInputStream(jarBytes
));
74 Manifest manifest
= jarIn
.getManifest();
75 IOUtils
.closeQuietly(jarIn
);
76 if (manifest
== null) {
77 log
.warn(entryName
+ " has no MANIFEST");
82 nv
= RepoUtils
.readNameVersion(manifest
);
83 } catch (Exception e
) {
84 log
.warn("Cannot read name version from " + entryName
, e
);
88 String bundleName
= RepoUtils
.extractBundleNameFromSourceName(nv
90 // skip excluded bundles and their sources
91 if (excludedBundles
.contains(bundleName
))
93 // for(String excludedBundle:excludedBundles){
94 // if(bundleName.contains(excludedBundle))
98 Artifact artifact
= new DefaultArtifact(groupId
, nv
.getName(),
99 "jar", nv
.getVersion());
100 Node artifactNode
= RepoUtils
.copyBytesAsArtifact(
101 session
.getNode(artifactBasePath
), artifact
, jarBytes
);
102 jarBytes
= null;// superstition, in order to free memory
105 artifactIndexer
.index(artifactNode
);
106 jarFileIndexer
.index(artifactNode
);
108 if (log
.isDebugEnabled())
109 log
.debug("Imported " + entryName
+ " to " + artifactNode
);
111 } catch (Exception e
) {
112 throw new SlcException("Cannot import zip " + zipFile
+ " to "
115 IOUtils
.closeQuietly(zipIn
);
116 IOUtils
.closeQuietly(jarIn
);
117 JcrUtils
.logoutQuietly(session
);
122 public void setRepository(Repository repository
) {
123 this.repository
= repository
;
126 public void setWorkspace(String workspace
) {
127 this.workspace
= workspace
;
130 public void setGroupId(String groupId
) {
131 this.groupId
= groupId
;
134 public void setArtifactBasePath(String artifactBasePath
) {
135 this.artifactBasePath
= artifactBasePath
;
138 public void setZipFile(String zipFile
) {
139 this.zipFile
= zipFile
;
142 public void setExcludedBundles(List
<String
> excludedBundles
) {
143 this.excludedBundles
= excludedBundles
;