net.yagga.util
Class MetaJarClassLoader

java.lang.Object
  |
  +--java.lang.ClassLoader
        |
        +--net.yagga.util.MetaJarClassLoader

public class MetaJarClassLoader
extends java.lang.ClassLoader

A Meta class loader for loading jar files in many situations. It is interesting that the class, by using the MetajarResources class can load jar classes from Jars in the classpath


 ./Test.class
 ./net/yagga/util/MetaJarClassLoader.class
 ./sample.jar
   +- Difficult.class
 
In this simple case one can read the class Difficult from the file "sample.jar" via this class, in the main method of the Test class. This case is more or less the same as seen in JavaWorld's tip 70 (JarClassLoader). The class reads the specified Jar entry and returns the byte array and then instantiate an object of the Class using the bytes. The trick is a bit more complicated if we are in such a case, executing the jar file named "all.jar":

 ./all.jar
   +- Test.class
   +- net/yagga/util/MetaJarClassLoader.class
   +- sample.jar
     +- Difficult.class
 
In such a case JarClassLoader does not work. Instead we use the ability of MetJarResources to load bytes also from jars that are enclosedin the top-level (executing) jar.

Author:
Walter Gamba
See Also:
JavaWorld's tip 70, MetaJarResources

Inner classes inherited from class java.lang.ClassLoader
java.lang.ClassLoader.NativeLibrary
 
Field Summary
private  java.lang.String jarFile
           
private  MetaJarResources metaJarResources
           
 
Fields inherited from class java.lang.ClassLoader
bootstrapClassPath, classes, defaultDomain, defaultPermissions, domains, getClassLoaderPerm, initialized, loadedLibraryNames, nativeLibraries, nativeLibraryContext, nocerts, package2certs, packages, parent, scl, sclSet, sys_paths, systemNativeLibraries, usr_paths
 
Constructor Summary
MetaJarClassLoader(java.lang.String jFile)
          Construct the class loader givena a Jar File.
 
Method Summary
protected  java.lang.Class findClass(java.lang.String className)
          Method that actually reads the class from raw bytes.
 java.lang.String getActualJarName()
          Return the actual jar name.
 java.lang.String getMainClassName()
          Returns the name of the jar file main class, or null if no "Main-Class" manifest attributes was defined.
 void invokeClass(java.lang.String name, java.lang.String[] args)
          Invokes the application in this jar file given the name of the main class and an array of arguments.
 java.lang.Object invokeMethod(java.lang.String className, java.lang.String method, java.lang.Object[] args)
          Invokes the application in this jar file given the name of the class, a static Method and an array of arguments.
 java.lang.Object simpleInvokeMethod(java.lang.String className, java.lang.String method, java.lang.Object[] args)
          Invokes a static method of a given class in this JAR.
 
Methods inherited from class java.lang.ClassLoader
, addClass, check, checkCerts, checkPackageAccess, compareCerts, defineClass, defineClass, defineClass, defineClass0, definePackage, findBootstrapClass, findBootstrapClass0, findLibrary, findLoadedClass, findNative, findResource, findResources, findSystemClass, getBootstrapClassPath, getBootstrapResource, getBootstrapResources, getCallerClassLoader, getDefaultDomain, getGetClassLoaderPerm, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, initializePath, isAncestor, loadClass, loadClass, loadClassInternal, loadLibrary, loadLibrary0, resolveClass, resolveClass0, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

metaJarResources

private MetaJarResources metaJarResources

jarFile

private java.lang.String jarFile
Constructor Detail

MetaJarClassLoader

public MetaJarClassLoader(java.lang.String jFile)
Construct the class loader givena a Jar File.
Parameters:
jFile - the name of the jar file.
Method Detail

getActualJarName

public java.lang.String getActualJarName()
Return the actual jar name. This can be the top level Jar (whose name we usually ignore) or the jar file name we have passed in the constructor.
Returns:
the file name of the top-level jar file (if searching a Jar from inside an executing jar) or the jar name as we have passed it to the Constructor.

getMainClassName

public java.lang.String getMainClassName()
Returns the name of the jar file main class, or null if no "Main-Class" manifest attributes was defined.
Returns:
the name of the main class of the Jar file

invokeMethod

public java.lang.Object invokeMethod(java.lang.String className,
                                     java.lang.String method,
                                     java.lang.Object[] args)
                              throws java.lang.ClassNotFoundException,
                                     java.lang.NoSuchMethodException,
                                     java.lang.reflect.InvocationTargetException
Invokes the application in this jar file given the name of the class, a static Method and an array of arguments. The class must define this static method which takes an array of Object arguments and can return anything.
Parameters:
name - the name of the class
method - the name of the method to invoke
args - the arguments to the method of this class of the jar file

invokeClass

public void invokeClass(java.lang.String name,
                        java.lang.String[] args)
                 throws java.lang.ClassNotFoundException,
                        java.lang.NoSuchMethodException,
                        java.lang.reflect.InvocationTargetException
Invokes the application in this jar file given the name of the main class and an array of arguments. The class must define a static method "main" which takes an array of String arguemtns and is of return type "void".
Parameters:
name - the name of the main class
args - the arguments to the main method of the main class of the jar file

simpleInvokeMethod

public java.lang.Object simpleInvokeMethod(java.lang.String className,
                                           java.lang.String method,
                                           java.lang.Object[] args)
                                    throws java.lang.ClassNotFoundException,
                                           java.lang.NoSuchMethodException,
                                           java.lang.reflect.InvocationTargetException
Invokes a static method of a given class in this JAR. The class must define this static method which takes any number of arguments and can return anything. One must pass Strings as arguemnts, and they are converted to the appropriate native types. Only native types can be used, not even arrays of native types.
Please note that no overloading/polymorohism of methods is used. The first method that has the same name as the one passed, and the same number of arguments, is used.
And of course, "first" has no predictable meaning, so please be sure that you don't have ambiguities.
Parameters:
name - the name of the class
method - the name of the method to invoke
args - the arguments to the method of this class of the jar file, as Strings
Returns:
an Object containing th return value or values.

findClass

protected java.lang.Class findClass(java.lang.String className)
                             throws java.lang.ClassNotFoundException
Method that actually reads the class from raw bytes. This method uses MetaJarResources to read raw bytes, then creates a Class with the given bytes
Overrides:
findClass in class java.lang.ClassLoader
Parameters:
className - the classname (fully qualified with "."). Teh name is then read friom the jar file substituting "." with "/" and appending ".class"
Returns:
a Class object of representing the given class