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
.eclipse
.aether
.artifact
.Artifact
;
26 import org
.eclipse
.aether
.artifact
.DefaultArtifact
;
29 * Import all bundles in a zip file (typically an Eclipse distribution) into the
32 * @deprecated Use {@link ArchiveWrapper} instead.
35 public class ImportBundlesZip
implements Runnable
{
36 private final static Log log
= LogFactory
.getLog(ImportBundlesZip
.class);
37 private Repository repository
;
38 private String workspace
;
39 private String groupId
;
40 private String artifactBasePath
= "/";
42 private ArtifactIndexer artifactIndexer
= new ArtifactIndexer();
43 private JarFileIndexer jarFileIndexer
= new JarFileIndexer();
45 private String zipFile
;
47 private List
<String
> excludedBundles
= new ArrayList
<String
>();
50 ZipInputStream zipIn
= null;
51 JarInputStream jarIn
= null;
52 Session session
= null;
54 URL url
= new URL(zipFile
);
55 session
= repository
.login(workspace
);
58 // String groupPath = MavenConventionsUtils.groupPath(
59 // artifactBasePath, groupId);
60 // if (session.itemExists(groupPath)) {
61 // session.getNode(groupPath).remove();
63 // if (log.isDebugEnabled())
64 // log.debug("Cleared " + groupPath);
67 zipIn
= new ZipInputStream(url
.openStream());
68 ZipEntry zipEntry
= null;
69 entries
: while ((zipEntry
= zipIn
.getNextEntry()) != null) {
70 String entryName
= zipEntry
.getName();
71 if (!entryName
.endsWith(".jar")
72 || entryName
.contains("feature"))
73 continue entries
;// skip
74 byte[] jarBytes
= IOUtils
.toByteArray(zipIn
);
76 jarIn
= new JarInputStream(new ByteArrayInputStream(jarBytes
));
77 Manifest manifest
= jarIn
.getManifest();
78 IOUtils
.closeQuietly(jarIn
);
79 if (manifest
== null) {
80 log
.warn(entryName
+ " has no MANIFEST");
85 nv
= RepoUtils
.readNameVersion(manifest
);
86 } catch (Exception e
) {
87 log
.warn("Cannot read name version from " + entryName
, e
);
91 String bundleName
= RepoUtils
92 .extractBundleNameFromSourceName(nv
.getName());
93 // skip excluded bundles and their sources
94 if (excludedBundles
.contains(bundleName
))
96 // for(String excludedBundle:excludedBundles){
97 // if(bundleName.contains(excludedBundle))
101 Artifact artifact
= new DefaultArtifact(groupId
, nv
.getName(),
102 "jar", nv
.getVersion());
103 Node artifactNode
= RepoUtils
.copyBytesAsArtifact(
104 session
.getNode(artifactBasePath
), artifact
, jarBytes
);
105 jarBytes
= null;// superstition, in order to free memory
108 artifactIndexer
.index(artifactNode
);
109 jarFileIndexer
.index(artifactNode
);
111 if (log
.isDebugEnabled())
112 log
.debug("Imported " + entryName
+ " to " + artifactNode
);
114 } catch (Exception e
) {
115 throw new SlcException("Cannot import zip " + zipFile
+ " to "
118 IOUtils
.closeQuietly(zipIn
);
119 IOUtils
.closeQuietly(jarIn
);
120 JcrUtils
.logoutQuietly(session
);
125 public void setRepository(Repository repository
) {
126 this.repository
= repository
;
129 public void setWorkspace(String workspace
) {
130 this.workspace
= workspace
;
133 public void setGroupId(String groupId
) {
134 this.groupId
= groupId
;
137 public void setArtifactBasePath(String artifactBasePath
) {
138 this.artifactBasePath
= artifactBasePath
;
141 public void setZipFile(String zipFile
) {
142 this.zipFile
= zipFile
;
145 public void setExcludedBundles(List
<String
> excludedBundles
) {
146 this.excludedBundles
= excludedBundles
;