1 | /******************************************************************************* |
2 | * Copyright (c) 2007, 2009 IBM Corporation and others. |
3 | * All rights reserved. This program and the accompanying materials |
4 | * are made available under the terms of the Eclipse Public License v1.0 |
5 | * which accompanies this distribution, and is available at |
6 | * http://www.eclipse.org/legal/epl-v10.html |
7 | * |
8 | * Contributors: |
9 | * IBM Corporation - initial API and implementation |
10 | *******************************************************************************/ |
11 | package org.eclipse.pde.api.tools.internal.model; |
12 | |
13 | import java.io.IOException; |
14 | import java.io.InputStream; |
15 | import java.util.ArrayList; |
16 | import java.util.Arrays; |
17 | import java.util.Collections; |
18 | import java.util.Enumeration; |
19 | import java.util.HashMap; |
20 | import java.util.HashSet; |
21 | import java.util.Iterator; |
22 | import java.util.List; |
23 | import java.util.Map; |
24 | import java.util.Set; |
25 | import java.util.zip.ZipEntry; |
26 | import java.util.zip.ZipFile; |
27 | |
28 | import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; |
29 | import org.eclipse.pde.api.tools.internal.provisional.model.ApiTypeContainerVisitor; |
30 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiElement; |
31 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeContainer; |
32 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeRoot; |
33 | import org.eclipse.pde.api.tools.internal.util.Util; |
34 | |
35 | import org.eclipse.core.runtime.CoreException; |
36 | |
37 | /** |
38 | * {@link IApiTypeContainer} container for an archive (jar or zip) file. |
39 | * |
40 | * @since 1.0.0 |
41 | */ |
42 | public class ArchiveApiTypeContainer extends ApiElement implements IApiTypeContainer { |
43 | |
44 | /** |
45 | * {@link IApiTypeRoot} implementation within an archive |
46 | */ |
47 | static class ArchiveApiTypeRoot extends AbstractApiTypeRoot implements Comparable { |
48 | |
49 | private String fTypeName; |
50 | |
51 | /** |
52 | * Constructs a new handle to an {@link IApiTypeRoot} in the archive. |
53 | * |
54 | * @param container archive |
55 | * @param entryName zip entry name |
56 | */ |
57 | public ArchiveApiTypeRoot(ArchiveApiTypeContainer container, String entryName) { |
58 | super(container, entryName); |
59 | } |
60 | |
61 | /* (non-Javadoc) |
62 | * @see org.eclipse.pde.api.tools.manifest.IClassFile#getTypeName() |
63 | */ |
64 | public String getTypeName() { |
65 | if (fTypeName == null) { |
66 | fTypeName = getName().replace('/', '.').substring(0, getName().length() - Util.DOT_CLASS_SUFFIX.length()); |
67 | } |
68 | return fTypeName; |
69 | } |
70 | |
71 | /* (non-Javadoc) |
72 | * @see java.lang.Comparable#compareTo(java.lang.Object) |
73 | */ |
74 | public int compareTo(Object o) { |
75 | return getTypeName().compareTo(((ArchiveApiTypeRoot)o).getTypeName()); |
76 | } |
77 | |
78 | /** |
79 | * @see java.lang.Object#equals(java.lang.Object) |
80 | */ |
81 | public boolean equals(Object obj) { |
82 | if (obj instanceof ArchiveApiTypeRoot) { |
83 | ArchiveApiTypeRoot classFile = (ArchiveApiTypeRoot) obj; |
84 | return this.getName().equals(classFile.getName()); |
85 | } |
86 | return false; |
87 | } |
88 | |
89 | /** |
90 | * @see java.lang.Object#hashCode() |
91 | */ |
92 | public int hashCode() { |
93 | return getName().hashCode(); |
94 | } |
95 | |
96 | /* (non-Javadoc) |
97 | * @see org.eclipse.pde.api.tools.internal.model.AbstractApiTypeRoot#getContents() |
98 | */ |
99 | public byte[] getContents() throws CoreException { |
100 | ArchiveApiTypeContainer archive = (ArchiveApiTypeContainer) getParent(); |
101 | ZipFile zipFile; |
102 | try { |
103 | zipFile= new ZipFile(archive.fLocation); |
104 | } catch (IOException e) { |
105 | abort("Failed to open archive: " + archive.fLocation, e); //$NON-NLS-1$ |
106 | return null; |
107 | } |
108 | try { |
109 | ZipEntry entry = zipFile.getEntry(getName()); |
110 | InputStream stream = null; |
111 | if (entry != null) { |
112 | try { |
113 | stream = zipFile.getInputStream(entry); |
114 | } catch (IOException e) { |
115 | abort("Failed to open class file: " + getTypeName() + " in archive: " + archive.fLocation, e); //$NON-NLS-1$ //$NON-NLS-2$ |
116 | return null; |
117 | } |
118 | try { |
119 | return Util.getInputStreamAsByteArray(stream, -1); |
120 | } |
121 | catch(IOException ioe) { |
122 | abort("Unable to read class file: " + getTypeName(), ioe); //$NON-NLS-1$ |
123 | return null; |
124 | } |
125 | finally { |
126 | try { |
127 | stream.close(); |
128 | } catch (IOException e) { |
129 | ApiPlugin.log(e); |
130 | } |
131 | } |
132 | } |
133 | } finally { |
134 | try { |
135 | zipFile.close(); |
136 | } catch (IOException e) { |
137 | abort("Failed to close class file archive", e); //$NON-NLS-1$ |
138 | } |
139 | } |
140 | abort("Class file not found: " + getTypeName() + " in archive: " + archive.fLocation, null); //$NON-NLS-1$ //$NON-NLS-2$ |
141 | return null; |
142 | } |
143 | |
144 | /* (non-Javadoc) |
145 | * @see java.lang.Object#toString() |
146 | */ |
147 | public String toString() { |
148 | return getTypeName(); |
149 | } |
150 | } |
151 | |
152 | /** |
153 | * Location of the archive in the local file system. |
154 | */ |
155 | String fLocation; |
156 | |
157 | /** |
158 | * Cache of package names to class file paths in that package, |
159 | * or <code>null</code> if not yet initialized. |
160 | */ |
161 | private Map fPackages; |
162 | |
163 | /** |
164 | * Cache of package names in this archive. |
165 | */ |
166 | private String[] fPackageNames; |
167 | |
168 | |
169 | /** |
170 | * Constructs an {@link IApiTypeContainer} container for the given jar or zip file |
171 | * at the specified location. |
172 | * |
173 | * @param parent the parent {@link IApiElement} or <code>null</code> if none |
174 | * @param path location of the file in the local file system |
175 | */ |
176 | public ArchiveApiTypeContainer(IApiElement parent, String path) { |
177 | super(parent, IApiElement.API_TYPE_CONTAINER, path); |
178 | this.fLocation = path; |
179 | } |
180 | |
181 | /** |
182 | * @see org.eclipse.pde.api.tools.internal.AbstractApiTypeContainer#accept(org.eclipse.pde.api.tools.internal.provisional.ApiTypeContainerVisitor) |
183 | */ |
184 | public void accept(ApiTypeContainerVisitor visitor) throws CoreException { |
185 | if(visitor.visit(this)) { |
186 | init(); |
187 | List packages = new ArrayList(fPackages.keySet()); |
188 | Collections.sort(packages); |
189 | Iterator iterator = packages.iterator(); |
190 | while (iterator.hasNext()) { |
191 | String pkg = (String) iterator.next(); |
192 | if (visitor.visitPackage(pkg)) { |
193 | List types = new ArrayList((Set) fPackages.get(pkg)); |
194 | Iterator cfIterator = types.iterator(); |
195 | List classFiles = new ArrayList(types.size()); |
196 | while (cfIterator.hasNext()) { |
197 | String entryName = (String) cfIterator.next(); |
198 | classFiles.add(new ArchiveApiTypeRoot(this, entryName)); |
199 | } |
200 | Collections.sort(classFiles); |
201 | cfIterator = classFiles.iterator(); |
202 | while (cfIterator.hasNext()) { |
203 | ArchiveApiTypeRoot classFile = (ArchiveApiTypeRoot) cfIterator.next(); |
204 | visitor.visit(pkg, classFile); |
205 | visitor.end(pkg, classFile); |
206 | } |
207 | } |
208 | visitor.endVisitPackage(pkg); |
209 | } |
210 | } |
211 | visitor.end(this); |
212 | } |
213 | |
214 | /** |
215 | * @see java.lang.Object#toString() |
216 | */ |
217 | public String toString() { |
218 | StringBuffer buff = new StringBuffer(); |
219 | buff.append("Archive Class File Container: "+getName()); //$NON-NLS-1$ |
220 | return buff.toString(); |
221 | } |
222 | |
223 | /** |
224 | * @see org.eclipse.pde.api.tools.internal.AbstractApiTypeContainer#close() |
225 | */ |
226 | public synchronized void close() throws CoreException { |
227 | } |
228 | |
229 | /** |
230 | * @see org.eclipse.pde.api.tools.internal.provisional.IApiTypeContainer#findTypeRoot(java.lang.String) |
231 | */ |
232 | public IApiTypeRoot findTypeRoot(String qualifiedName) throws CoreException { |
233 | init(); |
234 | int index = qualifiedName.lastIndexOf('.'); |
235 | String packageName = Util.DEFAULT_PACKAGE_NAME; |
236 | if (index >= 0) { |
237 | packageName = qualifiedName.substring(0, index); |
238 | } |
239 | Set classFileNames = (Set) fPackages.get(packageName); |
240 | if (classFileNames != null) { |
241 | String fileName = qualifiedName.replace('.', '/') + Util.DOT_CLASS_SUFFIX; |
242 | if (classFileNames.contains(fileName)) { |
243 | return new ArchiveApiTypeRoot(this, fileName); |
244 | } |
245 | } |
246 | return null; |
247 | } |
248 | |
249 | /** |
250 | * @see org.eclipse.pde.api.tools.internal.AbstractApiTypeContainer#getPackageNames() |
251 | */ |
252 | public String[] getPackageNames() throws CoreException { |
253 | init(); |
254 | synchronized (this) { |
255 | if (fPackageNames == null) { |
256 | Set names = fPackages.keySet(); |
257 | String[] result = new String[names.size()]; |
258 | names.toArray(result); |
259 | Arrays.sort(result); |
260 | fPackageNames = result; |
261 | } |
262 | return fPackageNames; |
263 | } |
264 | } |
265 | |
266 | /** |
267 | * Initializes cache of packages and types. |
268 | * |
269 | * @throws CoreException |
270 | */ |
271 | private synchronized void init() throws CoreException { |
272 | if (fPackages == null) { |
273 | fPackages = new HashMap(); |
274 | ZipFile zipFile; |
275 | try { |
276 | zipFile= new ZipFile(fLocation); |
277 | } catch (IOException e) { |
278 | abort("Failed to open archive: " + fLocation, e); //$NON-NLS-1$ |
279 | return; |
280 | } |
281 | try { |
282 | Enumeration entries= zipFile.entries(); |
283 | while (entries.hasMoreElements()) { |
284 | ZipEntry entry= (ZipEntry)entries.nextElement(); |
285 | String name= entry.getName(); |
286 | if (name.endsWith(Util.DOT_CLASS_SUFFIX)) { |
287 | String pkg= Util.DEFAULT_PACKAGE_NAME; |
288 | int index= name.lastIndexOf('/'); |
289 | if (index >= 0) { |
290 | pkg= name.substring(0, index).replace('/', '.'); |
291 | } |
292 | Set fileNames= (Set)fPackages.get(pkg); |
293 | if (fileNames == null) { |
294 | fileNames= new HashSet(); |
295 | fPackages.put(pkg, fileNames); |
296 | } |
297 | fileNames.add(name); |
298 | } |
299 | } |
300 | } finally { |
301 | try { |
302 | zipFile.close(); |
303 | } catch (IOException e) { |
304 | abort("Failed to close class file archive", e); //$NON-NLS-1$ |
305 | } |
306 | } |
307 | } |
308 | } |
309 | |
310 | |
311 | /* (non-Javadoc) |
312 | * @see java.lang.Object#equals(java.lang.Object) |
313 | */ |
314 | public boolean equals(Object obj) { |
315 | if (obj instanceof ArchiveApiTypeContainer) { |
316 | return this.fLocation.equals(((ArchiveApiTypeContainer) obj).fLocation); |
317 | } |
318 | return false; |
319 | } |
320 | /* (non-Javadoc) |
321 | * @see java.lang.Object#hashCode() |
322 | */ |
323 | public int hashCode() { |
324 | return this.fLocation.hashCode(); |
325 | } |
326 | |
327 | /** |
328 | * @see org.eclipse.pde.api.tools.internal.provisional.IApiTypeContainer#findTypeRoot(java.lang.String, java.lang.String) |
329 | */ |
330 | public IApiTypeRoot findTypeRoot(String qualifiedName, String id) throws CoreException { |
331 | return findTypeRoot(qualifiedName); |
332 | } |
333 | |
334 | /* (non-Javadoc) |
335 | * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeContainer#getContainerType() |
336 | */ |
337 | public int getContainerType() { |
338 | return ARCHIVE; |
339 | } |
340 | } |