Interface ClassLoaderProvider

  • All Known Subinterfaces:
    DependencyProvider
    All Known Implementing Classes:
    DefaultClassLoaderProvider, RestrictedDependencyProvider

    public interface ClassLoaderProvider

    Definition of an interface for objects that act as a registry for class loaders.

    In complex environments different class loaders can play an important role. Thus the dependency injection framework supports the registration of arbitrary class loaders; they are assigned a symbolic name. When a class is loaded, a class loader can be specified by the symbolic name it was registered.

    This interface provides access to the registered class loaders by their symbolic name. It can be used by components that need to load classes per reflection.

    Version:
    $Id: ClassLoaderProvider.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Field Detail

      • CONTEXT_CLASS_LOADER

        static final String CONTEXT_CLASS_LOADER
        Constant for the reserved name of the context class loader. If this constant is passed into the getClassLoader() method, the context class loader will automatically be used.
        See Also:
        Constant Field Values
    • Method Detail

      • getClassLoader

        ClassLoader getClassLoader​(String name)
        Returns a reference to the class loader with the specified symbolic name. The passed in name can be null, then a default class loader will be returned. The constant CONTEXT_CLASS_LOADER can be passed in, too, for obtaining a reference to the context class loader. In all other cases the parameter is interpreted as the symbolic name of a registered class loader. If no class loader is found under this name, an exception will be thrown.
        Parameters:
        name - the name of the desired class loader
        Returns:
        the class loader for this name
        Throws:
        InjectionException - if the class loader cannot be resolved
      • loadClass

        Class<?> loadClass​(String name,
                           String loaderRef)
        Returns the class specified by the given name. This is a convenience method that obtains the class loader specified by its symbolic name and immediately uses it for loading a class. The same naming conventions apply as described for the getClassLoader(String) method. Occurring exceptions are re-thrown as runtime exceptions.
        Parameters:
        name - the name of the class to be loaded (must not be null)
        loaderRef - the name, under which the desired class loader is registered (null for the default class loader)
        Returns:
        the loaded class
        Throws:
        InjectionException - if the class cannot be loaded
      • registerClassLoader

        void registerClassLoader​(String name,
                                 ClassLoader loader)
        Registers a ClassLoader under the given name. This class loader can later be accessed using the getClassLoader() method. If the class loader reference is null, the class loader with the given name will be unregistered.
        Parameters:
        name - a symbolic name for the class loader (must not be null)
        loader - the class loader to be registered
        Throws:
        IllegalArgumentException - if the name is undefined
      • getDefaultClassLoaderName

        String getDefaultClassLoaderName()
        Returns the name of the default class loader.
        Returns:
        the name of the default class loader
        See Also:
        #setDefaultClassLoader(ClassLoader)
      • setDefaultClassLoaderName

        void setDefaultClassLoaderName​(String loaderName)
        Sets the name of the default class loader. It is possible to mark one of the registered class loaders as default class loader by passing its name to this method. Then this class loader will be used for all dynamic class loading operations for which no specific class loader name is specified. If this property was not set or the default class loader name is null, an implementation is free to return an arbitrary class loader - probably the one that loaded the implementation class.
        Parameters:
        loaderName - the name of the new default class loader
      • classLoaderNames

        Set<String> classLoaderNames()
        Returns a set with the names of all registered class loaders.
        Returns:
        the names of all registered class loaders