Browse Source

embrace java.util.jar. PR 48853

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@919018 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 15 years ago
parent
commit
b33d33d754
1 changed files with 37 additions and 60 deletions
  1. +37
    -60
      src/main/org/apache/tools/ant/AntClassLoader.java

+ 37
- 60
src/main/org/apache/tools/ant/AntClassLoader.java View File

@@ -43,8 +43,6 @@ import java.util.jar.Attributes.Name;
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 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
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 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
+ 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 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
*/
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 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
*/
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 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
}
}
} 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 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
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 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
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 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
* 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);
}


Loading…
Cancel
Save