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