net.yagga.util
Class ResourceMgr

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

public class ResourceMgr
extends java.lang.ClassLoader

A class to retrieve resource no matter where they are: on the file system, in a JAR, in the currently executing JAR or in a JAR inside the currently executing JAR. This class can extract resources as ImageIcon, String or raw InputStream, given a file name, and also classes. This class applies to these cases:
Where the class that uses ResourceMgr resides Where the Resource file can be
free (as a .class file) in the file system
in a JAR the file system
in a JAR (as a .class in the JAR we are invoking) in the file system (not tested)
in the same JAR as a plain file
in a JAR (as a .class in a JAR we are calling from the JAR we are invoking) in the same JAR as a plain file
in the top level JAR
The typical example is if we are executing the JAR below

  FOO.jar
    +- Main.class
    +- Support classes ..
    +- BUNDLE1.JAR
         +- Display.class
         +- ResourceMgr.class
         +- IMG/
            +- test.gif
  
If Main.classes uses classes in BUNDLE.jar (loaded with MetaJarClassLoader) and one such class (Display.class) must load a resource form the same JAR, say IMG/test.gif it can load it using ResourceMgr.retrieveImageIcon, thus ignoring its execution environment.

Author:
Walter Gamba
See Also:
MetaJarResources, MetaJarClassLoader

Inner classes inherited from class java.lang.ClassLoader
java.lang.ClassLoader.NativeLibrary
 
Field Summary
static boolean debug
          toggles debugging
private static MetaJarResources mjr
          MetaJarResource used to read meta-jar data.
 
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
ResourceMgr()
           
 
Method Summary
protected  java.lang.Class findClass(java.lang.String className)
          Method that reads the class from everywhere, file system jarred inside me etc..
static java.io.InputStream openResource(java.lang.String filename)
          gets the InputSream associated with a given file.
static java.lang.Class retrieveClass(java.lang.String className)
           
static java.lang.String retrieveFile(java.lang.String fileName)
          retrieve a (text) file as a String.
static javax.swing.ImageIcon retrieveImageIcon(java.lang.String image)
          retrieve an ImageIcon given a filename.
static javax.swing.ImageIcon retrieveImageIcon2(java.lang.String image)
           
static java.lang.String toHexString(byte[] orig, java.lang.String prefix, java.lang.String newLineSep, int limit)
          Write a String in HEXadecimal format.
 
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

debug

public static boolean debug
toggles debugging

mjr

private static MetaJarResources mjr
MetaJarResource used to read meta-jar data.
Constructor Detail

ResourceMgr

public ResourceMgr()
Method Detail

retrieveImageIcon

public static javax.swing.ImageIcon retrieveImageIcon(java.lang.String image)
retrieve an ImageIcon given a filename. The icon can reside quite anywhere: in the file system, in a JAR, even if the calling object resides in a JAR itself, or if it is called inside a JAR jarred inside the top-level executing JAR.
This version of retrieveImageIcon is corrected: now it can read images from:
1) file system
2) a jar file
3) from inside the executing jar
4) from a jar inside the currently executing jar
Parameters:
image - the name of the image file. If the name refers to a file or an entry in a JAR/ZIP file, please be sure to specify also the directory, if there is one (speaking relatively...(
Returns:
the ImageIcon created from the file, or null if errors

retrieveImageIcon2

public static javax.swing.ImageIcon retrieveImageIcon2(java.lang.String image)

retrieveFile

public static java.lang.String retrieveFile(java.lang.String fileName)
retrieve a (text) file as a String. Only suited to read text files. The file can reside quite anywhere: in the file system, in a JAR, even if the calling object resides in a JAR itself, or if it is called inside a JAR jarred inside the top-level executing JAR.
Parameters:
fileName - the name of the (text) file. If the name refers to a file or an entry in a JAR/ZIP file, please be sure to specify also the directory.
Returns:
the String containig the lines of the file

openResource

public static java.io.InputStream openResource(java.lang.String filename)
gets the InputSream associated with a given file. Use this method to read raw data from a resource The file can reside quite anywhere: in the file system, in a JAR, even if the calling object resides in a JAR itself, or if it is called inside a JAR jarred inside the top-level executing JAR.
Parameters:
fileName - the name of the file. If the name refers to a file or an entry in a JAR/ZIP file, please be sure to specify also the directory.
Returns:
the InputStream associated with the file.

retrieveClass

public static java.lang.Class retrieveClass(java.lang.String className)
                                     throws java.lang.ClassNotFoundException

findClass

protected java.lang.Class findClass(java.lang.String className)
                             throws java.lang.ClassNotFoundException
Method that reads the class from everywhere, file system jarred inside me etc.. 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

toHexString

public static java.lang.String toHexString(byte[] orig,
                                           java.lang.String prefix,
                                           java.lang.String newLineSep,
                                           int limit)
Write a String in HEXadecimal format. Output format is
'a' = 0x0060
'b' = 0x0061
Between lines the paraemter "newLineSep"is written.
Parameters:
orig - string to decode
prefix - a prefix for every "line"
newLineSep - string to use as line-separator
Returns:
decoded string (useful for debugging)