]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ImportBundlesZip.java
Some more UI functionalities :
[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 public class ImportBundlesZip implements Runnable {
48 private final static Log log = LogFactory.getLog(ImportBundlesZip.class);
49 private Repository repository;
50 private String workspace;
51 private String groupId;
52 private String artifactBasePath = "/";
53
54 private ArtifactIndexer artifactIndexer = new ArtifactIndexer();
55 private JarFileIndexer jarFileIndexer = new JarFileIndexer();
56
57 private String zipFile;
58
59 private List<String> excludedBundles = new ArrayList<String>();
60
61 public void run() {
62 ZipInputStream zipIn = null;
63 JarInputStream jarIn = null;
64 Session session = null;
65 try {
66 URL url = new URL(zipFile);
67 session = repository.login(workspace);
68
69 // clear
70 // String groupPath = MavenConventionsUtils.groupPath(
71 // artifactBasePath, groupId);
72 // if (session.itemExists(groupPath)) {
73 // session.getNode(groupPath).remove();
74 // session.save();
75 // if (log.isDebugEnabled())
76 // log.debug("Cleared " + groupPath);
77 // }
78
79 zipIn = new ZipInputStream(url.openStream());
80 ZipEntry zipEntry = null;
81 entries: while ((zipEntry = zipIn.getNextEntry()) != null) {
82 String entryName = zipEntry.getName();
83 if (!entryName.endsWith(".jar")
84 || entryName.contains("feature"))
85 continue entries;// skip
86 byte[] jarBytes = IOUtils.toByteArray(zipIn);
87 zipIn.closeEntry();
88 jarIn = new JarInputStream(new ByteArrayInputStream(jarBytes));
89 Manifest manifest = jarIn.getManifest();
90 IOUtils.closeQuietly(jarIn);
91 if (manifest == null) {
92 log.warn(entryName + " has no MANIFEST");
93 continue entries;
94 }
95 NameVersion nv;
96 try {
97 nv = RepoUtils.readNameVersion(manifest);
98 } catch (Exception e) {
99 log.warn("Cannot read name version from " + entryName, e);
100 continue entries;
101 }
102
103 String bundleName = RepoUtils.extractBundleNameFromSourceName(nv
104 .getName());
105 // skip excluded bundles and their sources
106 if (excludedBundles.contains(bundleName))
107 continue entries;
108 // for(String excludedBundle:excludedBundles){
109 // if(bundleName.contains(excludedBundle))
110 // continue entries;
111 // }
112
113 Artifact artifact = new DefaultArtifact(groupId, nv.getName(),
114 "jar", nv.getVersion());
115 Node artifactNode = RepoUtils.copyBytesAsArtifact(
116 session.getNode(artifactBasePath), artifact, jarBytes);
117 jarBytes = null;// superstition, in order to free memory
118
119 // indexes
120 artifactIndexer.index(artifactNode);
121 jarFileIndexer.index(artifactNode);
122 session.save();
123 if (log.isDebugEnabled())
124 log.debug("Imported " + entryName + " to " + artifactNode);
125 }
126 } catch (Exception e) {
127 throw new SlcException("Cannot import zip " + zipFile + " to "
128 + workspace, e);
129 } finally {
130 IOUtils.closeQuietly(zipIn);
131 IOUtils.closeQuietly(jarIn);
132 JcrUtils.logoutQuietly(session);
133 }
134
135 }
136
137 public void setRepository(Repository repository) {
138 this.repository = repository;
139 }
140
141 public void setWorkspace(String workspace) {
142 this.workspace = workspace;
143 }
144
145 public void setGroupId(String groupId) {
146 this.groupId = groupId;
147 }
148
149 public void setArtifactBasePath(String artifactBasePath) {
150 this.artifactBasePath = artifactBasePath;
151 }
152
153 public void setZipFile(String zipFile) {
154 this.zipFile = zipFile;
155 }
156
157 public void setExcludedBundles(List<String> excludedBundles) {
158 this.excludedBundles = excludedBundles;
159 }
160
161 }