Class 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 the handleInternalClasses 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
    • Constructor Detail

      • DefaultClassLoaderProvider

        public DefaultClassLoaderProvider()
        Creates a new instance of DefaultClassLoaderProvider.
      • DefaultClassLoaderProvider

        public DefaultClassLoaderProvider​(boolean fHandleInternalClasses)
        Creates a new instance of DefaultClassLoaderProvider 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 the getClassLoader() method.
        Specified by:
        classLoaderNames in interface ClassLoaderProvider
        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 interface ClassLoaderProvider
        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 interface ClassLoaderProvider
        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 interface ClassLoaderProvider
        Parameters:
        name - the class of the name to be loaded
        loaderRef - 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 interface ClassLoaderProvider
        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