org.webmacro
Class Context

java.lang.Object
  |
  +--org.webmacro.Context
All Implemented Interfaces:
java.lang.Cloneable, java.util.Map
Direct Known Subclasses:
BuildContext, WebContext

public class Context
extends java.lang.Object
implements java.util.Map, java.lang.Cloneable

A Context contains state. The idea is to put all of the data you wish to render into the Context and then merge it with a Template via the Template.write() or Template.evaluate() methods. Actually you can render any Macro object by passing a Context to its write() or evaluat() method, not just templates.

A Context is a per-thread data structure. It should not be shared between threads since it is not thread safe. The idea is to put all of the state for a single request into the context and then execute it, with each request having its own separate context. In this thread-per-request worldview there is no reason to synchronzie the Context objects as they are not shared bewteen threads.

Ordinarily you acquire a Context object from the WebMacro interface, use it for awhile, and then recycle() it. But you can implement your own Context objects and pass it to the evaluate() and write() method of any Template or other Macro.


Inner Class Summary
static class Context.TemplateEvaluationContext
           
 
Inner classes inherited from class java.util.Map
java.util.Map.Entry
 
Constructor Summary
Context(Broker broker)
          Create a new Context relative to the supplied broker
 
Method Summary
 void clear()
          Clear the context so that it can be used for another request.
 java.lang.Object clone()
          See cloneContext().
 Context cloneContext()
          Create a copy of this context.
 boolean containsKey(java.lang.Object key)
          Method from Map interface, operates on underlying Map
 boolean containsValue(java.lang.Object value)
          Method from Map interface, operates on underlying Map
 java.util.Set entrySet()
          Method from Map interface, operates on underlying Map
 java.lang.Object get(java.lang.Object name)
          Get the named object/property from the Context; returns null if not found.
 Broker getBroker()
          Get the instance of the Broker for this request
 java.lang.String getCurrentLocation()
           
 EvaluationExceptionHandler getEvaluationExceptionHandler()
          Get the EvaluationExceptionHandler
 Log getLog(java.lang.String type)
          Get a log instance that can be used to write log messages into the log under the supplied log type.
 Log getLog(java.lang.String type, java.lang.String description)
          Get a log instance that can be used to write log messages into the log under the supplied log type.
 java.util.Map getMap()
          Get the underlying Map object.
 Pool getPool()
           
 java.lang.Object getProperty(java.lang.Object name)
          Same as get(name) but can be overridden by subclasses to do something different
 java.lang.Object getProperty(java.lang.Object[] names)
          Same as get(Object names[]) but can be overridden by subclasses to behave differently
 Context.TemplateEvaluationContext getTemplateEvaluationContext()
           
 java.lang.Object internalGet(java.lang.Object name)
          Get the named object/property from the Context.
 java.lang.Object internalGet(java.lang.Object[] names)
          Get the named object from the Context.
 boolean isEmpty()
          Method from Map interface, operates on underlying Map
 boolean isTiming()
          Return true if the Context contains an active profiler, and calls to startTiming/stopTiming will be counted.
 java.util.Set keySet()
          Method from Map interface, operates on underlying Map
protected  void loadTools(java.lang.String keyName)
          Load the context tools listed in the supplied string.
 void put(java.lang.Object o, boolean b)
           
 void put(java.lang.Object o, byte b)
           
 void put(java.lang.Object o, char c)
           
 java.lang.Object put(java.lang.Object name, java.lang.Class c)
          Convenience method for putting static classes into the context, wraps the class instance in a wrapper
 void put(java.lang.Object o, double d)
           
 void put(java.lang.Object o, float f)
           
 void put(java.lang.Object o, int i)
           
 void put(java.lang.Object o, long l)
           
 java.lang.Object put(java.lang.Object name, java.lang.Object value)
          Add an object to the context returning the object that was there previously under the same name, if any.
 void put(java.lang.Object o, short s)
           
 void putAll(java.util.Map t)
          Method from Map interface, operates on underlying Map
 void recycle()
          Return the context to the object pool assigned via setPool(), if any.
 java.lang.Object remove(java.lang.Object key)
          Method from Map interface, operates on underlying Map
 boolean set(java.lang.Object[] names, java.lang.Object value)
          Set the named property in the Context.
 void setEvaluationExceptionHandler(EvaluationExceptionHandler eeh)
          Set a new EvaluationExceptionHandler
 void setMap(java.util.Map m)
          Set the underlying Map object.
 void setPool(Pool contextPool)
          Assign the object pool that this context should return to when its recycle() method is called.
 boolean setProperty(java.lang.Object[] names, java.lang.Object value)
          Same as set(Object names[], Object value) but can be overridden by subclasses to behave differently
 boolean setProperty(java.lang.Object name, java.lang.Object value)
          Same as put(name,value) but can be overridden by subclasses to do something different
 int size()
          Method from Map interface, operates on underlying Map
 void startTiming(java.lang.String name)
          Mark the start of an event for profiling.
 void startTiming(java.lang.String name, boolean arg)
          Same as startTiming(name1 + "(" + arg + ")") but the concatenation of strings and the call to toString() occurs only if profiling is enabled.
 void startTiming(java.lang.String name, int arg)
          Same as startTiming(name1 + "(" + arg + ")") but the concatenation of strings and the call to toString() occurs only if profiling is enabled.
 void startTiming(java.lang.String name1, java.lang.Object arg)
          Same as startTiming(name1 + "(" + arg + ")") but the concatenation of strings and the call to arg.toString() occurs only if profiling is enabled.
 void startTiming(java.lang.String name1, java.lang.Object arg1, java.lang.Object arg2)
          Same as startTiming(name1 + "(" + arg1 + "," + arg2 + ")") but the concatenation of strings and the call to arg.toString() occurs only if profiling * is enabled.
 void stopTiming()
          Mark the end of an event for profiling.
 java.util.Collection values()
          Method from Map interface, operates on underlying Map
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

Context

public Context(Broker broker)
Create a new Context relative to the supplied broker
Method Detail

loadTools

protected final void loadTools(java.lang.String keyName)
Load the context tools listed in the supplied string. See the ComponentMap class for a description of the format of this string.

clone

public final java.lang.Object clone()
See cloneContext(). Subclasses should override cloneContext() rather than the clone() method.
Overrides:
clone in class java.lang.Object

cloneContext

public Context cloneContext()
Create a copy of this context. The underlying storage will be copied and the local variables reset.

clear

public void clear()
Clear the context so that it can be used for another request. This does not meant hat the context is completely empty: it may have been configured with some initial state, such as a collection of tools, that are to be re-used. But all local variables and other local structures will be cleared.

Subclasses may override the clear method and add functionality but they must call super.clear() if they do so.

Specified by:
clear in interface java.util.Map

getBroker

public final Broker getBroker()
Get the instance of the Broker for this request

getTemplateEvaluationContext

public final Context.TemplateEvaluationContext getTemplateEvaluationContext()

getCurrentLocation

public final java.lang.String getCurrentLocation()

getLog

public final Log getLog(java.lang.String type,
                        java.lang.String description)
Get a log instance that can be used to write log messages into the log under the supplied log type.

getLog

public final Log getLog(java.lang.String type)
Get a log instance that can be used to write log messages into the log under the supplied log type. The type will be used as the description.

getEvaluationExceptionHandler

public EvaluationExceptionHandler getEvaluationExceptionHandler()
Get the EvaluationExceptionHandler

setEvaluationExceptionHandler

public void setEvaluationExceptionHandler(EvaluationExceptionHandler eeh)
Set a new EvaluationExceptionHandler

internalGet

public final java.lang.Object internalGet(java.lang.Object name)
                                   throws PropertyException
Get the named object/property from the Context. If the Object does not exist and there is a tool of the same name then the Object will be instantiated and managed by the tool. If there's no such variable, it throws.

get

public final java.lang.Object get(java.lang.Object name)
Get the named object/property from the Context; returns null if not found.
Specified by:
get in interface java.util.Map

put

public final java.lang.Object put(java.lang.Object name,
                                  java.lang.Class c)
Convenience method for putting static classes into the context, wraps the class instance in a wrapper

put

public final java.lang.Object put(java.lang.Object name,
                                  java.lang.Object value)
Add an object to the context returning the object that was there previously under the same name, if any.
Specified by:
put in interface java.util.Map

internalGet

public final java.lang.Object internalGet(java.lang.Object[] names)
                                   throws PropertyException
Get the named object from the Context. The name is a list of property names. The first name is the name of an object in the context. The subsequent names are properties of that object which will be searched using introspection.

set

public final boolean set(java.lang.Object[] names,
                         java.lang.Object value)
                  throws PropertyException
Set the named property in the Context. The first name is the name of an object in the context. The subsequent names are properties of that object which will be searched using introspection.

getProperty

public java.lang.Object getProperty(java.lang.Object name)
                             throws PropertyException
Same as get(name) but can be overridden by subclasses to do something different

setProperty

public boolean setProperty(java.lang.Object name,
                           java.lang.Object value)
                    throws PropertyException
Same as put(name,value) but can be overridden by subclasses to do something different

getProperty

public java.lang.Object getProperty(java.lang.Object[] names)
                             throws PropertyException
Same as get(Object names[]) but can be overridden by subclasses to behave differently

setProperty

public boolean setProperty(java.lang.Object[] names,
                           java.lang.Object value)
                    throws PropertyException
Same as set(Object names[], Object value) but can be overridden by subclasses to behave differently

setPool

public final void setPool(Pool contextPool)
Assign the object pool that this context should return to when its recycle() method is called.

getPool

public final Pool getPool()

recycle

public final void recycle()
Return the context to the object pool assigned via setPool(), if any. This method implicitly calls clear().

setMap

public final void setMap(java.util.Map m)
Set the underlying Map object. The supplied Map will subsequently be used to resolve local variables.

getMap

public final java.util.Map getMap()
Get the underlying Map object.

containsKey

public final boolean containsKey(java.lang.Object key)
Method from Map interface, operates on underlying Map
Specified by:
containsKey in interface java.util.Map

containsValue

public final boolean containsValue(java.lang.Object value)
Method from Map interface, operates on underlying Map
Specified by:
containsValue in interface java.util.Map

entrySet

public final java.util.Set entrySet()
Method from Map interface, operates on underlying Map
Specified by:
entrySet in interface java.util.Map

isEmpty

public final boolean isEmpty()
Method from Map interface, operates on underlying Map
Specified by:
isEmpty in interface java.util.Map

keySet

public final java.util.Set keySet()
Method from Map interface, operates on underlying Map
Specified by:
keySet in interface java.util.Map

putAll

public final void putAll(java.util.Map t)
Method from Map interface, operates on underlying Map
Specified by:
putAll in interface java.util.Map

remove

public final java.lang.Object remove(java.lang.Object key)
Method from Map interface, operates on underlying Map
Specified by:
remove in interface java.util.Map

size

public final int size()
Method from Map interface, operates on underlying Map
Specified by:
size in interface java.util.Map

values

public final java.util.Collection values()
Method from Map interface, operates on underlying Map
Specified by:
values in interface java.util.Map

isTiming

public final boolean isTiming()
Return true if the Context contains an active profiler, and calls to startTiming/stopTiming will be counted.

startTiming

public final void startTiming(java.lang.String name)
Mark the start of an event for profiling. Note that you MUST call stop() or the results of profiling will be invalid.

startTiming

public final void startTiming(java.lang.String name1,
                              java.lang.Object arg)
Same as startTiming(name1 + "(" + arg + ")") but the concatenation of strings and the call to arg.toString() occurs only if profiling is enabled.

startTiming

public final void startTiming(java.lang.String name1,
                              java.lang.Object arg1,
                              java.lang.Object arg2)
Same as startTiming(name1 + "(" + arg1 + "," + arg2 + ")") but the concatenation of strings and the call to arg.toString() occurs only if profiling * is enabled.

startTiming

public final void startTiming(java.lang.String name,
                              int arg)
Same as startTiming(name1 + "(" + arg + ")") but the concatenation of strings and the call to toString() occurs only if profiling is enabled.

startTiming

public final void startTiming(java.lang.String name,
                              boolean arg)
Same as startTiming(name1 + "(" + arg + ")") but the concatenation of strings and the call to toString() occurs only if profiling is enabled.

stopTiming

public final void stopTiming()
Mark the end of an event for profiling. Note that you MUST HAVE CALLED start() first or the results of profiling will be invalid.

put

public final void put(java.lang.Object o,
                      int i)

put

public final void put(java.lang.Object o,
                      byte b)

put

public final void put(java.lang.Object o,
                      short s)

put

public final void put(java.lang.Object o,
                      long l)

put

public final void put(java.lang.Object o,
                      char c)

put

public final void put(java.lang.Object o,
                      float f)

put

public final void put(java.lang.Object o,
                      double d)

put

public final void put(java.lang.Object o,
                      boolean b)