classUtils.pack.util
Class ClassFinder

java.lang.Object
  extended by classUtils.pack.util.ClassFinder
All Implemented Interfaces:
ClassFinderInterface, DynamicClassFinderInterface, DynamicResourceFinderInterfaceInterface, ResourceFinderInterface

public class ClassFinder
extends java.lang.Object
implements ClassFinderInterface, ResourceFinderInterface, DynamicClassFinderInterface, DynamicResourceFinderInterfaceInterface

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

It works only when classes are loaded from a filesystem.

Version:
1.4
Author:
Cristiano Sadun

Field Summary
 ClassPathBean classPathBean
          The application class path used by this classFileFinder
 
Constructor Summary
ClassFinder(ClassPathBean cpb)
          Creates a classfinder which looks (besides system directories) in the given class path
 
Method Summary
 void addClassPathEntry(java.lang.String entry)
           
 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
 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
 java.lang.String getClassPath()
           
 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.
 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.
 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.
 void setClassPath(java.lang.String classPath)
           
static void testClassFinder()
           
 void testFindClassFile()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

classPathBean

public ClassPathBean classPathBean
The application class path used by this classFileFinder

Constructor Detail

ClassFinder

public ClassFinder(ClassPathBean cpb)
Creates a classfinder which looks (besides system directories) in the given class path

Parameters:
cpb - the search class path
Method Detail

getSupportedLoadingScheme

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

Specified by:
getSupportedLoadingScheme in interface ClassFinderInterface
Specified by:
getSupportedLoadingScheme in interface ResourceFinderInterface
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 ClassFinderInterface
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

testFindClassFile

public void testFindClassFile()
                       throws java.io.IOException,
                              java.lang.ClassNotFoundException
Throws:
java.io.IOException
java.lang.ClassNotFoundException

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 ClassFinderInterface
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

searchJars

public 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

getClassBytes

public byte[] getClassBytes(java.lang.String className)
                     throws java.io.IOException,
                            java.lang.ClassNotFoundException
Description copied from interface: ClassFinderInterface
Returns a byte array with the bytecode for the class

Specified by:
getClassBytes in interface ClassFinderInterface
Parameters:
className - the name of the class to find
Returns:
byte[] the bytecode for the class
Throws:
java.io.IOException - if a problem arises while loading the bytecode
java.lang.ClassNotFoundException - if the class definition cannot be found
See Also:
ClassFinderInterface.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 ResourceFinderInterface
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

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 ResourceFinderInterface
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

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.

Specified by:
getResourceBytes in interface ResourceFinderInterface
Parameters:
resource - the resource name
Returns:
byte[] the bytes of the resource
Throws:
java.io.IOException - if a problem arises accessing the resource
See Also:
ResourceFinderInterface.getResourceBytes(String)

testClassFinder

public static void testClassFinder()

addClassPathEntry

public void addClassPathEntry(java.lang.String entry)
Specified by:
addClassPathEntry in interface DynamicClassFinderInterface
Specified by:
addClassPathEntry in interface DynamicResourceFinderInterfaceInterface

setClassPath

public void setClassPath(java.lang.String classPath)
Specified by:
setClassPath in interface DynamicClassFinderInterface
Specified by:
setClassPath in interface DynamicResourceFinderInterfaceInterface

getClassPath

public java.lang.String getClassPath()
Specified by:
getClassPath in interface DynamicClassFinderInterface
Specified by:
getClassPath in interface DynamicResourceFinderInterfaceInterface
See Also:
DynamicClassFinderInterface.getClassPath()

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.

Returns:
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.


main

public static void main(java.lang.String[] args)
                 throws java.io.IOException,
                        java.lang.ClassNotFoundException
Throws:
java.io.IOException
java.lang.ClassNotFoundException