]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.repo/src/org/argeo/slc/repo/osgi/ImportBundlesZip.java
Merge remote-tracking branch 'origin/master' into testing
[gpl/argeo-slc.git] / org.argeo.slc.repo / src / org / argeo / slc / repo / osgi / ImportBundlesZip.java
1 package org.argeo.slc.repo.osgi;
2
3 import java.io.ByteArrayInputStream;
4 import java.net.URL;
5 import java.util.ArrayList;
6 import java.util.List;
7 import java.util.jar.JarInputStream;
8 import java.util.jar.Manifest;
9 import java.util.zip.ZipEntry;
10 import java.util.zip.ZipInputStream;
11
12 import javax.jcr.Node;
13 import javax.jcr.Repository;
14 import javax.jcr.Session;
15
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;
27
28 /**
29 * Import all bundles in a zip file (typically an Eclipse distribution) into the
30 * workspace.
31 *
32 * @deprecated Use {@link ArchiveWrapper} instead.
33 */
34 @Deprecated
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 = "/";
41
42 private ArtifactIndexer artifactIndexer = new ArtifactIndexer();
43 private JarFileIndexer jarFileIndexer = new JarFileIndexer();
44
45 private String zipFile;
46
47 private List<String> excludedBundles = new ArrayList<String>();
48
49 public void run() {
50 ZipInputStream zipIn = null;
51 JarInputStream jarIn = null;
52 Session session = null;
53 try {
54 URL url = new URL(zipFile);
55 session = repository.login(workspace);
56
57 // clear
58 // String groupPath = MavenConventionsUtils.groupPath(
59 // artifactBasePath, groupId);
60 // if (session.itemExists(groupPath)) {
61 // session.getNode(groupPath).remove();
62 // session.save();
63 // if (log.isDebugEnabled())
64 // log.debug("Cleared " + groupPath);
65 // }
66
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);
75 zipIn.closeEntry();
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");
81 continue entries;
82 }
83 NameVersion nv;
84 try {
85 nv = RepoUtils.readNameVersion(manifest);
86 } catch (Exception e) {
87 log.warn("Cannot read name version from " + entryName, e);
88 continue entries;
89 }
90
91 String bundleName = RepoUtils
92 .extractBundleNameFromSourceName(nv.getName());
93 // skip excluded bundles and their sources
94 if (excludedBundles.contains(bundleName))
95 continue entries;
96 // for(String excludedBundle:excludedBundles){
97 // if(bundleName.contains(excludedBundle))
98 // continue entries;
99 // }
100
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
106
107 // indexes
108 artifactIndexer.index(artifactNode);
109 jarFileIndexer.index(artifactNode);
110 session.save();
111 if (log.isDebugEnabled())
112 log.debug("Imported " + entryName + " to " + artifactNode);
113 }
114 } catch (Exception e) {
115 throw new SlcException("Cannot import zip " + zipFile + " to "
116 + workspace, e);
117 } finally {
118 IOUtils.closeQuietly(zipIn);
119 IOUtils.closeQuietly(jarIn);
120 JcrUtils.logoutQuietly(session);
121 }
122
123 }
124
125 public void setRepository(Repository repository) {
126 this.repository = repository;
127 }
128
129 public void setWorkspace(String workspace) {
130 this.workspace = workspace;
131 }
132
133 public void setGroupId(String groupId) {
134 this.groupId = groupId;
135 }
136
137 public void setArtifactBasePath(String artifactBasePath) {
138 this.artifactBasePath = artifactBasePath;
139 }
140
141 public void setZipFile(String zipFile) {
142 this.zipFile = zipFile;
143 }
144
145 public void setExcludedBundles(List<String> excludedBundles) {
146 this.excludedBundles = excludedBundles;
147 }
148
149 }