com.deltax.util
Class JDK12ClassFileFinder

java.lang.Object
  extended bycom.deltax.util.JDK12ClassFileFinder
All Implemented Interfaces:
ClassFileFinder, ResourceFileFinder
Direct Known Subclasses:
DynamicJDK12ClassFileFinder

public class JDK12ClassFileFinder
extends java.lang.Object
implements ClassFileFinder, ResourceFileFinder

This class emulates JDK 1.2 behaviour for finding class, providing direct access to the files / streams.

Of course, it works only when classes are actually loaded from a filesystem.

Version:
1.4
Author:
Cristiano Sadun

Field Summary
protected  java.util.HashMap classCache
          Maps class names to Object[2] arrays containing file and classfile (File, byte[])
protected  boolean classCacheOn
           
protected  java.lang.String classPath
          The application class path used by this classFileFinder
protected  java.util.Enumeration classPathDirs
          Caches results of getClassPathDirs() If the classpath changes, it must be invalidated.
protected  java.util.HashSet missingFilesNotified
           
 
Constructor Summary
JDK12ClassFileFinder()
          Creates a classfinder which looks (besides system directories) in $java.class.path
JDK12ClassFileFinder(java.lang.String classPath)
          Creates a classfinder which looks (besides system directories) in the given class path
 
Method Summary
protected  java.lang.String classNameToEntry(java.lang.String className)
          Converts a fully qualified externalized java class name into a zip file entry.
protected  java.lang.String classNameToPath(java.lang.String className)
          Converts a fully qualified externalized java class name into a relative file path.
 void clearCorruptFiles()
          Empty the set of files considered corrupted since they failed to open when the finder was searching for classes, so that subsequent work will not ignore them.
 java.io.File findClassFile(java.lang.String className)
          This method emulates 1.2 behaviour for class finding - which means: - it first searches into the $java.home/lib/ and $java.home/lib/*.jar; - then in $java.ext.dirs/*.jar; - eventually in $java.class.path
protected  java.io.File[] findJarsInPath(java.io.File path)
          Find the JAR files in the given path
 java.io.File findResourceFile(java.lang.String resource)
          Find a resource in the filesystem.
 byte[] getBytes(java.lang.String className)
          Return the byte array for the class
 byte[] getClassBytes(java.lang.String className)
          Returns a byte array with the bytecode for the class * @param className the name of the class to find * @return byte[] the bytecode for the class * @throws IOException if a problem arises while loading the bytecode * @throws ClassNotFoundException if the class definition cannot be found
protected  java.util.Enumeration getClassPathDirs()
           
 java.util.Set getCorruptFiles()
          Return the set, possibly empty, of files which couldn't be opened when the finder was searching for classes
 byte[] getResourceBytes(java.lang.String resource)
          Return a byte array with the bytes for the resource, or null if the resource cannot be found
 java.lang.String getSupportedLoadingScheme()
          Returns the supported loading scheme
 boolean isClassCacheOn()
          Returns the classCacheOn.
 boolean isJar(java.io.File f)
          Just checks if the file ends with .jar
static void main(java.lang.String[] args)
           
 java.io.InputStream openClass(java.lang.String className)
          Open class data.
protected  java.io.InputStream openClassInJar(java.lang.String className, java.io.File jarFile)
          Open class data in a JAR file
 java.io.InputStream openResource(java.lang.String resource)
          Return the stream associated to a resource in the filesystem, or null if such resource cannot be found.
protected  java.io.File searchJars(java.lang.String className, java.io.File[] jars)
          Searches jar files for a given class path
 void setClassCacheOn(boolean classCacheOn)
          Sets the classCacheOn.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

classPath

protected java.lang.String classPath
The application class path used by this classFileFinder


classPathDirs

protected java.util.Enumeration classPathDirs
Caches results of getClassPathDirs() If the classpath changes, it must be invalidated.


missingFilesNotified

protected java.util.HashSet missingFilesNotified

classCache

protected java.util.HashMap classCache
Maps class names to Object[2] arrays containing file and classfile (File, byte[])


classCacheOn

protected boolean classCacheOn
Constructor Detail

JDK12ClassFileFinder

public JDK12ClassFileFinder(java.lang.String classPath)
Creates a classfinder which looks (besides system directories) in the given class path

Parameters:
classPath - the search class path

JDK12ClassFileFinder

public JDK12ClassFileFinder()
Creates a classfinder which looks (besides system directories) in $java.class.path

Method Detail

getSupportedLoadingScheme

public java.lang.String getSupportedLoadingScheme()
Returns the supported loading scheme

Specified by:
getSupportedLoadingScheme in interface ClassFileFinder
Returns:
a description string

openClass

public java.io.InputStream openClass(java.lang.String className)
                              throws java.io.IOException,
                                     java.lang.ClassNotFoundException
Open class data. The input stream reads exactly and only the class byte data.

Specified by:
openClass in interface ClassFileFinder
Parameters:
className - the name of the class to find
Throws:
java.lang.ClassNotFoundException - if the class is not found
java.io.IOException - if an I/O Exception occurs

findClassFile

public java.io.File findClassFile(java.lang.String className)
                           throws java.io.IOException,
                                  java.lang.ClassNotFoundException
This method emulates 1.2 behaviour for class finding - which means: - it first searches into the $java.home/lib/ and $java.home/lib/*.jar; - then in $java.ext.dirs/*.jar; - eventually in $java.class.path

Specified by:
findClassFile in interface ClassFileFinder
Parameters:
className - the name of the class to find
Returns:
the File object for the class file; this can be a .class file, or a JAR file containing the class
Throws:
java.lang.ClassNotFoundException - if the class is not found
java.io.IOException - if an I/O Exception occurs

getBytes

public byte[] getBytes(java.lang.String className)
                throws java.io.IOException,
                       java.lang.ClassNotFoundException
Return the byte array for the class

Throws:
java.lang.ClassNotFoundException - if the class is not found in the JAR file
java.io.IOException - if an I/O Exception occurs

isJar

public boolean isJar(java.io.File f)
Just checks if the file ends with .jar

Parameters:
f - the file to check
Returns:
true if the file name ends with .jar

openClassInJar

protected java.io.InputStream openClassInJar(java.lang.String className,
                                             java.io.File jarFile)
                                      throws java.io.IOException,
                                             java.lang.ClassNotFoundException
Open class data in a JAR file

Returns:
an input stream to the class byte data in JAR
Throws:
java.lang.ClassNotFoundException - if the class is not found in the JAR file
java.io.IOException - if an I/O Exception occurs
java.io.FileNotFoundException - if the JAR file does not exist

searchJars

protected java.io.File searchJars(java.lang.String className,
                                  java.io.File[] jars)
                           throws java.io.IOException,
                                  java.lang.ClassNotFoundException
Searches jar files for a given class path

Parameters:
className - the name of the class to find
jars - an array of JAR file objects
Returns:
the jar file containing the class
Throws:
java.lang.ClassNotFoundException - if the class is not found
java.io.IOException - if an I/O Exception occurs

findJarsInPath

protected java.io.File[] findJarsInPath(java.io.File path)
Find the JAR files in the given path

Parameters:
path - the path to search in
Returns:
an array (may be zero-length) with the JAR file names

classNameToPath

protected java.lang.String classNameToPath(java.lang.String className)
Converts a fully qualified externalized java class name into a relative file path.

Parameters:
className - the name of the class to find
Returns:
the system-dependent path name for the class

classNameToEntry

protected java.lang.String classNameToEntry(java.lang.String className)
Converts a fully qualified externalized java class name into a zip file entry. (ZIP format uses always UNIX slash)

Parameters:
className - the name of the class to find
Returns:
the JAR entry name for the class

getClassPathDirs

protected java.util.Enumeration getClassPathDirs()

getClassBytes

public byte[] getClassBytes(java.lang.String className)
                     throws java.io.IOException,
                            java.lang.ClassNotFoundException
Description copied from interface: ClassFileFinder
Returns a byte array with the bytecode for the class * @param className the name of the class to find * @return byte[] the bytecode for the class * @throws IOException if a problem arises while loading the bytecode * @throws ClassNotFoundException if the class definition cannot be found

Specified by:
getClassBytes in interface ClassFileFinder
Throws:
java.io.IOException
java.lang.ClassNotFoundException
See Also:
ClassFileFinder.getClassBytes(java.lang.String)

openResource

public java.io.InputStream openResource(java.lang.String resource)
                                 throws java.io.IOException
Return the stream associated to a resource in the filesystem, or null if such resource cannot be found.

The returned stream is not buffered.

Specified by:
openResource in interface ResourceFileFinder
Parameters:
resource - the resource path
Returns:
a stream to the resource, or null if the resource cannot be found
Throws:
java.io.IOException - if a problem arises accessing the resource
See Also:
com.deltax.util.ResourceFileFinder#findResource(java.lang.String)

findResourceFile

public java.io.File findResourceFile(java.lang.String resource)
                              throws java.io.IOException
Find a resource in the filesystem.

This class implements findResource by operating as findClassFile() and attempting to locate the resource within directories or JAR files therein.

Resource loading from other than filesystem is not supported.

Specified by:
findResourceFile in interface ResourceFileFinder
Parameters:
resource - the resource path
Returns:
a stream to the resource, or null if the resource cannot be found
Throws:
java.io.IOException - if a problem arises accessing the resource
See Also:
com.deltax.util.ResourceFileFinder#findResource(java.lang.String)

getResourceBytes

public byte[] getResourceBytes(java.lang.String resource)
                        throws java.io.IOException
Return a byte array with the bytes for the resource, or null if the resource cannot be found. * @see com.deltax.util.ResourceFileFinder#getResourceBytes(String)

Specified by:
getResourceBytes in interface ResourceFileFinder
Throws:
java.io.IOException

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Throws:
java.lang.Exception

isClassCacheOn

public boolean isClassCacheOn()
Returns the classCacheOn.

Returns:
boolean

setClassCacheOn

public void setClassCacheOn(boolean classCacheOn)
Sets the classCacheOn. The class cache speeds up searching in jars by preserving classfile-classname-jar associations, but may lead to substantial growth of the amount of memory needed.

Parameters:
classCacheOn - The classCacheOn to set

getCorruptFiles

public java.util.Set getCorruptFiles()
Return the set, possibly empty, of files which couldn't be opened when the finder was searching for classes. * @return the set, possibly empty, of files which couldn't be opened.


clearCorruptFiles

public void clearCorruptFiles()
Empty the set of files considered corrupted since they failed to open when the finder was searching for classes, so that subsequent work will not ignore them.