net.sf.jguiraffe.di.impl
Class RestrictedDependencyProvider

java.lang.Object
  extended by net.sf.jguiraffe.di.impl.RestrictedDependencyProvider
All Implemented Interfaces:
ClassLoaderProvider, DependencyProvider

public class RestrictedDependencyProvider
extends java.lang.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 a BeanContext. The methods defined by the ClassLoaderProvider interface are fully implemented (by delegating to a wrapped ClassLoaderProvider). But other methods for dealing with dependencies throw an exception. This makes it possible to execute simple invocation scripts (e.g. defined by a ChainedInvocation), 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 (or BeanStore) 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 156 2009-03-03 21:04:47Z oheger $
Author:
Oliver Heger

Field Summary
 
Fields inherited from interface net.sf.jguiraffe.di.ClassLoaderProvider
CONTEXT_CLASS_LOADER
 
Constructor Summary
RestrictedDependencyProvider(ClassLoaderProvider clp)
          Creates a new instance of RestrictedDependencyProvider and initializes it with the specified ClassLoaderProvider.
 
Method Summary
 void addInitializer(BeanInitializer initializer)
          Adds a BeanInitializer.
 void beanCreated(java.lang.Object bean, BeanProvider provider)
          Notifies this dependency provider about a newly created bean.
 java.util.Set<java.lang.String> classLoaderNames()
          Returns a set with the names of all class loaders registered at this object.
 java.lang.ClassLoader getClassLoader(java.lang.String name)
          Returns the class loader that was registered under the given name.
 ClassLoaderProvider getClassLoaderProvider()
          Returns the wrapped ClassLoaderProvider.
 java.lang.ClassLoader getDefaultClassLoader()
          Returns the default class loader.
 java.lang.Object getDependentBean(Dependency dependency)
          Returns a dependent bean.
 boolean isBeanAvailable(Dependency dependency)
          Checks whether a bean is available.
 java.lang.Class<?> loadClass(java.lang.String name, java.lang.String loaderRef)
          Loads a class using the specified class loader.
 void registerClassLoader(java.lang.String name, java.lang.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 setDefaultClassLoader(java.lang.ClassLoader loader)
          Sets the default class loader.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RestrictedDependencyProvider

public RestrictedDependencyProvider(ClassLoaderProvider clp)
Creates a new instance of RestrictedDependencyProvider and initializes it with the specified ClassLoaderProvider.

Parameters:
clp - the ClassLoaderProvider (must not be null)
Throws:
java.lang.IllegalArgumentException - if the ClassLoaderProvider is null
Method Detail

getClassLoaderProvider

public ClassLoaderProvider getClassLoaderProvider()
Returns the wrapped ClassLoaderProvider.

Returns:
the ClassLoaderProvider

addInitializer

public void addInitializer(BeanInitializer initializer)
Adds a BeanInitializer. This implementation just throws an exception.

Specified by:
addInitializer in interface DependencyProvider
Parameters:
initializer - the initializer

beanCreated

public void beanCreated(java.lang.Object bean,
                        BeanProvider provider)
Notifies this dependency provider about a newly created bean. This implementation just throws an exception.

Specified by:
beanCreated in interface DependencyProvider
Parameters:
bean - the new bean
provider - the responsible bean provider

getDependentBean

public java.lang.Object getDependentBean(Dependency dependency)
Returns a dependent bean. This implementation invokes the passed in Dependency with a null bean store. If a bean provider is returned, this provider's bean is queried. This way certain Dependency implementations can be served, while others cause an exception.

Specified by:
getDependentBean in interface DependencyProvider
Parameters:
dependency - the dependency to resolve
Returns:
the corresponding bean
Throws:
InjectionException - if the Dependency 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 interface DependencyProvider
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 interface DependencyProvider
Parameters:
context - the context

classLoaderNames

public java.util.Set<java.lang.String> classLoaderNames()
Returns a set with the names of all class loaders registered at this object. This implementation delegates to the wrapped ClassLoaderProvider.

Specified by:
classLoaderNames in interface ClassLoaderProvider
Returns:
a set with the names of the known class loaders

getClassLoader

public java.lang.ClassLoader getClassLoader(java.lang.String name)
Returns the class loader that was registered under the given name. This implementation delegates to the wrapped ClassLoaderProvider.

Specified by:
getClassLoader in interface ClassLoaderProvider
Parameters:
name - the name of the class loader
Returns:
the corresponding class loader
Throws:
InjectionException - if the name is unknown

getDefaultClassLoader

public java.lang.ClassLoader getDefaultClassLoader()
Returns the default class loader. This implementation delegates to the wrapped ClassLoaderProvider.

Specified by:
getDefaultClassLoader in interface ClassLoaderProvider
Returns:
the default class loader

loadClass

public java.lang.Class<?> loadClass(java.lang.String name,
                                    java.lang.String loaderRef)
Loads a class using the specified class loader. This implementation delegates to the wrapped ClassLoaderProvider.

Specified by:
loadClass in interface ClassLoaderProvider
Parameters:
name - the name of the class to load
loaderRef - the name of the class loader
Returns:
the corresponding class
Throws:
InjectionException - if an error occurs

registerClassLoader

public void registerClassLoader(java.lang.String name,
                                java.lang.ClassLoader loader)
Registers a class loader under a name. This implementation delegates to the wrapped ClassLoaderProvider.

Specified by:
registerClassLoader in interface ClassLoaderProvider
Parameters:
name - the name
loader - the class loader

setDefaultClassLoader

public void setDefaultClassLoader(java.lang.ClassLoader loader)
Sets the default class loader. This implementation delegates to the wrapped ClassLoaderProvider.

Specified by:
setDefaultClassLoader in interface ClassLoaderProvider
Parameters:
loader - the new default class loader


Copyright © 2009 The JGUIraffe Team. All Rights Reserved.