org.roscopeco.janno.util
Class BeanBindingCollector

java.lang.Object
  extended by org.roscopeco.janno.util.BeanBindingCollector

public class BeanBindingCollector
extends Object

Simple utility class that collects instances of BeanBinding. This can be used by view processing servlets (for example) to ensure they get all bindings from the request line, rather than just those from the request stack itself.

To use, simply register in the appropriate container (or manually make a ComponentAdapter) with an appropriate instance of CollectionComponentParameter instantiated with the BeanBinding class. When instantiated, all instances of BeanBinding will be injected via the constructor, and used to build an internal map from String (coerced from the ComponentAdapter key) to the BeanBinding instance from the container. The binding is queried each time a bean is looked up, allowing custom bindings to use some other type of lookup as necessary, and as well as providing convienient access to the beans themselves (by simply querying the binding) this class provides methodised access to the binding itself. Do note, however, that only a single PicoContainer.getComponentInstance(java.lang.Object) will be made by any given collector for any given BeanBinding implementation registered in the request line tree.

Like the bindings themselves, this class is immutable, and should therefore be safe for use across multiple threads.

Version:
$Revision: 1.2 $ $Date: 2005/08/24 16:34:19 $
Author:
Ross Bamford (rosco<at>roscopeco.co.uk)

Constructor Summary
BeanBindingCollector(Map<?,?> bindings)
          Create a new BeanBindingCollector from the specified map.
 
Method Summary
static BeanBindingCollector collectFrom(PicoContainer pico)
          Convienience method that automatically obtains a BeanCollector with all BeanBindings visible in a given PicoContainer.
 Object getBean(String key)
          Convienience method that will directly obtain the bean bound to a given key.
 BeanBinding getBinding(String key)
          Obtain the BeanBinding with a given key.
protected  Map<String,BeanBinding> getBindings()
          Provides subclass-access to the binding map.
 Set<String> keySet()
          Returns a set of strings with all the bound bean-names.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BeanBindingCollector

public BeanBindingCollector(Map<?,?> bindings)
Create a new BeanBindingCollector from the specified map. Each entry will be copied from the supplied map, with keys coerced to Strings. Passing null is equivalent to passing an empty map.

When instantiating a collector from a request-line container, you must use a CollectionComponentParameter with the BeanBinding

Parameters:
bindings - A map containing string-to-BeanBindings. This is copied to the binding map (with forced string keys).
Throws:
ClassCastException - if an invalid BeanBinding implementation is encountered. If this occurs you should ensure that you are correctly registering the collector with a CollectionComponentParameter initialized with the BeanBinding class (and not the BeanBindingCollector class itself, for example).
Method Detail

collectFrom

public static BeanBindingCollector collectFrom(PicoContainer pico)
Convienience method that automatically obtains a BeanCollector with all BeanBindings visible in a given PicoContainer.

Parameters:
pico - The PicoContainer from which bindings should be collected (directly or by inheritence).
Returns:
A new BeanBindingCollector with all bean-bindings visible to the specified pico.

keySet

public Set<String> keySet()
Returns a set of strings with all the bound bean-names.

Returns:
An immutable Set of valid bean names.

getBean

public Object getBean(String key)
Convienience method that will directly obtain the bean bound to a given key. This method simply obtains the binding (with getBinding(String)) and returns the instance from it's BeanBinding.get() method. If no binding is found with the specified key, this method throws IllegalArgumentException.

Parameters:
key - The String key.
Returns:
The bound bean instance (never null).
Throws:
IllegalArgumentException - if the specified key is not bound.

getBinding

public BeanBinding getBinding(String key)
Obtain the BeanBinding with a given key.

Parameters:
key - The String key.
Returns:
The BeanBinding that was collected with the specified key (never null).
Throws:
IllegalArgumentException - if the specified key is not bound.

getBindings

protected Map<String,BeanBinding> getBindings()
Provides subclass-access to the binding map.

Returns:
The List<String,BeanBinding> held by this collector.


Copyright © 2005 Roscopeco Open Technologies. All Rights Reserved.