Author: bodewig
Date: Thu Mar 4 15:16:49 2010
New Revision: 919018
URL: http://svn.apache.org/viewvc?rev=919018&view=rev
Log:
embrace java.util.jar. PR 48853
Modified:
ant/core/trunk/src/main/org/apache/tools/ant/AntClassLoader.java
Modified: ant/core/trunk/src/main/org/apache/tools/ant/AntClassLoader.java
URL:
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/AntClassLoader.java?rev=919018&r1=919017&r2=919018&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/AntClassLoader.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/AntClassLoader.java Thu Mar 4
15:16:49 2010
@@ -43,8 +43,6 @@
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.util.CollectionUtils;
import org.apache.tools.ant.util.FileUtils;
@@ -213,9 +211,9 @@
private ClassLoader parent = null;
/**
- * A hashtable of zip files opened by the classloader (File to ZipFile).
+ * A hashtable of zip files opened by the classloader (File to JarFile).
*/
- private Hashtable zipFiles = new Hashtable();
+ private Hashtable jarFiles = new Hashtable();
/** Static map of jar file/time to manifest class-path entries */
private static Map/*<String,String>*/ pathMap =
Collections.synchronizedMap(new HashMap());
@@ -488,23 +486,16 @@
+ pathComponent.lastModified() + "-" + pathComponent.length();
String classpath = (String) pathMap.get(absPathPlusTimeAndLength);
if (classpath == null) {
- ZipFile jarFile = null;
- InputStream manifestStream = null;
+ JarFile jarFile = null;
try {
- jarFile = new ZipFile(pathComponent);
- manifestStream = jarFile.getInputStream(new
ZipEntry("META-INF/MANIFEST.MF"));
-
- if (manifestStream == null) {
+ jarFile = new JarFile(pathComponent);
+ Manifest manifest = jarFile.getManifest();
+ if (manifest == null) {
return;
}
- Reader manifestReader = new InputStreamReader(manifestStream,
"UTF-8");
- org.apache.tools.ant.taskdefs.Manifest manifest
- = new
org.apache.tools.ant.taskdefs.Manifest(manifestReader);
- classpath =
manifest.getMainSection().getAttributeValue("Class-Path");
- } catch (org.apache.tools.ant.taskdefs.ManifestException e) {
- // ignore
+ classpath = manifest.getMainAttributes()
+ .getValue(Attributes.Name.CLASS_PATH);
} finally {
- FileUtils.close(manifestStream);
if (jarFile != null) {
jarFile.close();
}
@@ -783,27 +774,27 @@
*/
private InputStream getResourceStream(File file, String resourceName) {
try {
- ZipFile zipFile = (ZipFile) zipFiles.get(file);
- if (zipFile == null && file.isDirectory()) {
+ JarFile jarFile = (JarFile) jarFiles.get(file);
+ if (jarFile == null && file.isDirectory()) {
File resource = new File(file, resourceName);
if (resource.exists()) {
return new FileInputStream(resource);
}
} else {
- if (zipFile == null) {
+ if (jarFile == null) {
if (file.exists()) {
- zipFile = new ZipFile(file);
- zipFiles.put(file, zipFile);
+ jarFile = new JarFile(file);
+ jarFiles.put(file, jarFile);
} else {
return null;
}
//to eliminate a race condition, retrieve the entry
//that is in the hash table under that filename
- zipFile = (ZipFile) zipFiles.get(file);
+ jarFile = (JarFile) jarFiles.get(file);
}
- ZipEntry entry = zipFile.getEntry(resourceName);
+ JarEntry entry = jarFile.getJarEntry(resourceName);
if (entry != null) {
- return zipFile.getInputStream(entry);
+ return jarFile.getInputStream(entry);
}
}
} catch (Exception e) {
@@ -997,8 +988,8 @@
*/
protected URL getResourceURL(File file, String resourceName) {
try {
- ZipFile zipFile = (ZipFile) zipFiles.get(file);
- if (zipFile == null && file.isDirectory()) {
+ JarFile jarFile = (JarFile) jarFiles.get(file);
+ if (jarFile == null && file.isDirectory()) {
File resource = new File(file, resourceName);
if (resource.exists()) {
@@ -1009,15 +1000,17 @@
}
}
} else {
- if (zipFile == null) {
+ if (jarFile == null) {
if (file.exists()) {
- zipFile = new ZipFile(file);
- zipFiles.put(file, zipFile);
+ jarFile = new JarFile(file);
+ jarFiles.put(file, jarFile);
} else {
return null;
}
+ // potential race-condition
+ jarFile = (JarFile) jarFiles.get(file);
}
- ZipEntry entry = zipFile.getEntry(resourceName);
+ JarEntry entry = jarFile.getJarEntry(resourceName);
if (entry != null) {
try {
return new URL("jar:" + FILE_UTILS.getFileURL(file) +
"!/" + entry);
@@ -1180,15 +1173,11 @@
if (container.isDirectory()) {
return null;
}
- JarFile jarFile = null;
- try {
- jarFile = new JarFile(container);
- return jarFile.getManifest();
- } finally {
- if (jarFile != null) {
- jarFile.close();
- }
+ JarFile jarFile = (JarFile) jarFiles.get(container);
+ if (jarFile == null) {
+ return null;
}
+ return jarFile.getManifest();
}
/**
@@ -1207,24 +1196,12 @@
if (container.isDirectory()) {
return null;
}
- JarFile jarFile = null;
- InputStream is = null;
- try {
- jarFile = new JarFile(container);
- JarEntry ent = jarFile.getJarEntry(entry);
- if (ent != null) {
- // must read the input in order to obtain certificates
- is = new BufferedInputStream(jarFile.getInputStream(ent));
- byte[] b = new byte[BUFFER_SIZE];
- while (is.read(b, 0, BUFFER_SIZE) >= 0);
- }
- return ent == null ? null : ent.getCertificates();
- } finally {
- FileUtils.close(is);
- if (jarFile != null) {
- jarFile.close();
- }
+ JarFile jarFile = (JarFile) jarFiles.get(container);
+ if (jarFile == null) {
+ return null;
}
+ JarEntry ent = jarFile.getJarEntry(entry);
+ return ent == null ? null : ent.getCertificates();
}
/**
@@ -1414,15 +1391,15 @@
* files are closed.
*/
public synchronized void cleanup() {
- for (Enumeration e = zipFiles.elements(); e.hasMoreElements();) {
- ZipFile zipFile = (ZipFile) e.nextElement();
+ for (Enumeration e = jarFiles.elements(); e.hasMoreElements();) {
+ JarFile jarFile = (JarFile) e.nextElement();
try {
- zipFile.close();
+ jarFile.close();
} catch (IOException ioe) {
// ignore
}
}
- zipFiles = new Hashtable();
+ jarFiles = new Hashtable();
if (project != null) {
project.removeBuildListener(this);
}