javolution.context
Class Context

Object
  extended by Context
All Implemented Interfaces:
Serializable, XMLSerializable
Direct Known Subclasses:
AllocatorContext, ConcurrentContext, LocalContext, LogContext, PersistentContext, SecurityContext

public abstract class Context
extends Object
implements XMLSerializable

This class represents an execution context; they can be associated to particular threads or objects.

Context-aware applications may extend the context base class or any predefined contexts in order to facilitate separation of concerns.

The scope of a Context should be surrounded by a try, finally block statement to ensure correct behavior in case of exceptions being raised. For example:

     LocalContext.enter(); // Current thread enter a local context.
     try 
         ModuloInteger.setModulus(m); // No impact on other threads!
         z = x.times(y); // Multiplication modulo m.
     } finally {
         LocalContext.exit();
     }

Context objects can be inherited by multiple threads (see ConcurrentContext}, but only one thread may enter a particular context instance at any given time (and becomes its owner. When the owner thread exits its context, the context is automatically recycled. Consequently, whether or not context objects are reused between multiple threads depends upon the AllocatorContext policy with regards to recycling. Threads executing in a PoolContext for example, will reuse the same pool of context objects.

Version:
5.0, April 15, 2007
Author:
Jean-Marie Dautelle
See Also:
Serialized Form

Field Summary
static Context ROOT
          Holds the root context.
 
Constructor Summary
protected Context()
          Default constructor.
 
Method Summary
static void enter(Class<? extends Context> contextType)
          Enters a context of specified type, this context is factory produced and automatically recycled upon exit.
static void enter(Context context)
          Enters the specified context.
protected abstract  void enterAction()
          The action to be performed after this context becomes the current context.
static void exit(Class<? extends Context> contextType)
          Exits the current context (the outer context becomes the current context).
static void exit(Context context)
          Exits the specified context.
protected abstract  void exitAction()
          The action to be performed before this context is no more the current context.
static Context getCurrentContext()
          Returns the current context for the current thread.
 Context getOuter()
          Returns the outer context of this context or null if ROOT or a default context (not entered).
 Thread getOwner()
          Returns the current owner of this context.
protected static void setConcurrentContext(ConcurrentContext context)
          Sets the current context, used by ConcurrentContext exclusively.
 String toString()
          Returns the string representation of this context (default "Instance of " + this.getClass().getName()).
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ROOT

public static final Context ROOT
Holds the root context. It is the current context for all newly created threads.

Constructor Detail

Context

protected Context()
Default constructor.

Method Detail

getCurrentContext

public static Context getCurrentContext()
Returns the current context for the current thread.

Returns:
the current context.

getOwner

public final Thread getOwner()
Returns the current owner of this context. The owner of a context is the thread which entered the context and has not yet exited. A context can only have one owner at any given time, although contexts can be shared by concurrent threads.

Returns:
the thread owner of this context or null.

getOuter

public final Context getOuter()
Returns the outer context of this context or null if ROOT or a default context (not entered).

Returns:
the outer context or null.

toString

public String toString()
Returns the string representation of this context (default "Instance of " + this.getClass().getName()).

Overrides:
toString in class Object
Returns:
the string representation of this context.

enterAction

protected abstract void enterAction()
The action to be performed after this context becomes the current context.


exitAction

protected abstract void exitAction()
The action to be performed before this context is no more the current context.


enter

public static final void enter(Context context)
Enters the specified context.

Parameters:
context - the context being entered.
Throws:
IllegalStateException - if this context is currently in use.

exit

public static final void exit(Context context)
Exits the specified context.

Parameters:
context - the context being exited.
Throws:
IllegalStateException - if the specified context is not the current context.

enter

public static final void enter(Class<? extends Context> contextType)
Enters a context of specified type, this context is factory produced and automatically recycled upon exit. If the specified contextType has no public no-arg constructor accessible, then the object factory for the class should be explicitely set (typically in a static initializer).

Parameters:
contextType - the type of context being entered.
See Also:
ObjectFactory.getInstance(Class)

exit

public static void exit(Class<? extends Context> contextType)
Exits the current context (the outer context becomes the current context).

Parameters:
contextType - the type of context being entered.
Throws:
IllegalStateException - if this context is the ROOT context or the current thread is not the context owner.

setConcurrentContext

protected static void setConcurrentContext(ConcurrentContext context)
Sets the current context, used by ConcurrentContext exclusively.

Parameters:
context - the concurrent context.


Copyright © 2005-2012 Javolution. All Rights Reserved.