]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ImportBundlesZip.java
Clean repo indexer, update modular distribution management, remove binaries concepts.
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / osgi / ImportBundlesZip.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.repo.osgi;
17
18 import java.io.ByteArrayInputStream;
19 import java.net.URL;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.jar.JarInputStream;
23 import java.util.jar.Manifest;
24 import java.util.zip.ZipEntry;
25 import java.util.zip.ZipInputStream;
26
27 import javax.jcr.Node;
28 import javax.jcr.Repository;
29 import javax.jcr.Session;
30
31 import org.apache.commons.io.IOUtils;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.argeo.jcr.JcrUtils;
35 import org.argeo.slc.NameVersion;
36 import org.argeo.slc.SlcException;
37 import org.argeo.slc.repo.ArtifactIndexer;
38 import org.argeo.slc.repo.JarFileIndexer;
39 import org.argeo.slc.repo.RepoUtils;
40 import org.sonatype.aether.artifact.Artifact;
41 import org.sonatype.aether.util.artifact.DefaultArtifact;
42
43 /**
44 * Import all bundles in a zip file (typically an Eclipse distribution) into the
45 * workspace.
46 *
47 * @deprecated Use {@link ArchiveWrapper} instead.
48 */
49 @Deprecated
50 public class ImportBundlesZip implements Runnable {
51 private final static Log log = LogFactory.getLog(ImportBundlesZip.class);
52 private Repository repository;
53 private String workspace;
54 private String groupId;
55 private String artifactBasePath = "/";
56
57 private ArtifactIndexer artifactIndexer = new ArtifactIndexer();
58 private JarFileIndexer jarFileIndexer = new JarFileIndexer();
59
60 private String zipFile;
61
62 private List<String> excludedBundles = new ArrayList<String>();
63
64 public void run() {
65 ZipInputStream zipIn = null;
66 JarInputStream jarIn = null;
67 Session session = null;
68 try {
69 URL url = new URL(zipFile);
70 session = repository.login(workspace);
71
72 // clear
73 // String groupPath = MavenConventionsUtils.groupPath(
74 // artifactBasePath, groupId);
75 // if (session.itemExists(groupPath)) {
76 // session.getNode(groupPath).remove();
77 // session.save();
78 // if (log.isDebugEnabled())
79 // log.debug("Cleared " + groupPath);
80 // }
81
82 zipIn = new ZipInputStream(url.openStream());
83 ZipEntry zipEntry = null;
84 entries: while ((zipEntry = zipIn.getNextEntry()) != null) {
85 String entryName = zipEntry.getName();
86 if (!entryName.endsWith(".jar")
87 || entryName.contains("feature"))
88 continue entries;// skip
89 byte[] jarBytes = IOUtils.toByteArray(zipIn);
90 zipIn.closeEntry();
91 jarIn = new JarInputStream(new ByteArrayInputStream(jarBytes));
92 Manifest manifest = jarIn.getManifest();
93 IOUtils.closeQuietly(jarIn);
94 if (manifest == null) {
95 log.warn(entryName + " has no MANIFEST");
96 continue entries;
97 }
98 NameVersion nv;
99 try {
100 nv = RepoUtils.readNameVersion(manifest);
101 } catch (Exception e) {
102 log.warn("Cannot read name version from " + entryName, e);
103 continue entries;
104 }
105
106 String bundleName = RepoUtils
107 .extractBundleNameFromSourceName(nv.getName());
108 // skip excluded bundles and their sources
109 if (excludedBundles.contains(bundleName))
110 continue entries;
111 // for(String excludedBundle:excludedBundles){
112 // if(bundleName.contains(excludedBundle))
113 // continue entries;
114 // }
115
116 Artifact artifact = new DefaultArtifact(groupId, nv.getName(),
117 "jar", nv.getVersion());
118 Node artifactNode = RepoUtils.copyBytesAsArtifact(
119 session.getNode(artifactBasePath), artifact, jarBytes);
120 jarBytes = null;// superstition, in order to free memory
121
122 // indexes
123 artifactIndexer.index(artifactNode);
124 jarFileIndexer.index(artifactNode);
125 session.save();
126 if (log.isDebugEnabled())
127 log.debug("Imported " + entryName + " to " + artifactNode);
128 }
129 } catch (Exception e) {
130 throw new SlcException("Cannot import zip " + zipFile + " to "
131 + workspace, e);
132 } finally {
133 IOUtils.closeQuietly(zipIn);
134 IOUtils.closeQuietly(jarIn);
135 JcrUtils.logoutQuietly(session);
136 }
137
138 }
139
140 public void setRepository(Repository repository) {
141 this.repository = repository;
142 }
143
144 public void setWorkspace(String workspace) {
145 this.workspace = workspace;
146 }
147
148 public void setGroupId(String groupId) {
149 this.groupId = groupId;
150 }
151
152 public void setArtifactBasePath(String artifactBasePath) {
153 this.artifactBasePath = artifactBasePath;
154 }
155
156 public void setZipFile(String zipFile) {
157 this.zipFile = zipFile;
158 }
159
160 public void setExcludedBundles(List<String> excludedBundles) {
161 this.excludedBundles = excludedBundles;
162 }
163
164 }