org.objectledge.context
Class Context

java.lang.Object
  extended by org.objectledge.context.Context

public class Context
extends Object

Context gives the application an ability to reference arbitrary objects using String names.

The bindings are local to the calling thread. This is a convenient way of passing data associated with a stream of processing without putting them into method attributes. This is especialy convenient with data that are not needed at a specific stage of processing, but may be needed by the nested stages - for example security credentials.

Note!It is important to clear the context before the thread departs from within application boundaries - otherwise the references may prevent the objects (and as a result large graphs of objects including classes and classloaders) from being garbage collected, while being otherwise being eligible to collection.

Version:
$Id: Context.java,v 1.8 2005/11/18 11:39:33 rafal Exp $
Author:
rafal@caltha.pl

Field Summary
private static ThreadLocal threadAttributes
          storage of the context attributes, specific to a thread.
 
Constructor Summary
Context()
           
 
Method Summary
 void clearAttributes()
          Removes all context attributes.
<T> T
getAttribute(Class<T> key)
          Return the value of a context attribute.
 Object getAttribute(String name)
          Return the value of a context attribute.
private  Map getAttributes()
           
 Object removeAttribute(Class key)
          Removes a context attribute.
 Object removeAttribute(String name)
          Removes a context attribute.
 Object setAttribute(Class key, Object value)
          Sets a new value of a context attribute.
 Object setAttribute(String name, Object value)
          Sets a new value of a context attribute.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

threadAttributes

private static ThreadLocal threadAttributes
storage of the context attributes, specific to a thread.

Constructor Detail

Context

public Context()
Method Detail

getAttribute

public Object getAttribute(String name)
Return the value of a context attribute.

Parameters:
name - the attribute name.
Returns:
the value of the attribute.

getAttribute

public <T> T getAttribute(Class<T> key)
Return the value of a context attribute.

Class object and the class name String are considered to be equivalent keys.

Type Parameters:
T - the type reflected by key Class object, to avoid casting on the caller's side.
Parameters:
key - a Class key of the attribute.
Returns:
the value of the attribute.

setAttribute

public Object setAttribute(String name,
                           Object value)
Sets a new value of a context attribute.

Parameters:
name - the name of the attribute.
value - the new value of the attribute.
Returns:
the old value of the attribute.

setAttribute

public Object setAttribute(Class key,
                           Object value)
Sets a new value of a context attribute.

Class object and the class name String are considered to be equivalent keys.

Parameters:
key - Class key of the attribute.
value - the new value of the attribute.
Returns:
the old value of the attribute.

removeAttribute

public Object removeAttribute(String name)
Removes a context attribute.

Parameters:
name - the name of the attribute.
Returns:
the old value of the attribute.

removeAttribute

public Object removeAttribute(Class key)
Removes a context attribute.

Class object and the class name String are considered to be equivalent keys.

Parameters:
key - Class key of the attribute.
Returns:
the old value of the attribute.

clearAttributes

public void clearAttributes()
Removes all context attributes.


getAttributes

private Map getAttributes()


Copyright © 2003-2006 objectledge.org. All Rights Reserved.