KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > builder > ClasspathJar


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * Tal Lev-Ami - added package cache for zip files
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.core.builder;
13
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.runtime.*;
16
17 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
18 import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
19 import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
20 import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable;
21 import org.eclipse.jdt.internal.compiler.util.SimpleSet;
22 import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
23 import org.eclipse.jdt.internal.core.util.Util;
24
25 import java.io.*;
26 import java.util.*;
27 import java.util.zip.*;
28
29 public class ClasspathJar extends ClasspathLocation {
30
31 static class PackageCacheEntry {
32     long lastModified;
33     long fileSize;
34     SimpleSet packageSet;
35     
36     PackageCacheEntry(long lastModified, long fileSize, SimpleSet packageSet) {
37         this.lastModified = lastModified;
38         this.fileSize = fileSize;
39         this.packageSet = packageSet;
40     }
41 }
42
43 static SimpleLookupTable PackageCache = new SimpleLookupTable();
44
45 /**
46  * Calculate and cache the package list available in the zipFile.
47  * @param jar The ClasspathJar to use
48  * @return A SimpleSet with the all the package names in the zipFile.
49  */

50 static SimpleSet findPackageSet(ClasspathJar jar) {
51     String JavaDoc zipFileName = jar.zipFilename;
52     long lastModified = jar.lastModified();
53     long fileSize = new File(zipFileName).length();
54     PackageCacheEntry cacheEntry = (PackageCacheEntry) PackageCache.get(zipFileName);
55     if (cacheEntry != null && cacheEntry.lastModified == lastModified && cacheEntry.fileSize == fileSize)
56         return cacheEntry.packageSet;
57
58     SimpleSet packageSet = new SimpleSet(41);
59     packageSet.add(""); //$NON-NLS-1$
60
nextEntry : for (Enumeration e = jar.zipFile.entries(); e.hasMoreElements(); ) {
61         String JavaDoc fileName = ((ZipEntry) e.nextElement()).getName();
62
63         // add the package name & all of its parent packages
64
int last = fileName.lastIndexOf('/');
65         while (last > 0) {
66             // extract the package name
67
String JavaDoc packageName = fileName.substring(0, last);
68             if (packageSet.addIfNotIncluded(packageName) == null)
69                 continue nextEntry; // already existed
70
last = packageName.lastIndexOf('/');
71         }
72     }
73
74     PackageCache.put(zipFileName, new PackageCacheEntry(lastModified, fileSize, packageSet));
75     return packageSet;
76 }
77
78
79 String JavaDoc zipFilename; // keep for equals
80
IFile resource;
81 ZipFile zipFile;
82 long lastModified;
83 boolean closeZipFileAtEnd;
84 SimpleSet knownPackageNames;
85 AccessRuleSet accessRuleSet;
86
87 ClasspathJar(IFile resource, AccessRuleSet accessRuleSet) {
88     this.resource = resource;
89     try {
90         java.net.URI JavaDoc location = resource.getLocationURI();
91         if (location == null) {
92             this.zipFilename = ""; //$NON-NLS-1$
93
} else {
94             File localFile = Util.toLocalFile(location, null);
95             this.zipFilename = localFile.getPath();
96         }
97     } catch (CoreException e) {
98         // ignore
99
}
100     this.zipFile = null;
101     this.knownPackageNames = null;
102     this.accessRuleSet = accessRuleSet;
103 }
104
105 ClasspathJar(String JavaDoc zipFilename, long lastModified, AccessRuleSet accessRuleSet) {
106     this.zipFilename = zipFilename;
107     this.lastModified = lastModified;
108     this.zipFile = null;
109     this.knownPackageNames = null;
110     this.accessRuleSet = accessRuleSet;
111 }
112
113 public ClasspathJar(ZipFile zipFile, AccessRuleSet accessRuleSet) {
114     this.zipFilename = zipFile.getName();
115     this.zipFile = zipFile;
116     this.closeZipFileAtEnd = false;
117     this.knownPackageNames = null;
118     this.accessRuleSet = accessRuleSet;
119 }
120
121 public void cleanup() {
122     if (this.zipFile != null && this.closeZipFileAtEnd) {
123         try {
124             this.zipFile.close();
125         } catch(IOException e) { // ignore it
126
}
127         this.zipFile = null;
128     }
129     this.knownPackageNames = null;
130 }
131
132 public boolean equals(Object JavaDoc o) {
133     if (this == o) return true;
134     if (!(o instanceof ClasspathJar)) return false;
135
136     ClasspathJar jar = (ClasspathJar) o;
137     if (this.accessRuleSet != jar.accessRuleSet)
138         if (this.accessRuleSet == null || !this.accessRuleSet.equals(jar.accessRuleSet))
139             return false;
140     return this.zipFilename.equals(jar.zipFilename) && this.lastModified() == jar.lastModified();
141 }
142
143 public NameEnvironmentAnswer findClass(String JavaDoc binaryFileName, String JavaDoc qualifiedPackageName, String JavaDoc qualifiedBinaryFileName) {
144     if (!isPackage(qualifiedPackageName)) return null; // most common case
145

146     try {
147         ClassFileReader reader = ClassFileReader.read(this.zipFile, qualifiedBinaryFileName);
148         if (reader != null) {
149             if (this.accessRuleSet == null)
150                 return new NameEnvironmentAnswer(reader, null);
151             String JavaDoc fileNameWithoutExtension = qualifiedBinaryFileName.substring(0, qualifiedBinaryFileName.length() - SuffixConstants.SUFFIX_CLASS.length);
152             return new NameEnvironmentAnswer(reader, this.accessRuleSet.getViolatedRestriction(fileNameWithoutExtension.toCharArray()));
153         }
154     } catch (Exception JavaDoc e) { // treat as if class file is missing
155
}
156     return null;
157 }
158
159 public IPath getProjectRelativePath() {
160     if (this.resource == null) return null;
161     return this.resource.getProjectRelativePath();
162 }
163
164 public boolean isPackage(String JavaDoc qualifiedPackageName) {
165     if (this.knownPackageNames != null)
166         return this.knownPackageNames.includes(qualifiedPackageName);
167
168     try {
169         if (this.zipFile == null) {
170             if (org.eclipse.jdt.internal.core.JavaModelManager.ZIP_ACCESS_VERBOSE) {
171                 System.out.println("(" + Thread.currentThread() + ") [ClasspathJar.isPackage(String)] Creating ZipFile on " + zipFilename); //$NON-NLS-1$ //$NON-NLS-2$
172
}
173             this.zipFile = new ZipFile(zipFilename);
174             this.closeZipFileAtEnd = true;
175         }
176         this.knownPackageNames = findPackageSet(this);
177     } catch(Exception JavaDoc e) {
178         this.knownPackageNames = new SimpleSet(); // assume for this build the zipFile is empty
179
}
180     return this.knownPackageNames.includes(qualifiedPackageName);
181 }
182
183 public long lastModified() {
184     if (this.lastModified == 0)
185         this.lastModified = new File(this.zipFilename).lastModified();
186     return this.lastModified;
187 }
188
189 public String JavaDoc toString() {
190     String JavaDoc start = "Classpath jar file " + this.zipFilename; //$NON-NLS-1$
191
if (this.accessRuleSet == null)
192         return start;
193     return start + " with " + this.accessRuleSet; //$NON-NLS-1$
194
}
195
196 public String JavaDoc debugPathString() {
197     long time = lastModified();
198     if (time == 0)
199         return this.zipFilename;
200     return this.zipFilename + '(' + (new Date(time)) + " : " + time + ')'; //$NON-NLS-1$
201
}
202
203 }
204
Popular Tags