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
DependencyProvider
interface, which does not support external dependencies.This class allows the execution of simple
Invokable
objects, which do not require access to other beans stored in aBeanContext
. The methods defined by theClassLoaderProvider
interface 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
ClassLoaderProvider
object 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 ofRestrictedDependencyProvider
and initializes it with the specifiedClassLoaderProvider
and theInvocationHelper
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addInitializer(BeanInitializer initializer)
Adds aBeanInitializer
.void
beanCreated(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.ClassLoader
getClassLoader(String name)
Returns the class loader that was registered under the given name.ClassLoaderProvider
getClassLoaderProvider()
Returns the wrappedClassLoaderProvider
.String
getDefaultClassLoaderName()
Returns the name of the default class loader.Object
getDependentBean(Dependency dependency)
Returns a dependent bean.InvocationHelper
getInvocationHelper()
Returns theInvocationHelper
object.boolean
isBeanAvailable(Dependency dependency)
Checks whether a bean is available.Class<?>
loadClass(String name, String loaderRef)
Loads a class using the specified class loader.void
registerClassLoader(String name, ClassLoader loader)
Registers a class loader under a name.void
setCreationBeanContext(BeanContext context)
Sets the context that is responsible for a bean creation event.void
setDefaultClassLoaderName(String loaderName)
Sets the name of the default class loader.
-
-
-
Constructor Detail
-
RestrictedDependencyProvider
public RestrictedDependencyProvider(ClassLoaderProvider clp, InvocationHelper invHlp)
Creates a new instance ofRestrictedDependencyProvider
and initializes it with the specifiedClassLoaderProvider
and 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:
addInitializer
in 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:
beanCreated
in 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 inDependency
with a null bean store. If a bean provider is returned, this provider's bean is queried. This way certainDependency
implementations can be served, while others cause an exception.- Specified by:
getDependentBean
in interfaceDependencyProvider
- Parameters:
dependency
- the dependency to resolve- Returns:
- the corresponding bean
- Throws:
InjectionException
- if theDependency
cannot be resolved
-
isBeanAvailable
public boolean isBeanAvailable(Dependency dependency)
Checks whether a bean is available. This implementation just throws an exception.- Specified by:
isBeanAvailable
in 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:
setCreationBeanContext
in 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:
classLoaderNames
in 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:
getClassLoader
in 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:
getDefaultClassLoaderName
in 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:
loadClass
in 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:
registerClassLoader
in 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:
setDefaultClassLoaderName
in interfaceClassLoaderProvider
- Parameters:
loaderName
- the new default class loader name
-
getInvocationHelper
public InvocationHelper getInvocationHelper()
Returns theInvocationHelper
object. This implementation returns the object that was passed to the constructor.- Specified by:
getInvocationHelper
in interfaceDependencyProvider
- Returns:
- the
InvocationHelper
object
-
-