Class DefaultClassLoaderProvider
- java.lang.Object
-
- net.sf.jguiraffe.di.impl.DefaultClassLoaderProvider
-
- All Implemented Interfaces:
ClassLoaderProvider
public class DefaultClassLoaderProvider extends Object implements ClassLoaderProvider
A default implementation of the
ClassLoaderProvider
interface.This is a straight-forward (but thread-safe) implementation of all the methods defined by the
ClassLoaderProvider
interface. Class loaders registered at this object are stored in a map. A default class loader name can be set. If none has been set, the class loader that loaded this class is returned as default class loader.Per default, classes of the library (starting with the prefix
net.sf.jguiraffe
) are always loaded by the class loader which loaded this class. This makes sense for instance in an OSGi environment: it allows access even to internal implementation classes. This behavior can be disabled by setting thehandleInternalClasses
property to false. Then the selected class loader is used to load all classes.- Version:
- $Id: DefaultClassLoaderProvider.java 211 2012-07-10 19:49:13Z oheger $
- Author:
- Oliver Heger
-
-
Field Summary
-
Fields inherited from interface net.sf.jguiraffe.di.ClassLoaderProvider
CONTEXT_CLASS_LOADER
-
-
Constructor Summary
Constructors Constructor Description DefaultClassLoaderProvider()
Creates a new instance ofDefaultClassLoaderProvider
.DefaultClassLoaderProvider(boolean fHandleInternalClasses)
Creates a new instance ofDefaultClassLoaderProvider
and sets the flag whether internal classes should be handled in a special way.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Set<String>
classLoaderNames()
Returns a set with the names of the class loaders that have been registered at this object.ClassLoader
getClassLoader(String name)
Returns the class loader specified by the given symbolic name.String
getDefaultClassLoaderName()
Returns the name of the default class loader.boolean
isHandleInternalClasses()
Returns a flag whether internal library classes are handled in a special way by this class loader provider.Class<?>
loadClass(String name, String loaderRef)
Loads the class with the specified name using the class loader identified by the given symbolic reference.void
registerClassLoader(String name, ClassLoader loader)
Allows to register a class loader under a symbolic name.void
setDefaultClassLoaderName(String loaderName)
Sets the name of the default class loader.
-
-
-
Constructor Detail
-
DefaultClassLoaderProvider
public DefaultClassLoaderProvider()
Creates a new instance ofDefaultClassLoaderProvider
.
-
DefaultClassLoaderProvider
public DefaultClassLoaderProvider(boolean fHandleInternalClasses)
Creates a new instance ofDefaultClassLoaderProvider
and sets the flag whether internal classes should be handled in a special way.- Parameters:
fHandleInternalClasses
- the value of the flag- Since:
- 1.2
-
-
Method Detail
-
isHandleInternalClasses
public boolean isHandleInternalClasses()
Returns a flag whether internal library classes are handled in a special way by this class loader provider. If this property is true, all classes belonging to this library are loaded by the class loader which also loaded this class.- Returns:
- true if library classes are handled in a special way, false otherwise
- Since:
- 1.2
-
classLoaderNames
public Set<String> classLoaderNames()
Returns a set with the names of the class loaders that have been registered at this object. All these names can be passed into thegetClassLoader()
method.- Specified by:
classLoaderNames
in interfaceClassLoaderProvider
- Returns:
- a set with the names of the class loaders registered at this object
-
getClassLoader
public ClassLoader getClassLoader(String name)
Returns the class loader specified by the given symbolic name. This method supports all variants: the default class loader (in this case the name is null), the context class loader, and a registered class loader.- Specified by:
getClassLoader
in interfaceClassLoaderProvider
- Parameters:
name
- the name of the class loader- Returns:
- the corresponding class loader
- Throws:
InjectionException
- if the class loader cannot be resolved
-
getDefaultClassLoaderName
public String getDefaultClassLoaderName()
Returns the name of the default class loader. Result can be null if no default class loader name has been set so far.- Specified by:
getDefaultClassLoaderName
in interfaceClassLoaderProvider
- Returns:
- the name of the default class loader
- See Also:
#setDefaultClassLoader(ClassLoader)
-
loadClass
public Class<?> loadClass(String name, String loaderRef)
Loads the class with the specified name using the class loader identified by the given symbolic reference.- Specified by:
loadClass
in interfaceClassLoaderProvider
- Parameters:
name
- the class of the name to be loadedloaderRef
- determines the class loader to be used- Returns:
- the loaded class
- Throws:
InjectionException
- if the class cannot be loaded
-
registerClassLoader
public void registerClassLoader(String name, ClassLoader loader)
Allows to register a class loader under a symbolic name.- Specified by:
registerClassLoader
in interfaceClassLoaderProvider
- Parameters:
name
- the name of the class loader (must not be null)loader
- the class loader to be registered; can be null, then the class loader with the given name will be removed- Throws:
IllegalArgumentException
- if the name is null
-
setDefaultClassLoaderName
public void setDefaultClassLoaderName(String loaderName)
Sets the name of the default class loader. Applications can here specify the name of a class loader they have registered or the reserved nameClassLoaderProvider.CONTEXT_CLASS_LOADER
for the context class loader. A value of null causesgetClassLoader(String)
to fall back to the class loader which loaded this class.- Specified by:
setDefaultClassLoaderName
in interfaceClassLoaderProvider
- Parameters:
loaderName
- the new default class loader
-
-