org.homeunix.thecave.moss.util
Class ClassLoaderFunctions

java.lang.Object
  extended by java.lang.ClassLoader
      extended by java.security.SecureClassLoader
          extended by java.net.URLClassLoader
              extended by org.homeunix.thecave.moss.util.ClassLoaderFunctions

public class ClassLoaderFunctions
extends java.net.URLClassLoader

Utility to load resources and objects from a jar file. Basically a wrapper to URLClassLoader, which formats a URL from a given Jar file. Also includes utilities to read entries from a Jar file.

Author:
wyatt

Nested Class Summary
static class ClassLoaderFunctions.JarLoaderException
           
 
Method Summary
static java.util.Collection<java.util.jar.JarEntry> getAllClasses(java.io.File jarFile, java.lang.String rootClass)
          Returns a collection of all the entries in a jar file.
static java.awt.Image getImageFromClasspath(java.lang.String name)
          Returns an ImageIcon from an image loaded in the current classpath.
static java.lang.Object getObject(java.io.File jarFile, java.lang.String className)
          Creates a new object specified by className from the given Jar file.
static java.io.InputStream getResourceAsStreamFromClasspath(java.lang.String resource)
          Returns the given resource as a stream.
static java.io.InputStream getResourceAsStreamFromJar(java.io.File jarFile, java.lang.String resource)
          Returns the given resource as a stream from the specified jar file.
static java.net.URL getResourceAsUrl(java.lang.String resource)
          Gets a URL representing the given resource on the class path.
 
Methods inherited from class java.net.URLClassLoader
addURL, definePackage, findClass, findResource, findResources, getPermissions, getURLs, newInstance, newInstance
 
Methods inherited from class java.security.SecureClassLoader
defineClass, defineClass
 
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findSystemClass, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, loadClass, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getObject

public static java.lang.Object getObject(java.io.File jarFile,
                                         java.lang.String className)
                                  throws ClassLoaderFunctions.JarLoaderException
Creates a new object specified by className from the given Jar file. Will not return a null object - if object is null, it will throw an exception.

Parameters:
jarFile - Jar file to load from
className - Class name to load
Returns:
The newly created object
Throws:
ClassLoaderFunctions.JarLoaderException

getAllClasses

public static java.util.Collection<java.util.jar.JarEntry> getAllClasses(java.io.File jarFile,
                                                                         java.lang.String rootClass)
Returns a collection of all the entries in a jar file. Can be used to browse for a particular class, etc.

Parameters:
jarFile - Jarfile to browse
Returns:

getResourceAsStreamFromJar

public static java.io.InputStream getResourceAsStreamFromJar(java.io.File jarFile,
                                                             java.lang.String resource)
Returns the given resource as a stream from the specified jar file. Useful for loading plugins and other objects from different files.

Parameters:
jarFile -
resource -
Returns:

getResourceAsStreamFromClasspath

public static java.io.InputStream getResourceAsStreamFromClasspath(java.lang.String resource)
Returns the given resource as a stream.

Parameters:
resource -
Returns:

getResourceAsUrl

public static java.net.URL getResourceAsUrl(java.lang.String resource)
Gets a URL representing the given resource on the class path. Use relative paths, from the root of the jar file. For instance, if you want to load the file example.png, in the folder Resources, use the string "Resources/example.png", but not "/Resources/example.png".

Parameters:
resource -
Returns:

getImageFromClasspath

public static java.awt.Image getImageFromClasspath(java.lang.String name)
Returns an ImageIcon from an image loaded in the current classpath. We first check for files in the current path, then we look in jar files.

Parameters:
name - The relative path and name of the file. Don't include the leading slash.
Returns:
An ImageIcon with the given icon, if available, or an empty one otherwise.