public class IlvClassLoaderUtil extends Object
Class loaders are designed to allow plugins (NetBeans, Eclipse, ...) to live in separate worlds and even to allow incompatible versions of the same class to coexist in the same JVM. Class loaders are opaque objects that can serve requests for classes and resources, given their name.
For resources and classes that are packaged within the same module or plugin,
one can use the class loader of the executing class: Simply use
getClass().getResource(...)
or Class.forName(...)
.
For accessing resources and classes that are packaged in a different module
or plugin, or that are made accessible by the user in his environment (of
which we don't know how it looks like), JViews uses the functions in this
class: forName(java.lang.Class, java.lang.String)
, getResource(java.lang.Class, java.lang.String)
, getResourceAsStream(java.lang.Class, java.lang.String)
.
You can use these functions as well.
Through registerClassLoader(java.lang.ClassLoader)
you can specify additional class loaders
to be considered in the lookup process. These can be class loaders of
application modules or plugins.
The functions in this class use the singleton instance of this class. The default lookup order is as follows:
Thread.currentThread().setContextClassLoader(...)
before
invoking JViews methods. This class loader is also able to emulate a
list of class loaders: The user can implement a
"MultiplexingClassLoader" if he needs to combine multiple class loaders
into a single one (but this is rarely needed).registerClassLoader(java.lang.ClassLoader)
.
Using the method setDelegate(IlvClassLoaderUtil)
, you can replace
the singleton instance of this class with an instance of
your subclass of yours, possibly overriding the methods forNameImpl(java.lang.Class, java.lang.String)
,
getResourceImpl(java.lang.Class, java.lang.String)
, getResourceAsStreamImpl(java.lang.Class, java.lang.String)
.
Background: http://www.javaworld.com/javaworld/javaqa/2003-06/01-qa-0606-load.html
Constructor and Description |
---|
IlvClassLoaderUtil()
Creates a new instance of
IlvClassLoaderUtil . |
Modifier and Type | Method and Description |
---|---|
static Class |
forName(Class callingClass,
String className)
Returns a class put in place by the user.
|
Class |
forNameImpl(Class callingClass,
String className)
Returns a class put in place by the user.
|
static ClassLoader |
getClassLoaderInstance()
Returns a ClassLoader that uses the lookup mechanism of this class.
|
static IlvClassLoaderUtil |
getDefaultInstance()
Returns the default instance of this class.
|
static IlvClassLoaderUtil |
getDelegate()
Returns the currently installed instance of this class.
|
static List<ClassLoader> |
getRegisteredClassLoaders()
Returns the list of registered class loaders.
|
static URL |
getResource(Class callingClass,
String name)
Returns the URL of a resource put in place by the user.
|
static InputStream |
getResourceAsStream(Class callingClass,
String name)
Returns a stream opened to read from a resource put in place by the user.
|
InputStream |
getResourceAsStreamImpl(Class callingClass,
String name)
Returns a stream opened to read from a resource put in place by the user.
|
URL |
getResourceImpl(Class callingClass,
String name)
Returns the URL of a resource put in place by the user.
|
static void |
registerClassLoader(ClassLoader cl)
Registers a class loader used for loading classes and resources.
|
static void |
registerClassLoaderLifetimeListener(ClassLoader loader,
ClassLoaderLifetimeListener listener)
Registers a
ClassLoaderLifetimeListener to be invoked when the given
ClassLoader stops being actively used. |
static void |
setDelegate(IlvClassLoaderUtil newInstance)
Replaces the currently installed instance of this class.
|
static void |
unregisterClassLoader(ClassLoader cl)
Unregisters a class loader used for loading classes and resources, and
executes the
ClassLoaderLifetimeListener s registered for the class
loader. |
static void |
unregisterClassLoaderLifetimeListener(ClassLoader loader,
ClassLoaderLifetimeListener listener)
Unregisters a
ClassLoaderLifetimeListener from being associated
with a given ClassLoader . |
static void |
unregisterClassLoaderLifetimeListeners(ClassLoader loader)
Unregisters all
ClassLoaderLifetimeListener s associated with a
given ClassLoader . |
public IlvClassLoaderUtil()
IlvClassLoaderUtil
.setDelegate(IlvClassLoaderUtil)
public static void registerClassLoaderLifetimeListener(ClassLoader loader, ClassLoaderLifetimeListener listener)
ClassLoaderLifetimeListener
to be invoked when the given
ClassLoader
stops being actively used.
The listener will be invoked once only: when
unregisterClassLoader(ClassLoader)
is called on the class loader.
You can register the same listener many times for a given ClassLoader
;
it will nevertheless be stored only once and be executed only once.
unregisterClassLoader(ClassLoader)
,
unregisterClassLoaderLifetimeListener(ClassLoader, ClassLoaderLifetimeListener)
public static void unregisterClassLoaderLifetimeListener(ClassLoader loader, ClassLoaderLifetimeListener listener)
ClassLoaderLifetimeListener
from being associated
with a given ClassLoader
.registerClassLoaderLifetimeListener(ClassLoader, ClassLoaderLifetimeListener)
public static void unregisterClassLoaderLifetimeListeners(ClassLoader loader)
ClassLoaderLifetimeListener
s associated with a
given ClassLoader
.public static void registerClassLoader(ClassLoader cl)
Note: If you use this method during the startup of an Eclipse plug-in,
you also have to use unregisterClassLoader(ClassLoader)
during the shutdown of the same Eclipse plug-in.
cl
- classloader used when searching a class or resourceforName(Class, String)
,
getResource(Class, String)
,
getResourceAsStream(Class, String)
public static void unregisterClassLoader(ClassLoader cl)
ClassLoaderLifetimeListener
s registered for the class
loader.
Note: If an Eclipse plug-in uses the JViews API, it should call this
method during its shutdown, even if it did not use
registerClassLoader(ClassLoader)
during its startup.
cl
- classloader used when searching a class or resourceforName(Class, String)
,
getResource(Class, String)
,
getResourceAsStream(Class, String)
,
registerClassLoaderLifetimeListener(ClassLoader, ClassLoaderLifetimeListener)
public static List<ClassLoader> getRegisteredClassLoaders()
public Class forNameImpl(Class callingClass, String className) throws ClassNotFoundException
registerClassLoader(ClassLoader)
./li>
callingClass
- the source for current class loaderclassName
- the name of class to loadClass
object for the class with the specified
name.LinkageError
- if the linkage failsExceptionInInitializerError
- if the initialization provoked by this method failsClassNotFoundException
- if the class cannot be locatedregisterClassLoader(ClassLoader)
,
unregisterClassLoader(ClassLoader)
public URL getResourceImpl(Class callingClass, String name)
callingClass
- the source for current class loadername
- The name of the desired resource, using the same conventions
as for the ClassLoader.getResource
method: an
absolute resource name, with the leading slash removed.java.net.URL
object, or null
if the
resource was not foundIllegalArgumentException
- if the name argument is syntactically
incorrectregisterClassLoader(ClassLoader)
,
unregisterClassLoader(ClassLoader)
public InputStream getResourceAsStreamImpl(Class callingClass, String name)
callingClass
- the source for current class loadername
- The name of the desired resource, using the same conventions
as for the ClassLoader.getResource
method: an
absolute resource name, with the leading slash removed.InputStream
object, or null
if the
resource was not foundIllegalArgumentException
- if the name argument is syntactically
incorrectregisterClassLoader(ClassLoader)
,
unregisterClassLoader(ClassLoader)
public static IlvClassLoaderUtil getDefaultInstance()
public static IlvClassLoaderUtil getDelegate()
public static void setDelegate(IlvClassLoaderUtil newInstance)
public static Class forName(Class callingClass, String className) throws ClassNotFoundException
forNameImpl(java.lang.Class, java.lang.String)
of the current instance.callingClass
- the source for current class loaderclassName
- the name of class to loadClass
object for the class with the specified
name.LinkageError
- if the linkage failsExceptionInInitializerError
- if the initialization provoked by this method failsClassNotFoundException
- if the class cannot be locatedforNameImpl(Class, String)
,
registerClassLoader(ClassLoader)
,
unregisterClassLoader(ClassLoader)
public static URL getResource(Class callingClass, String name)
getResourceImpl(java.lang.Class, java.lang.String)
of the current instance.callingClass
- the source for current class loadername
- The name of the desired resource, using the same conventions
as for the ClassLoader.getResource
method: an
absolute resource name, with the leading slash removed.java.net.URL
object, or null
if the
resource was not foundIllegalArgumentException
- if the name argument is syntactically
incorrectregisterClassLoader(ClassLoader)
,
unregisterClassLoader(ClassLoader)
public static InputStream getResourceAsStream(Class callingClass, String name)
getResourceAsStreamImpl(java.lang.Class, java.lang.String)
of the current instance.callingClass
- the source for current class loadername
- The name of the desired resource, using the same conventions
as for the ClassLoader.getResource
method: an
absolute resource name, with the leading slash removed.InputStream
object, or null
if the
resource was not foundIllegalArgumentException
- if the name argument is syntactically
incorrectregisterClassLoader(ClassLoader)
,
unregisterClassLoader(ClassLoader)
public static ClassLoader getClassLoaderInstance()
null
.© Copyright Rogue Wave Software, Inc. 1997, 2015. All Rights Reserved.