aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/javassist/ClassPoolTail.java
diff options
context:
space:
mode:
authorchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>2003-12-25 13:52:32 +0000
committerchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>2003-12-25 13:52:32 +0000
commit9c01e324314959bcf6609bd26b5d098f5d3417e2 (patch)
tree97685651ada345c980a569f59676beae29799cdf /src/main/javassist/ClassPoolTail.java
parent1a1b9d2feccc15e1bd1a26ff7a7709630d478fe8 (diff)
downloadjavassist-9c01e324314959bcf6609bd26b5d098f5d3417e2.tar.gz
javassist-9c01e324314959bcf6609bd26b5d098f5d3417e2.zip
Implemented several methods (e.g. CtClass#getURL) to support security
policies based on code source. git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@59 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'src/main/javassist/ClassPoolTail.java')
-rw-r--r--src/main/javassist/ClassPoolTail.java84
1 files changed, 75 insertions, 9 deletions
diff --git a/src/main/javassist/ClassPoolTail.java b/src/main/javassist/ClassPoolTail.java
index 64560147..5e6898e8 100644
--- a/src/main/javassist/ClassPoolTail.java
+++ b/src/main/javassist/ClassPoolTail.java
@@ -16,7 +16,9 @@
package javassist;
import java.io.*;
-import java.util.zip.*;
+import java.util.jar.*;
+import java.net.MalformedURLException;
+import java.net.URL;
import java.util.Hashtable;
final class ClassPathList {
@@ -50,6 +52,11 @@ final class SystemClassPath implements ClassPath {
return thisClass.getResourceAsStream(jarname);
}
+ public URL find(String classname) {
+ String jarname = "/" + classname.replace('.', '/') + ".class";
+ return thisClass.getResource(jarname);
+ }
+
public void close() {}
public String toString() {
@@ -77,6 +84,21 @@ final class DirClassPath implements ClassPath {
return null;
}
+ public URL find(String classname) {
+ char sep = File.separatorChar;
+ String filename = directory + sep
+ + classname.replace('.', sep) + ".class";
+ File f = new File(filename);
+ if (f.exists())
+ try {
+ return f.getCanonicalFile().toURL();
+ }
+ catch (MalformedURLException e) {}
+ catch (IOException e) {}
+
+ return null;
+ }
+
public void close() {}
public String toString() {
@@ -86,14 +108,16 @@ final class DirClassPath implements ClassPath {
final class JarClassPath implements ClassPath {
- ZipFile jarfile;
+ JarFile jarfile;
+ String jarfileURL;
JarClassPath(String pathname) throws NotFoundException {
try {
- jarfile = new ZipFile(pathname);
+ jarfile = new JarFile(pathname);
+ jarfileURL = new File(pathname).getCanonicalFile()
+ .toURL().toString();
return;
}
- catch (ZipException e) {}
catch (IOException e) {}
throw new NotFoundException(pathname);
}
@@ -103,18 +127,29 @@ final class JarClassPath implements ClassPath {
{
try {
String jarname = classname.replace('.', '/') + ".class";
- ZipEntry ze = jarfile.getEntry(jarname);
- if (ze != null)
- return jarfile.getInputStream(ze);
+ JarEntry je = jarfile.getJarEntry(jarname);
+ if (je != null)
+ return jarfile.getInputStream(je);
else
return null; // not found
}
- catch (ZipException e) {}
catch (IOException e) {}
throw new NotFoundException("broken jar file?: "
+ jarfile.getName());
}
+ public URL find(String classname) {
+ String jarname = classname.replace('.', '/') + ".class";
+ JarEntry je = jarfile.getJarEntry(jarname);
+ if (je != null)
+ try {
+ return new URL("jar:" + jarfileURL + "!/" + jarname);
+ }
+ catch (MalformedURLException e) {}
+
+ return null; // not found
+ }
+
public void close() {
try {
jarfile.close();
@@ -183,12 +218,20 @@ final class ClassPoolTail extends ClassPool {
}
void checkClassName(String classname) throws NotFoundException {
+ if (find(classname) == null)
+ throw new NotFoundException(classname);
+ }
+
+ /* slower version.
+
+ void checkClassName(String classname) throws NotFoundException {
InputStream fin = openClassfile(classname);
try {
fin.close();
}
- catch (IOException e) { /* ignore */ }
+ catch (IOException e) {}
}
+ */
public synchronized ClassPath insertClassPath(ClassPath cp) {
pathList = new ClassPathList(cp, pathList);
@@ -313,6 +356,29 @@ final class ClassPoolTail extends ClassPool {
}
/**
+ * Obtains the URL of the class file specified by classname.
+ *
+ * @param classname a fully-qualified class name.
+ * @return null if the class file could not be found.
+ */
+ public URL find(String classname) {
+ if (packages.get(classname) != null)
+ return null;
+
+ ClassPathList list = pathList;
+ URL url = null;
+ while (list != null) {
+ url = list.path.find(classname);
+ if (url == null)
+ list = list.next;
+ else
+ return url;
+ }
+
+ return null;
+ }
+
+ /**
* Reads an input stream until it reaches the end.
*
* @return the contents of that input stream