|
2.8.3: 2008-10-15 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.ClassLoader
net.sf.basedb.util.JarClassLoader
public final class JarClassLoader
A class loader implementation that loads classes from JAR files.
Each JAR file requires a separate instance of this class. Use
the getInstance(String)
method to get an existing or create
a new instance for a specific JAR file. If the classes in the JAR file
requires other classes in another JAR file to work, the paths to those
JAR files must be listed in the Class-Path
attribute in the
META-INF/MANIFEST.MF
file. For example:
Manifest-Version: 1.0 Class-Path: OtherJarPlugin.jarIf more than one JAR is needed separate them with one or more spaces. Note! It is only the
Class-Path
entry for the JAR file passed
to the getInstance(String)
method that is checked. The manifest
file is not checked for the other JARs.
Nested Class Summary | |
---|---|
(package private) static class |
JarClassLoader.JarInfo
|
Field Summary | |
---|---|
private static HashMap<String,JarClassLoader> |
classLoaders
A map of all loaded class loaders. |
private Map<String,List<File>> |
classPath
Contains mappings from class names to the JAR file in which the class implementation can be found. |
private List<JarClassLoader.JarInfo> |
jarFiles
For keeping track of jar files listed by Class-Path entry in the manifest file. |
private long |
jarSize
The size of the JAR file. |
private long |
jarTimeStamp
The timestamp of the JAR file. |
private static Logger |
log
|
private File |
mainJarFile
The main JAR file to load classes from. |
Constructor Summary | |
---|---|
private |
JarClassLoader(String jarPath)
Create a new JAR file class loader. |
Method Summary | |
---|---|
private String |
classNameToPath(String className)
Convert a class name to a file path. |
static boolean |
exists(String jarPath)
Check if a class loader for the given JAR file exists. |
protected Class<?> |
findClass(String name)
|
protected URL |
findResource(String name)
|
protected Enumeration<URL> |
findResources(String name)
|
static ClassLoader |
getInstance(String jarPath)
Get a class loader for the specified JAR file. |
static ClassLoader |
getInstance(String jarPath,
boolean autoUnload)
Get a class loader for the specified JAR file, optionally unloading an the old one if the JAR file has been modified. |
URL |
getResource(String name)
|
Enumeration<URL> |
getResources(String name)
|
boolean |
hasChanged(boolean checkSecondary)
Check if the JAR file this class loader loads is classes from has changed since this class loader was created. |
protected Class<?> |
loadClass(String name,
boolean resolve)
|
private byte[] |
loadClassData(File file,
String name)
Load the byte[] of the given class. |
private void |
loadJarFile(File file,
boolean followClassPath)
Open the specified JAR file, list all entries and put them in the classPath mapping. |
static ClassLoader |
newInstance(String jarPath)
Get a new class loader for the specified jar file. |
static void |
unload(String jarPath)
Unload the class loader for the given JAR file. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final Logger log
private static final HashMap<String,JarClassLoader> classLoaders
private final File mainJarFile
private final Map<String,List<File>> classPath
private final List<JarClassLoader.JarInfo> jarFiles
private final long jarTimeStamp
private final long jarSize
Constructor Detail |
---|
private JarClassLoader(String jarPath) throws IOException
jarPath
- The path to the JAR file
InvalidDataException
- If the JAR file can't be loaded
IOException
- If there is another IO-related errorMethod Detail |
---|
public static final ClassLoader getInstance(String jarPath) throws IOException
jarPath
- The path to a JAR file
IOException
- If the jar file can't be loadedpublic static final ClassLoader getInstance(String jarPath, boolean autoUnload) throws IOException
autoUnload
is true
and the JAR file
has changed since the existing class loader was created.
jarPath
- The path to a JAR fileautoUnload
- If TRUE the old class loaded will automatically be unloaded if the
JAR file or any one it depends on (listed in the Class-Path attribute in the manifest file)
has been modified (if the timestamp and/or size) is different
IOException
- If the jar file can't be loadedpublic static final ClassLoader newInstance(String jarPath) throws IOException
jarPath
- The path to the jar file
IOException
- If the jar file can't be loadedpublic static final void unload(String jarPath)
getInstance(String)
method is called again, a new class loader
instance will be created.
jarPath
- The path to the JAR filepublic static final boolean exists(String jarPath)
jarPath
- The path to the JAR file
protected Class<?> findClass(String name) throws ClassNotFoundException
findClass
in class ClassLoader
ClassNotFoundException
protected URL findResource(String name)
findResource
in class ClassLoader
protected Enumeration<URL> findResources(String name)
findResources
in class ClassLoader
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException
loadClass
in class ClassLoader
ClassNotFoundException
public URL getResource(String name)
getResource
in class ClassLoader
public Enumeration<URL> getResources(String name) throws IOException
getResources
in class ClassLoader
IOException
public boolean hasChanged(boolean checkSecondary)
checkSecondary
- TRUE to also check secondary JAR files
listed in the Class-Path entry of the manifest fileprivate String classNameToPath(String className)
private void loadJarFile(File file, boolean followClassPath) throws IOException
classPath
mapping.
file
- The JAR file to openfollowClassPath
- If the MANIFEST file should be checked for
a Class-Path
entry that lists other JAR files
that also should be checked.
IOException
- If there is an error reading the JAR filesprivate byte[] loadClassData(File file, String name) throws ClassNotFoundException
file
- The JAR file in which the class implmentation is locatedname
- The name of the class using regular naming convention,
ie. net.sf.basedb.util.JarClassLoader
ClassNotFoundException
- If the class can't be loaded
|
2.8.3: 2008-10-15 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |