]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ImportBundlesZip.java
Improve Repo
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / 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.sonatype.aether.artifact.Artifact;
26 import org.sonatype.aether.util.artifact.DefaultArtifact;
27
28 /**
29 * Import all bundles in a zip file (typically an Eclipse distribution) into the
30 * workspace.
31 */
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 = "/";
38
39 private ArtifactIndexer artifactIndexer = new ArtifactIndexer();
40 private JarFileIndexer jarFileIndexer = new JarFileIndexer();
41
42 private String zipFile;
43
44 private List<String> excludedBundles = new ArrayList<String>();
45
46 public void run() {
47 ZipInputStream zipIn = null;
48 JarInputStream jarIn = null;
49 Session session = null;
50 try {
51 URL url = new URL(zipFile);
52 session = repository.login(workspace);
53
54 // clear
55 // String groupPath = MavenConventionsUtils.groupPath(
56 // artifactBasePath, groupId);
57 // if (session.itemExists(groupPath)) {
58 // session.getNode(groupPath).remove();
59 // session.save();
60 // if (log.isDebugEnabled())
61 // log.debug("Cleared " + groupPath);
62 // }
63
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);
72 zipIn.closeEntry();
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");
78 continue entries;
79 }
80 NameVersion nv;
81 try {
82 nv = RepoUtils.readNameVersion(manifest);
83 } catch (Exception e) {
84 log.warn("Cannot read name version from " + entryName, e);
85 continue entries;
86 }
87
88 String bundleName = RepoUtils.extractBundleNameFromSourceName(nv
89 .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(),
99 "jar", nv.getVersion());
100 Node artifactNode = RepoUtils.copyBytesAsArtifact(
101 session.getNode(artifactBasePath), artifact, jarBytes);
102 jarBytes = null;// superstition, in order to free memory
103
104 // indexes
105 artifactIndexer.index(artifactNode);
106 jarFileIndexer.index(artifactNode);
107 session.save();
108 if (log.isDebugEnabled())
109 log.debug("Imported " + entryName + " to " + artifactNode);
110 }
111 } catch (Exception e) {
112 throw new SlcException("Cannot import zip " + zipFile + " to "
113 + workspace, e);
114 } finally {
115 IOUtils.closeQuietly(zipIn);
116 IOUtils.closeQuietly(jarIn);
117 JcrUtils.logoutQuietly(session);
118 }
119
120 }
121
122 public void setRepository(Repository repository) {
123 this.repository = repository;
124 }
125
126 public void setWorkspace(String workspace) {
127 this.workspace = workspace;
128 }
129
130 public void setGroupId(String groupId) {
131 this.groupId = groupId;
132 }
133
134 public void setArtifactBasePath(String artifactBasePath) {
135 this.artifactBasePath = artifactBasePath;
136 }
137
138 public void setZipFile(String zipFile) {
139 this.zipFile = zipFile;
140 }
141
142 public void setExcludedBundles(List<String> excludedBundles) {
143 this.excludedBundles = excludedBundles;
144 }
145
146 }