net.sf.jguiraffe.di.impl
Class DefaultDependencyProvider

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

 class DefaultDependencyProvider
extends java.lang.Object
implements DependencyProvider

An internally used implementation of the DependencyProvider interface.

An instance of this class is created by the default BeanContext implementation at the beginning of a transaction. Its task is to obtain and cache all dependencies of the currently requested bean. This can fail if a dependency cannot be resolved or one of the dependent bean providers is already locked by another transaction. In this case the transaction has to be suspended.

If an instance could be successfully initialized, it allows access to all dependent bean providers - and no more. So the getDependentBean() method can be implemented in a meaningful way.

This class works closely together with the default bean context implementation. From there it also obtains a map with the registered class loaders.

Version:
$Id: DefaultDependencyProvider.java 92 2007-09-29 15:05:57Z oheger $
Author:
Oliver Heger

Field Summary
 
Fields inherited from interface net.sf.jguiraffe.di.ClassLoaderProvider
CONTEXT_CLASS_LOADER
 
Constructor Summary
DefaultDependencyProvider(ClassLoaderProvider loaderProvider)
          Creates a new instance of DefaultDependencyProvider and initializes it with the class loader provider to be used.
 
Method Summary
 void addInitializer(BeanInitializer initializer)
          Adds a bean initializer.
 java.util.Set<java.lang.String> classLoaderNames()
          Returns a set with the names of all registered class loaders.
 java.lang.ClassLoader getClassLoader(java.lang.String name)
          Returns the class loader with the given symbolic name.
 ClassLoaderProvider getClassLoaderProvider()
          Returns the internally used class loader provider.
 java.lang.ClassLoader getDefaultClassLoader()
          Returns the default class loader.
(package private)  java.util.Map<Dependency,BeanProvider> getDependencyMap()
          Returns a map with the resolved dependencies.
 java.lang.Object getDependentBean(Dependency dependency)
          Returns the bean of the bean provider specified by the given dependency.
 BeanProvider getDependentProvider(Dependency dependency)
          Returns the bean provider specified by the given dependency.
 boolean initialize(Dependency dependency, BeanStore store)
          Initializes this object.
 void invokeInitializers()
          Invokes all registered bean initializers.
 boolean isBeanAvailable(Dependency dependency)
          Tests whether a bean is available.
 java.lang.Class<?> loadClass(java.lang.String name, java.lang.String loaderRef)
          Loads the class with the specified name using the class loader identified by the given symbolic reference.
 void lock(java.lang.Long lockID)
          Marks all resolved dependencies as locked.
 void registerClassLoader(java.lang.String name, java.lang.ClassLoader loader)
          Registers a class loader under a symbolic name.
 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

DefaultDependencyProvider

public DefaultDependencyProvider(ClassLoaderProvider loaderProvider)
Creates a new instance of DefaultDependencyProvider and initializes it with the class loader provider to be used.

Parameters:
loaderProvider - the class loader provider
Method Detail

initialize

public boolean initialize(Dependency dependency,
                          BeanStore store)
Initializes this object. This method resolves the specified dependency and, recursively, all dependencies it depends on. The found bean providers are stored in an internal data structure. If a dependency cannot be resolved, a InjectionException exception is thrown. The return value indicates whether all found bean providers are unlocked. If a locked provider is found, the method is aborted, and false is returned.

Parameters:
dependency - the initial dependency to resolve
store - the starting bean store
Returns:
a flag whether all dependencies can be locked
Throws:
InjectionException - if a dependency cannot be resolved

getClassLoaderProvider

public ClassLoaderProvider getClassLoaderProvider()
Returns the internally used class loader provider.

Returns:
the class loader provider

lock

public void lock(java.lang.Long lockID)
Marks all resolved dependencies as locked. This method can be called after a successful invocation of initialize(). It sets the lock IDs for all involved BeanProviders, so that they cannot take part in another concurrent transaction. A second call to this method with the argument null releases all locks.

Parameters:
lockID - the lock ID

getDependentProvider

public BeanProvider getDependentProvider(Dependency dependency)
Returns the bean provider specified by the given dependency. This dependency must belong to the set of dependencies fetched during the initialize() method.

Parameters:
dependency - the dependency
Returns:
the bean provider specified by this dependency
Throws:
InjectionException - if the dependency cannot be resolved

getDependentBean

public java.lang.Object getDependentBean(Dependency dependency)
Returns the bean of the bean provider specified by the given dependency. This dependency must belong to the set of dependencies fetched during the initialize() method.

Specified by:
getDependentBean in interface DependencyProvider
Parameters:
dependency - the dependency
Returns:
the bean from the bean provider specified by this dependency
Throws:
InjectionException - if the dependency cannot be resolved

classLoaderNames

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

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

getClassLoader

public java.lang.ClassLoader getClassLoader(java.lang.String name)
Returns the class loader with the given symbolic name. This implementation delegates to the internal ClassLoaderProvider.

Specified by:
getClassLoader in interface ClassLoaderProvider
Parameters:
name - the name of the class loader
Returns:
the class loader with this name
Throws:
InjectionException - if the class loader cannot be resolved

getDefaultClassLoader

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

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

registerClassLoader

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

Specified by:
registerClassLoader in interface ClassLoaderProvider
Parameters:
name - the symbolic name for the class loader
loader - the class loader to register

setDefaultClassLoader

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

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

loadClass

public java.lang.Class<?> loadClass(java.lang.String name,
                                    java.lang.String loaderRef)
Loads the class with the specified name using the class loader identified by the given symbolic reference. This implementation delegates to the internal ClassLoaderProvider.

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

addInitializer

public void addInitializer(BeanInitializer initializer)
Adds a bean initializer. This initializer will be invoked by the invokeInitializers() method.

Specified by:
addInitializer in interface DependencyProvider
Parameters:
initializer - the initializer to be added (may be null, then this operation has no effect)

invokeInitializers

public void invokeInitializers()
Invokes all registered bean initializers. If an initializer throws an exception, it is caught and saved. The remaining initializers will be invoked. After that the exception is re-thrown.

Throws:
InjectionException - if an error occurs

isBeanAvailable

public boolean isBeanAvailable(Dependency dependency)
Tests whether a bean is available.

Specified by:
isBeanAvailable in interface DependencyProvider
Parameters:
dependency - the dependency
Returns:
a flag whether this dependency can be currently resolved
See Also:
DependencyProvider.addInitializer(BeanInitializer)

getDependencyMap

java.util.Map<Dependency,BeanProvider> getDependencyMap()
Returns a map with the resolved dependencies.

Returns:
the internal dependency map


Copyright 2007 null. All Rights Reserved.