de.innovationgate.utils
public class DynamicClassLoadingChain extends java.lang.ClassLoader
The DynamicClassLoadingChain builds a chain of URLClassLoaders for the purpose of retrieving classes from all loaders while keeping individual loaders separate
for addition and updating.
The loaders registered are called "subloaders". New subloaders can be added at runtime, where each new classloader has the
most recently added classloader as it's parent. So using the most recently added classloader - the "top loader" retrievable by getTopLoader()
- you can load all classes provided by all class loaders in the chain.
Also at runtime individual class loaders in the chain may be replaced. At adding time you can provide a key with each new class loader that you can
use later to address the loader to be replaced. Use method updateSubLoader(String, URL[])
for this. Replacing a class loader will implicitly
recreate all class loaders down the child chain, but leave all class loaders up the parent chain untouched.
When adding a loader you can declare it "static" (when using updateSubLoader(String, URL[], boolean)
and setting the last param to true). These loaders will be
added to the chain in a way that will prevent them from being updated and rebuilt. They are inserted to the chain before all non-static loaders and updating or
removing them will cause an IllegalStateException
to be thrown. You can use static loaders for classes that need to be static in the classpath for their
functionalities to work. For example if you have a singleton class that maintains state, rebuilding the loader of the class will drop the singleton instance and by
the next use of it a new one will be created. By setting the loader to static you prevent such problems.
Modifier and Type | Class and Description |
---|---|
class |
DynamicClassLoadingChain.SubLoader |
Constructor and Description |
---|
DynamicClassLoadingChain(java.lang.ClassLoader parent)
Create a new chain.
|
Modifier and Type | Method and Description |
---|---|
void |
clearAssertionStatus() |
boolean |
equals(java.lang.Object obj) |
java.util.List<java.lang.String> |
getChainKeys()
Returns the keys of the classpath chain in the order of their loading priority (from parents to children)
|
java.net.URL |
getResource(java.lang.String arg0) |
java.io.InputStream |
getResourceAsStream(java.lang.String arg0) |
java.util.Enumeration<java.net.URL> |
getResources(java.lang.String arg0) |
DynamicClassLoadingChain.SubLoader |
getSubLoader(java.lang.String key) |
java.net.URL[] |
getSubLoaderURLs(java.lang.String key)
Retuns the url array registered for the subloader with the given key, in identical order as given to
updateSubLoader(String, URL[]) |
java.lang.ClassLoader |
getTopLoader()
Returns the topmost subloader of the chain, which can be used to load classes from all currently registered subloaders
|
int |
hashCode() |
boolean |
hasSubLoader(java.lang.String key) |
java.lang.Class<?> |
loadClass(java.lang.String arg0) |
void |
removeSubLoader(java.lang.String key)
Removes the subloader with the given key from the chain.
|
void |
setClassAssertionStatus(java.lang.String arg0,
boolean arg1) |
void |
setDefaultAssertionStatus(boolean arg0) |
void |
setPackageAssertionStatus(java.lang.String arg0,
boolean arg1) |
java.lang.String |
toString() |
void |
updateSubLoader(java.lang.String key,
java.net.URL[] urls)
Creates or updates a subloader for the given key.
|
void |
updateSubLoader(java.lang.String key,
java.net.URL[] urls,
boolean isstatic)
Creates or updates a subloader for the given key.
|
public DynamicClassLoadingChain(java.lang.ClassLoader parent)
parent
- The parent class loader to be used by the first subloader registeredpublic void updateSubLoader(java.lang.String key, java.net.URL[] urls, boolean isstatic) throws java.lang.IllegalStateException
key
- The of the loaderurls
- URLs to JARs or Java class folders that should be loaderd by this subloaderisstatic
- True if the loader should be static, i.e. should never be updatedjava.lang.IllegalStateException
- When trying to update a subloader that is staticpublic void updateSubLoader(java.lang.String key, java.net.URL[] urls)
key
- The of the loaderurls
- URLs to JARs or Java class folders that should be loaderd by this subloaderpublic java.net.URL[] getSubLoaderURLs(java.lang.String key)
updateSubLoader(String, URL[])
key
- The key of the subloader to usepublic void removeSubLoader(java.lang.String key)
key
- The key of the loader to removejava.lang.IllegalStateException
- When trying to update a subloader that is staticpublic java.lang.ClassLoader getTopLoader()
public java.util.List<java.lang.String> getChainKeys()
public void clearAssertionStatus()
clearAssertionStatus
in class java.lang.ClassLoader
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public java.net.URL getResource(java.lang.String arg0)
getResource
in class java.lang.ClassLoader
public java.io.InputStream getResourceAsStream(java.lang.String arg0)
getResourceAsStream
in class java.lang.ClassLoader
public java.util.Enumeration<java.net.URL> getResources(java.lang.String arg0) throws java.io.IOException
getResources
in class java.lang.ClassLoader
java.io.IOException
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.Class<?> loadClass(java.lang.String arg0) throws java.lang.ClassNotFoundException
loadClass
in class java.lang.ClassLoader
java.lang.ClassNotFoundException
public void setClassAssertionStatus(java.lang.String arg0, boolean arg1)
setClassAssertionStatus
in class java.lang.ClassLoader
public void setDefaultAssertionStatus(boolean arg0)
setDefaultAssertionStatus
in class java.lang.ClassLoader
public void setPackageAssertionStatus(java.lang.String arg0, boolean arg1)
setPackageAssertionStatus
in class java.lang.ClassLoader
public java.lang.String toString()
toString
in class java.lang.Object
public boolean hasSubLoader(java.lang.String key)
public DynamicClassLoadingChain.SubLoader getSubLoader(java.lang.String key)