org.openejb.util.proxy
Class ProxyClassLoader

java.lang.Object
  |
  +--java.lang.ClassLoader
        |
        +--java.security.SecureClassLoader
              |
              +--java.net.URLClassLoader
                    |
                    +--org.openejb.util.proxy.ProxyClassLoader

public class ProxyClassLoader
extends java.net.URLClassLoader

ClassLoader for dynamically generating byte-code and loading classes.


Inner Class Summary
(package private) static class ProxyClassLoader.ClassByteCode
          Wrapper for class to byte-code mappings.
 class ProxyClassLoader.ProxyByteCodeStreamHandler
           
 class ProxyClassLoader.ProxyByteCodeURLConnection
           
 
Inner classes inherited from class java.lang.ClassLoader
java.lang.ClassLoader.NativeLibrary
 
Fields inherited from class java.lang.ClassLoader
nocerts
 
Constructor Summary
ProxyClassLoader()
           
 
Method Summary
 void addURL(java.io.File directory)
           
protected static void cacheByteCode(java.lang.Class clazz, byte[] byteCode)
           
 java.lang.Class defineClass(java.lang.String name, byte[] byteCode)
          Converts an array of bytes into an instance of class Class.
 java.net.URL findResource(java.lang.String name)
          Finds the resource with the given name.
protected static byte[] getCachedByteCode(java.lang.Class clazz)
           
protected static byte[] getCachedByteCode(java.lang.String className)
          Find the byte-code for the specified class.
protected  java.lang.Class loadClass(java.lang.String name, boolean resolve)
          This method should not need to be overridden by subclasses.
 
Methods inherited from class java.net.URLClassLoader
addURL, definePackage, findClass, findResources, getPermissions, getURLs, newInstance, newInstance
 
Methods inherited from class java.security.SecureClassLoader
, defineClass
 
Methods inherited from class java.lang.ClassLoader
addClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findNative, findSystemClass, getBootstrapClassPath, getCallerClassLoader, getGetClassLoaderPerm, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, isAncestor, loadClass, loadLibrary, resolveClass, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProxyClassLoader

public ProxyClassLoader()
Method Detail

defineClass

public java.lang.Class defineClass(java.lang.String name,
                                   byte[] byteCode)
                            throws java.lang.ClassFormatError
Converts an array of bytes into an instance of class Class. Before the Class can be used it must be resolved.

This method assigns a default ProtectionDomain to the newly defined class. The ProtectionDomain contains the set of permissions granted when a call to Policy.getPolicy().getPermissions() is made with a Codesource of null,null. The default domain is created on the first invocation of defineClass, and re-used on subsequent calls.

To assign a specific ProtectionDomain to the class, use the defineClass method that takes a ProtectionDomain as one of its arguments.

Parameters:
name - the expected name of the class, or null if not known, using '.' and not '/' as the separator and without a trailing ".class" suffix.
b - the bytes that make up the class data. The bytes in positions off through off+len-1 should have the format of a valid class file as defined by the Java Virtual Machine Specification.
Returns:
the Class object that was created from the specified class data
Throws:
java.lang.ClassFormatError - if the data did not contain a valid class
java.lang.SecurityException - if an attempt is made to add this class to a package that contains classes that were signed by a different set of certificates then this class, which is unsigned.
See Also:
ClassLoader.loadClass(java.lang.String, boolean), ClassLoader.resolveClass(java.lang.Class), ProtectionDomain, java.security.Policy, CodeSource, SecureClassLoader

findResource

public java.net.URL findResource(java.lang.String name)
Finds the resource with the given name. Class loader implementations should override this method to specify where to find resources. Find the byte-code for the specified class. The proxy classes are generated at runtime and are created from a byte array and not a .class file. The byte array containing the byte-code is discarded by the VM after a Class is created using ClassLoader.defineClass(...). Calling Class.getResourceAsStream(proxyClassName) will return null because there is no .class definition in the file system. In this situation, it is the responsibility of the class loader (ProxyClassLoader) that loaded the class definition to implement ClassLoader.findResource(...) and return a URL that can be used to retreive the proxy class byte-code.
Overrides:
findResource in class java.net.URLClassLoader
Parameters:
name - the resource name
Returns:
a URL for reading the resource, or null if the resource could not be found
Since:
JDK1.2
See Also:
java.lang.ClassLoader.defineClass, java.lang.ClassLoader.getSystemResourceAsStream(), ProxyClassLoader, org.openejb.util.proxy.ProxyClassLoader.findResource

addURL

public void addURL(java.io.File directory)
            throws java.net.MalformedURLException

loadClass

protected java.lang.Class loadClass(java.lang.String name,
                                    boolean resolve)
                             throws java.lang.ClassNotFoundException
This method should not need to be overridden by subclasses. It is done here because of a bug in the VM. This method was overriden using the algorithm of it's superclass with one workaround that reliably calls the findClass method. No other changes should be made in this method.
Overrides:
loadClass in class java.lang.ClassLoader
Parameters:
name -  
resolve -  
Returns:
 
Throws:
java.lang.ClassNotFoundException -  

getCachedByteCode

protected static byte[] getCachedByteCode(java.lang.String className)
Find the byte-code for the specified class. The proxy classes are generated at runtime and are created from a byte array and not a .class file. The byte array containing the byte-code is discarded by the VM after a Class is created using ClassLoader.defineClass(...). Calling Class.getResourceAsStream(proxyClassName) will return null because there is no .class definition in the file system. In this situation, it is the responsibility of the class loader (ProxyClassLoader) that loaded the class definition to implement ClassLoader.findResource(...) and return a URL that can be used to retreive the proxy class byte-code.
Parameters:
className -  
Returns:
 
See Also:
java.lang.ClassLoader.defineClass, java.lang.ClassLoader.getSystemResourceAsStream(), ProxyClassLoader, org.openejb.util.proxy.ProxyClassLoader.findResource

getCachedByteCode

protected static byte[] getCachedByteCode(java.lang.Class clazz)

cacheByteCode

protected static void cacheByteCode(java.lang.Class clazz,
                                    byte[] byteCode)