com.sun.enterprise.loader
Class ASURLClassLoader

java.lang.Object
  extended by java.lang.ClassLoader
      extended by java.security.SecureClassLoader
          extended by java.net.URLClassLoader
              extended by com.sun.enterprise.loader.ASURLClassLoader
All Implemented Interfaces:
JasperAdapter, InstrumentableClassLoader, org.jvnet.hk2.component.PreDestroy

public class ASURLClassLoader
extends java.net.URLClassLoader
implements JasperAdapter, InstrumentableClassLoader, org.jvnet.hk2.component.PreDestroy

Class loader used by the ejbs of an application or stand alone module. This class loader also keeps cache of not found classes and resources.

Since:
JDK 1.4
Author:
Nazrul Islam, Kenneth Saks, Sivakumar Thyagarajan

Nested Class Summary
protected  class ASURLClassLoader.SentinelInputStream
          Wraps all InputStreams returned by this class loader to report when a finalizer is run before the stream has been closed.
protected static class ASURLClassLoader.URLEntry
          URL entry - keeps track of the url resources.
 
Constructor Summary
ASURLClassLoader()
          Constructor.
ASURLClassLoader(java.lang.ClassLoader parent)
          Constructor.
 
Method Summary
 void addTransformer(java.lang.instrument.ClassFileTransformer transformer)
           
 void addURL(java.net.URL url)
          Appends the specified URL to the list of URLs to search for classes and resources.
 void appendURL(java.io.File file)
          Adds a URL to the search list, based on the specified File.
 void appendURL(java.net.URL url)
          Add a url to the list of urls we search for a class's bytecodes.
 java.lang.ClassLoader copy()
          Create a new instance of a sibling classloader
 void done()
          This method should be called to free up the resources.
protected  java.lang.Class findClass(java.lang.String name)
          THREAD SAFETY: what happens when more than one thread requests the same class and thus works on the same classData? Or defines the same package? Maybe the same work just gets done twice, and that's all.
protected  com.sun.enterprise.loader.ASURLClassLoader.ClassData findClassData(java.lang.String name)
          This method is responsible for locating the url from the class bytes have to be read and reading the bytes.
 java.net.URL findResource(java.lang.String name)
           
 java.util.Enumeration<java.net.URL> findResources(java.lang.String name)
          Returns an enumeration of java.net.URL objects representing all the resources with the given name.
protected  java.lang.String getClassLoaderName()
           
 java.lang.String getClasspath()
          Returns all the "file" protocol resources of this ASURLClassLoader, concatenated to a classpath string.
 java.io.InputStream getResourceAsStream(java.lang.String name)
           
 java.net.URL[] getURLs()
          Returns the urls of this class loader.
 boolean isDone()
           
 void preDestroy()
           
 void refresh()
          Refreshes the memory of the class loader.
 java.lang.String toString()
          Returns a string representation of this class loader.
 
Methods inherited from class java.net.URLClassLoader
definePackage, getPermissions, newInstance, newInstance
 
Methods inherited from class java.security.SecureClassLoader
defineClass, defineClass
 
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findSystemClass, getPackage, getPackages, getParent, getResource, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, loadClass, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ASURLClassLoader

public ASURLClassLoader()
Constructor.


ASURLClassLoader

public ASURLClassLoader(java.lang.ClassLoader parent)
Constructor.

Parameters:
parent - parent class loader
Method Detail

isDone

public boolean isDone()

preDestroy

public void preDestroy()
Specified by:
preDestroy in interface org.jvnet.hk2.component.PreDestroy

done

public void done()
This method should be called to free up the resources. It helps garbage collection. Must be synchronized for: (a) visibility of variables (b) race condition while checking 'doneCalled' (c) only one caller should close the zip files, (d) done should occur only once and set the flag when done. (e) shoudl not return 'true' when a previous thread might still be in the process of executing the method.


appendURL

public void appendURL(java.io.File file)
               throws java.io.IOException
Adds a URL to the search list, based on the specified File.

This variant of the method makes sure that the URL is valid, in particular encoding special characters (such as blanks) in the file path.

Parameters:
file - the File to use in creating the URL
Throws:
java.io.IOException - in case of errors converting the file to a URL

addURL

public void addURL(java.net.URL url)
Appends the specified URL to the list of URLs to search for classes and resources.

Overrides:
addURL in class java.net.URLClassLoader
Parameters:
url - the URL to be added to the search path of URLs

appendURL

public void appendURL(java.net.URL url)
Add a url to the list of urls we search for a class's bytecodes.

Parameters:
url - url to be added

getURLs

public java.net.URL[] getURLs()
Returns the urls of this class loader. Method is 'synchronized' to avoid the thread-unsafe null-check idiom idiom, also protects the caller from simultaneous changes while iterating, by returning a URL[] (copy) rather than the original. Also protects against changes to 'urlSet' while iterating over it.

Specified by:
getURLs in interface JasperAdapter
Overrides:
getURLs in class java.net.URLClassLoader
Returns:
the urls of this class loader or an empty array

getClasspath

public java.lang.String getClasspath()
Returns all the "file" protocol resources of this ASURLClassLoader, concatenated to a classpath string. Notice that this method is called by the setClassPath() method of org.apache.catalina.loader.WebappLoader, since this ASURLClassLoader does not extend off of URLClassLoader.

Returns:
Classpath string containing all the "file" protocol resources of this ASURLClassLoader

refresh

public void refresh()
             throws java.io.IOException
Refreshes the memory of the class loader. This involves clearing the not-found cahces and recreating the hash tables for the URLEntries that record the files accessible for each.

Code that creates an ASURLClassLoader and then adds files to a directory that is in the loader's classpath should invoke this method after the new file(s) have been added in order to update the class loader's data structures which optimize class and resource searches.

Throws:
java.io.IOException - in case of errors refreshing the cache

addTransformer

public void addTransformer(java.lang.instrument.ClassFileTransformer transformer)
Specified by:
addTransformer in interface InstrumentableClassLoader

copy

public java.lang.ClassLoader copy()
Create a new instance of a sibling classloader

Specified by:
copy in interface InstrumentableClassLoader
Returns:
a new instance of a class loader that has the same visibility as this class loader

findResource

public java.net.URL findResource(java.lang.String name)
Overrides:
findResource in class java.net.URLClassLoader

findResources

public java.util.Enumeration<java.net.URL> findResources(java.lang.String name)
                                                  throws java.io.IOException
Returns an enumeration of java.net.URL objects representing all the resources with the given name. This method is synchronized to avoid (a) race condition checking 'doneCalled', (b) changes to contents or length of 'resourcesList' and/or 'notFoundResources' while iterating over them, (c) thread visibility to all of the above.

Overrides:
findResources in class java.net.URLClassLoader
Throws:
java.io.IOException

findClass

protected java.lang.Class findClass(java.lang.String name)
                             throws java.lang.ClassNotFoundException
THREAD SAFETY: what happens when more than one thread requests the same class and thus works on the same classData? Or defines the same package? Maybe the same work just gets done twice, and that's all. CAUTION: this method might be overriden, and subclasses must be cautious (also) about thread safety.

Overrides:
findClass in class java.net.URLClassLoader
Throws:
java.lang.ClassNotFoundException

findClassData

protected com.sun.enterprise.loader.ASURLClassLoader.ClassData findClassData(java.lang.String name)
                                                                      throws java.lang.ClassNotFoundException
This method is responsible for locating the url from the class bytes have to be read and reading the bytes. It does not actually define the Class object.

To preclude a race condition on checking 'doneCalled', as well as transient errors if done() is called while running, this method is 'synchronized'.

Parameters:
name - class name in java.lang.Object format
Returns:
class bytes as well protection domain information
Throws:
java.lang.ClassNotFoundException

getClassLoaderName

protected java.lang.String getClassLoaderName()

toString

public java.lang.String toString()
Returns a string representation of this class loader.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of this class loader

getResourceAsStream

public java.io.InputStream getResourceAsStream(java.lang.String name)
Overrides:
getResourceAsStream in class java.lang.ClassLoader


Copyright © 2012 GlassFish Community. All Rights Reserved.