]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.repo/src/org/argeo/slc/repo/osgi/ImportBundlesZip.java
Make Eclipse E4 optional
[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.argeo.api.cms.CmsLog;
18 import org.argeo.jcr.JcrUtils;
19 import org.argeo.slc.NameVersion;
20 import org.argeo.slc.SlcException;
21 import org.argeo.slc.repo.ArtifactIndexer;
22 import org.argeo.slc.repo.JarFileIndexer;
23 import org.argeo.slc.repo.RepoUtils;
24 import org.eclipse.aether.artifact.Artifact;
25 import org.eclipse.aether.artifact.DefaultArtifact;
26
27 /**
28 * Import all bundles in a zip file (typically an Eclipse distribution) into the
29 * workspace.
30 *
31 * @deprecated Use {@link ArchiveWrapper} instead.
32 */
33 @Deprecated
34 public class ImportBundlesZip implements Runnable {
35 private final static CmsLog log = CmsLog.getLog(ImportBundlesZip.class);
36 private Repository repository;
37 private String workspace;
38 private String groupId;
39 private String artifactBasePath = "/";
40
41 private ArtifactIndexer artifactIndexer = new ArtifactIndexer();
42 private JarFileIndexer jarFileIndexer = new JarFileIndexer();
43
44 private String zipFile;
45
46 private List<String> excludedBundles = new ArrayList<String>();
47
48 public void run() {
49 ZipInputStream zipIn = null;
50 JarInputStream jarIn = null;
51 Session session = null;
52 try {
53 URL url = new URL(zipFile);
54 session = repository.login(workspace);
55
56 // clear
57 // String groupPath = MavenConventionsUtils.groupPath(
58 // artifactBasePath, groupId);
59 // if (session.itemExists(groupPath)) {
60 // session.getNode(groupPath).remove();
61 // session.save();
62 // if (log.isDebugEnabled())
63 // log.debug("Cleared " + groupPath);
64 // }
65
66 zipIn = new ZipInputStream(url.openStream());
67 ZipEntry zipEntry = null;
68 entries: while ((zipEntry = zipIn.getNextEntry()) != null) {
69 String entryName = zipEntry.getName();
70 if (!entryName.endsWith(".jar") || entryName.contains("feature"))
71 continue entries;// skip
72 byte[] jarBytes = IOUtils.toByteArray(zipIn);
73 zipIn.closeEntry();
74 jarIn = new JarInputStream(new ByteArrayInputStream(jarBytes));
75 Manifest manifest = jarIn.getManifest();
76 IOUtils.closeQuietly(jarIn);
77 if (manifest == null) {
78 log.warn(entryName + " has no MANIFEST");
79 continue entries;
80 }
81 NameVersion nv;
82 try {
83 nv = RepoUtils.readNameVersion(manifest);
84 } catch (Exception e) {
85 log.warn("Cannot read name version from " + entryName, e);
86 continue entries;
87 }
88
89 String bundleName = RepoUtils.extractBundleNameFromSourceName(nv.getName());
90 // skip excluded bundles and their sources
91 if (excludedBundles.contains(bundleName))
92 continue entries;
93 // for(String excludedBundle:excludedBundles){
94 // if(bundleName.contains(excludedBundle))
95 // continue entries;
96 // }
97
98 Artifact artifact = new DefaultArtifact(groupId, nv.getName(), "jar", nv.getVersion());
99 Node artifactNode = RepoUtils.copyBytesAsArtifact(session.getNode(artifactBasePath), artifact,
100 jarBytes);
101 jarBytes = null;// superstition, in order to free memory
102
103 // indexes
104 artifactIndexer.index(artifactNode);
105 jarFileIndexer.index(artifactNode);
106 session.save();
107 if (log.isDebugEnabled())
108 log.debug("Imported " + entryName + " to " + artifactNode);
109 }
110 } catch (Exception e) {
111 throw new SlcException("Cannot import zip " + zipFile + " to " + workspace, e);
112 } finally {
113 IOUtils.closeQuietly(zipIn);
114 IOUtils.closeQuietly(jarIn);
115 JcrUtils.logoutQuietly(session);
116 }
117
118 }
119
120 public void setRepository(Repository repository) {
121 this.repository = repository;
122 }
123
124 public void setWorkspace(String workspace) {
125 this.workspace = workspace;
126 }
127
128 public void setGroupId(String groupId) {
129 this.groupId = groupId;
130 }
131
132 public void setArtifactBasePath(String artifactBasePath) {
133 this.artifactBasePath = artifactBasePath;
134 }
135
136 public void setZipFile(String zipFile) {
137 this.zipFile = zipFile;
138 }
139
140 public void setExcludedBundles(List<String> excludedBundles) {
141 this.excludedBundles = excludedBundles;
142 }
143
144 }