diff options
author | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2003-12-25 13:52:32 +0000 |
---|---|---|
committer | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2003-12-25 13:52:32 +0000 |
commit | 9c01e324314959bcf6609bd26b5d098f5d3417e2 (patch) | |
tree | 97685651ada345c980a569f59676beae29799cdf /src | |
parent | 1a1b9d2feccc15e1bd1a26ff7a7709630d478fe8 (diff) | |
download | javassist-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')
-rw-r--r-- | src/main/javassist/ByteArrayClassPath.java | 20 | ||||
-rw-r--r-- | src/main/javassist/ClassPath.java | 10 | ||||
-rw-r--r-- | src/main/javassist/ClassPool.java | 11 | ||||
-rw-r--r-- | src/main/javassist/ClassPoolTail.java | 84 | ||||
-rw-r--r-- | src/main/javassist/CtClass.java | 10 | ||||
-rw-r--r-- | src/main/javassist/CtClassType.java | 9 | ||||
-rw-r--r-- | src/main/javassist/LoaderClassPath.java | 15 | ||||
-rw-r--r-- | src/main/javassist/URLClassPath.java | 39 |
8 files changed, 181 insertions, 17 deletions
diff --git a/src/main/javassist/ByteArrayClassPath.java b/src/main/javassist/ByteArrayClassPath.java index 5eed3d1a..9125646a 100644 --- a/src/main/javassist/ByteArrayClassPath.java +++ b/src/main/javassist/ByteArrayClassPath.java @@ -16,6 +16,8 @@ package javassist; import java.io.*; +import java.net.URL; +import java.net.MalformedURLException; /** * A <code>ByteArrayClassPath</code> contains bytes that is served as @@ -68,7 +70,7 @@ public class ByteArrayClassPath implements ClassPath { } /** - * Opens a class file. + * Opens the class file. */ public InputStream openClassfile(String classname) { if(this.classname.equals(classname)) @@ -76,4 +78,20 @@ public class ByteArrayClassPath implements ClassPath { else return null; } + + /** + * Obtains the URL. + */ + public URL find(String classname) { + if(this.classname.equals(classname)) { + String cname = classname.replace('.', '/') + ".class"; + try { + // return new File(cname).toURL(); + return new URL("file:/ByteArrayClassPath/" + cname); + } + catch (MalformedURLException e) {} + } + + return null; + } } diff --git a/src/main/javassist/ClassPath.java b/src/main/javassist/ClassPath.java index f5578e21..f05713a8 100644 --- a/src/main/javassist/ClassPath.java +++ b/src/main/javassist/ClassPath.java @@ -16,6 +16,7 @@ package javassist; import java.io.InputStream; +import java.net.URL; /** * <code>ClassPath</code> is an interface implemented by objects @@ -50,6 +51,15 @@ public interface ClassPath { InputStream openClassfile(String classname) throws NotFoundException; /** + * Returns the uniform resource locator (URL) of the class file + * with the specified name. + * + * @param classname a fully-qualified class name. + * @return null if the specified class file could not be found. + */ + URL find(String classname); + + /** * This method is invoked when the <code>ClassPath</code> object is * detached from the search path. It will be an empty method in most of * classes. diff --git a/src/main/javassist/ClassPool.java b/src/main/javassist/ClassPool.java index 394c8410..ac4b5a08 100644 --- a/src/main/javassist/ClassPool.java +++ b/src/main/javassist/ClassPool.java @@ -16,6 +16,7 @@ package javassist; import java.io.*; +import java.net.URL; import java.util.Hashtable; /** @@ -747,6 +748,16 @@ public class 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) { + return source.find(classname); + } + + /** * Appends the system search path to the end of the * search path. The system search path * usually includes the platform library, extension 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 diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index b4f4e26a..228f31b8 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -20,6 +20,7 @@ import java.io.IOException; import javassist.bytecode.*; import java.util.Collection; import javassist.expr.ExprEditor; +import java.net.URL; // Subclasses of CtClass: CtClassType, CtPrimitiveType, and CtArray @@ -200,6 +201,13 @@ public abstract class CtClass { public ClassFile getClassFile2() { return null; } /** + * Returns the uniform resource locator (URL) of the class file. + */ + public URL getURL() throws NotFoundException { + throw new NotFoundException(getName()); + } + + /** * Returns true if the definition of the class has been modified. */ public boolean isModified() { return false; } @@ -859,7 +867,7 @@ public abstract class CtClass { ClassPool cp = getClassPool(); if (cp == null) throw new CannotCompileException( - "no ClassPool found. not a class?"); + getName() + ": no ClassPool found. not a class?"); else return cp; } diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java index 943f9ea9..c2c4cf20 100644 --- a/src/main/javassist/CtClassType.java +++ b/src/main/javassist/CtClassType.java @@ -24,6 +24,7 @@ import java.io.InputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; +import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.Hashtable; @@ -126,6 +127,14 @@ class CtClassType extends CtClass { public ClassPool getClassPool() { return classPool; } + public URL getURL() throws NotFoundException { + URL url = classPool.find(getName()); + if (url == null) + throw new NotFoundException(getName()); + else + return url; + } + public boolean isModified() { return wasChanged; } public boolean isFrozen() { return wasFrozen; } diff --git a/src/main/javassist/LoaderClassPath.java b/src/main/javassist/LoaderClassPath.java index e6d83836..071b2fc9 100644 --- a/src/main/javassist/LoaderClassPath.java +++ b/src/main/javassist/LoaderClassPath.java @@ -16,6 +16,7 @@ package javassist; import java.io.InputStream; +import java.net.URL; import java.lang.ref.WeakReference; /** @@ -65,6 +66,20 @@ public class LoaderClassPath implements ClassPath { } /** + * Obtains the URL of the specified class file. + * + * @return null if the class file could not be found. + */ + public URL find(String classname) { + String cname = classname.replace('.', '/') + ".class"; + ClassLoader cl = (ClassLoader)clref.get(); + if (cl == null) + return null; // not found + else + return cl.getResource(cname); + } + + /** * Closes this class path. */ public void close() { diff --git a/src/main/javassist/URLClassPath.java b/src/main/javassist/URLClassPath.java index b455f381..745f3208 100644 --- a/src/main/javassist/URLClassPath.java +++ b/src/main/javassist/URLClassPath.java @@ -45,6 +45,7 @@ public class URLClassPath implements ClassPath { * @param port port number * @param directory directory name ending with "/". * It can be "/" (root directory). + * It must start with "/". * @param packageName package name. */ public URLClassPath(String host, int port, @@ -62,19 +63,44 @@ public class URLClassPath implements ClassPath { /** * Opens a class file with http. * - * @return null if the class file is not found. + * @return null if the class file could not be found. */ public InputStream openClassfile(String classname) { try { - if (packageName == null || classname.startsWith(packageName)) { - String jarname - = directory + classname.replace('.', '/') + ".class"; - URLConnection con = fetchClass0(hostname, port, jarname); + URLConnection con = openClassfile0(classname); + if (con != null) return con.getInputStream(); + } + catch (IOException e) {} + return null; // not found + } + + private URLConnection openClassfile0(String classname) throws IOException { + if (packageName == null || classname.startsWith(packageName)) { + String jarname + = directory + classname.replace('.', '/') + ".class"; + return fetchClass0(hostname, port, jarname); + } + else + return null; // not found + } + + /** + * Returns the URL. + * + * @return null if the class file could not be obtained. + */ + public URL find(String classname) { + try { + URLConnection con = openClassfile0(classname); + InputStream is = con.getInputStream(); + if (is != null) { + is.close(); + return con.getURL(); } } catch (IOException e) {} - return null; // not found + return null; } /** @@ -89,6 +115,7 @@ public class URLClassPath implements ClassPath { * @param port port number * @param directory directory name ending with "/". * It can be "/" (root directory). + * It must start with "/". * @param classname fully-qualified class name */ public static byte[] fetchClass(String host, int port, |