Class RestrictedDependencyProvider
- java.lang.Object
-
- net.sf.jguiraffe.di.impl.RestrictedDependencyProvider
-
- All Implemented Interfaces:
ClassLoaderProvider,DependencyProvider
public class RestrictedDependencyProvider extends Object implements DependencyProvider
An implementation of the
DependencyProviderinterface, which does not support external dependencies.This class allows the execution of simple
Invokableobjects, which do not require access to other beans stored in aBeanContext. The methods defined by theClassLoaderProviderinterface are fully implemented (by delegating to a wrappedClassLoaderProvider). But other methods for dealing with dependencies throw an exception. This makes it possible to execute simple invocation scripts (e.g. defined by aChainedInvocation), including access to local variables. However, other bean providers cannot be accessed.A use case for this class is the execution of a shutdown script by a bean provider that is no longer needed. At the time the shutdown script is invoked the corresponding
BeanContext(orBeanStore) may already be partly destroyed, so access to other beans is not safe. Nevertheless arbitrary methods on the affected bean can be invoked.When creating an instance of this class a
ClassLoaderProviderobject must be provided. All methods dealing with classes and class loaders are passed to this object. The other methods are implemented by simply throwing an exception.- Version:
- $Id: RestrictedDependencyProvider.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Field Summary
-
Fields inherited from interface net.sf.jguiraffe.di.ClassLoaderProvider
CONTEXT_CLASS_LOADER
-
-
Constructor Summary
Constructors Constructor Description RestrictedDependencyProvider(ClassLoaderProvider clp, InvocationHelper invHlp)Creates a new instance ofRestrictedDependencyProviderand initializes it with the specifiedClassLoaderProviderand theInvocationHelper.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddInitializer(BeanInitializer initializer)Adds aBeanInitializer.voidbeanCreated(Object bean, BeanProvider provider)Notifies this dependency provider about a newly created bean.Set<String>classLoaderNames()Returns a set with the names of all class loaders registered at this object.ClassLoadergetClassLoader(String name)Returns the class loader that was registered under the given name.ClassLoaderProvidergetClassLoaderProvider()Returns the wrappedClassLoaderProvider.StringgetDefaultClassLoaderName()Returns the name of the default class loader.ObjectgetDependentBean(Dependency dependency)Returns a dependent bean.InvocationHelpergetInvocationHelper()Returns theInvocationHelperobject.booleanisBeanAvailable(Dependency dependency)Checks whether a bean is available.Class<?>loadClass(String name, String loaderRef)Loads a class using the specified class loader.voidregisterClassLoader(String name, ClassLoader loader)Registers a class loader under a name.voidsetCreationBeanContext(BeanContext context)Sets the context that is responsible for a bean creation event.voidsetDefaultClassLoaderName(String loaderName)Sets the name of the default class loader.
-
-
-
Constructor Detail
-
RestrictedDependencyProvider
public RestrictedDependencyProvider(ClassLoaderProvider clp, InvocationHelper invHlp)
Creates a new instance ofRestrictedDependencyProviderand initializes it with the specifiedClassLoaderProviderand theInvocationHelper.- Parameters:
clp- theClassLoaderProvider(must not be null)invHlp- theInvocationHelper(must not be null)- Throws:
IllegalArgumentException- if a required parameter is null
-
-
Method Detail
-
getClassLoaderProvider
public ClassLoaderProvider getClassLoaderProvider()
Returns the wrappedClassLoaderProvider.- Returns:
- the
ClassLoaderProvider
-
addInitializer
public void addInitializer(BeanInitializer initializer)
Adds aBeanInitializer. This implementation just throws an exception.- Specified by:
addInitializerin interfaceDependencyProvider- Parameters:
initializer- the initializer
-
beanCreated
public void beanCreated(Object bean, BeanProvider provider)
Notifies this dependency provider about a newly created bean. This implementation just throws an exception.- Specified by:
beanCreatedin interfaceDependencyProvider- Parameters:
bean- the new beanprovider- the responsible bean provider
-
getDependentBean
public Object getDependentBean(Dependency dependency)
Returns a dependent bean. This implementation invokes the passed inDependencywith a null bean store. If a bean provider is returned, this provider's bean is queried. This way certainDependencyimplementations can be served, while others cause an exception.- Specified by:
getDependentBeanin interfaceDependencyProvider- Parameters:
dependency- the dependency to resolve- Returns:
- the corresponding bean
- Throws:
InjectionException- if theDependencycannot be resolved
-
isBeanAvailable
public boolean isBeanAvailable(Dependency dependency)
Checks whether a bean is available. This implementation just throws an exception.- Specified by:
isBeanAvailablein interfaceDependencyProvider- Parameters:
dependency- the dependency to the bean- Returns:
- a flag whether this bean is available
- See Also:
DependencyProvider.addInitializer(BeanInitializer)
-
setCreationBeanContext
public void setCreationBeanContext(BeanContext context)
Sets the context that is responsible for a bean creation event. This implementation just throws an exception.- Specified by:
setCreationBeanContextin interfaceDependencyProvider- Parameters:
context- the context
-
classLoaderNames
public Set<String> classLoaderNames()
Returns a set with the names of all class loaders registered at this object. This implementation delegates to the wrappedClassLoaderProvider.- Specified by:
classLoaderNamesin interfaceClassLoaderProvider- Returns:
- a set with the names of the known class loaders
-
getClassLoader
public ClassLoader getClassLoader(String name)
Returns the class loader that was registered under the given name. This implementation delegates to the wrappedClassLoaderProvider.- Specified by:
getClassLoaderin interfaceClassLoaderProvider- Parameters:
name- the name of the class loader- Returns:
- the corresponding class loader
- Throws:
InjectionException- if the name is unknown
-
getDefaultClassLoaderName
public String getDefaultClassLoaderName()
Returns the name of the default class loader. This implementation delegates to the wrappedClassLoaderProvider.- Specified by:
getDefaultClassLoaderNamein interfaceClassLoaderProvider- Returns:
- the default class loader
- See Also:
#setDefaultClassLoader(ClassLoader)
-
loadClass
public Class<?> loadClass(String name, String loaderRef)
Loads a class using the specified class loader. This implementation delegates to the wrappedClassLoaderProvider.- Specified by:
loadClassin interfaceClassLoaderProvider- Parameters:
name- the name of the class to loadloaderRef- the name of the class loader- Returns:
- the corresponding class
- Throws:
InjectionException- if an error occurs
-
registerClassLoader
public void registerClassLoader(String name, ClassLoader loader)
Registers a class loader under a name. This implementation delegates to the wrappedClassLoaderProvider.- Specified by:
registerClassLoaderin interfaceClassLoaderProvider- Parameters:
name- the nameloader- the class loader
-
setDefaultClassLoaderName
public void setDefaultClassLoaderName(String loaderName)
Sets the name of the default class loader. This implementation delegates to the wrappedClassLoaderProvider.- Specified by:
setDefaultClassLoaderNamein interfaceClassLoaderProvider- Parameters:
loaderName- the new default class loader name
-
getInvocationHelper
public InvocationHelper getInvocationHelper()
Returns theInvocationHelperobject. This implementation returns the object that was passed to the constructor.- Specified by:
getInvocationHelperin interfaceDependencyProvider- Returns:
- the
InvocationHelperobject
-
-