com.acciente.commons.loader
Class JavaCompiledClassDefLoader

java.lang.Object
  extended by com.acciente.commons.loader.JavaCompiledClassDefLoader
All Implemented Interfaces:
ClassDefLoader

public class JavaCompiledClassDefLoader
extends Object
implements ClassDefLoader

A class definition loader that loads compiled Java class files.

Author:
Adinath Raveendra Raj

Constructor Summary
JavaCompiledClassDefLoader()
           
JavaCompiledClassDefLoader(File oCompiledDirectory)
           
 
Method Summary
 Set findClassNames(String[] asPackageNames, Pattern oClassNamePattern)
          Searches for class names that match the specified package root and regex.
 ClassDef getClassDef(String sClassName)
          Load the byte code and other information about the specified class
 File getCompiledDirectory()
          Returns the compiled directory from which this classloader loads java compiled files
 String getPackageNamePrefix()
          Returns the package name prefix set by a call to setPackageNamePrefix() or a null string if no package name prefix was ever set.
 ResourceDef getResourceDef(String sResourceName)
          Load the data and other information about the specified resource
 void setCompiledDirectory(File sCompiledDirectory)
          Sets the directory from which this classloader will attempt to java compiled files
 void setPackageNamePrefix(String sPackageNamePrefix)
          A package name prefix that represents the prefix that should be removed from the class name passed to loadClass() before attempting to convert the class's package name to a sub-directory off the specified compiled directory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JavaCompiledClassDefLoader

public JavaCompiledClassDefLoader()

JavaCompiledClassDefLoader

public JavaCompiledClassDefLoader(File oCompiledDirectory)
Method Detail

getClassDef

public ClassDef getClassDef(String sClassName)
                     throws ClassNotFoundException
Description copied from interface: ClassDefLoader
Load the byte code and other information about the specified class

Specified by:
getClassDef in interface ClassDefLoader
Parameters:
sClassName - the name of the class for which a definition is requested
Returns:
a class definition if this class definition loader is able to locate and successfully load the byte code for the class. If the class could not be located a null is returned, if the class was located but the load failed an exception is thrown.
Throws:
ClassNotFoundException - if this loader was able to locate the class definition was unable to sucessfully load it

getResourceDef

public ResourceDef getResourceDef(String sResourceName)
Description copied from interface: ClassDefLoader
Load the data and other information about the specified resource

Specified by:
getResourceDef in interface ClassDefLoader
Parameters:
sResourceName - the name of the class for which a definition is requested
Returns:
a resource definition if this class definition loader is able to locate and successfully load the data for the resource. If the resource could not be located a null is returned, if the resource was located but the load failed again a null is returned (since the getResource() and getResourceAsStreamMethods() do not have checked exceptions in their signature).

findClassNames

public Set findClassNames(String[] asPackageNames,
                          Pattern oClassNamePattern)
Description copied from interface: ClassDefLoader
Searches for class names that match the specified package root and regex. This method is expected to search and return a list of fully qualified classnames.

Specified by:
findClassNames in interface ClassDefLoader
Parameters:
asPackageNames - an array of starting package names to start the search, if the package names arg is null all package names are searched
oClassNamePattern - a java regex that defines the classnames that match the search
Returns:
a set of string classnames that match the specified regex

getCompiledDirectory

public File getCompiledDirectory()
Returns the compiled directory from which this classloader loads java compiled files

Returns:
a File object representing a directory path

setCompiledDirectory

public void setCompiledDirectory(File sCompiledDirectory)
Sets the directory from which this classloader will attempt to java compiled files

Parameters:
sCompiledDirectory - a File object representing a directory path

getPackageNamePrefix

public String getPackageNamePrefix()
Returns the package name prefix set by a call to setPackageNamePrefix() or a null string if no package name prefix was ever set.

Returns:
null or a package name without a terminating period (e.g: foo.bar and NOT foo.bar.)

setPackageNamePrefix

public void setPackageNamePrefix(String sPackageNamePrefix)
A package name prefix that represents the prefix that should be removed from the class name passed to loadClass() before attempting to convert the class's package name to a sub-directory off the specified compiled directory. For consistency this setting also causes the class loader to enfore that all the package name of classes loaded by this classloader start with the specified package name prefix. Following is an illustration of how is parameter may be used. Assume we have compiled files in a directory /project/foobar/src, lets also assume that we want to reliably restrict this class loader to only load compiled files in the folder /project/foobar/src/foobar/controller. So for so security we set the compiled directory of this classoader to /project/foobar/src/foobar/controller. Now if we ask this classloader to load the class foobar.controller.HelloWorld it would (unsuccessfully) try to look for a compiled file named: "/project/foobar/src/foobar/controller/foobar/controller/HelloWorld.class" computed using: CompiledDir="/project/foobar/src/foobar/controller" + InferredCompiledFilePath="/foobar/controller/HelloWorld.class" To get the correct behaviour in the above example the PackageNamePrefix of this class loader must be set to "foobar.controller". The prefix "foobar.controller" is then removed from the package name of class before using it to compute the compiled file path for the class foobar.controller.HelloWorld.

Parameters:
sPackageNamePrefix - null or a package name without a terminating period (e.g: foo.bar and NOT foo.bar.)


Copyright © 2008-2013 Acciente, LLC. All Rights Reserved.