org.apache.log

Class ContextMap

public final class ContextMap extends Object implements Serializable

The ContextMap contains non-hierarchical context information relevant to a particular LogEvent. It may include information such as; The context is bound to a thread (and inherited by sub-threads) but it can also be added to by LogTargets.

Author: Avalon Development Team Peter Donald

Field Summary
static ThreadLocalc_localContext
Hashtablem_map
ContextMapm_parent
booleanm_readOnly
Constructor Summary
ContextMap()
Default constructor.
ContextMap(ContextMap parent)
Constructor that sets parent contextMap.
Method Summary
static voidbind(ContextMap context)
Bind a particular ContextMap to current thread.
voidcheckReadable()
Utility method to verify that Context is read-only.
voidclear()
Empty the context map.
Objectget(String key, Object defaultObject)
Get an entry from the context.
Objectget(String key)
Get an entry from the context.
static ContextMapgetCurrentContext()
Get the Current ContextMap.
static ContextMapgetCurrentContext(boolean autocreate)
Get the Current ContextMap.
intgetSize()
Get the number of contexts in map.
booleanisReadOnly()
Determine if context is read-only.
voidmakeReadOnly()
Make the context read-only.
ObjectreadResolve()
Helper method that sets context to read-only after de-serialization.
voidset(String key, Object value)
Set a value in context

Field Detail

c_localContext

private static final ThreadLocal c_localContext

m_map

private Hashtable m_map

m_parent

private final ContextMap m_parent

m_readOnly

private transient boolean m_readOnly

Constructor Detail

ContextMap

public ContextMap()
Default constructor.

ContextMap

public ContextMap(ContextMap parent)
Constructor that sets parent contextMap.

Parameters: parent the parent ContextMap

Method Detail

bind

public static final void bind(ContextMap context)
Bind a particular ContextMap to current thread.

Parameters: context the context map (may be null)

checkReadable

private void checkReadable()
Utility method to verify that Context is read-only.

clear

public void clear()
Empty the context map.

get

public Object get(String key, Object defaultObject)
Get an entry from the context.

Parameters: key the key to map defaultObject a default object to return if key does not exist

Returns: the object in context

get

public Object get(String key)
Get an entry from the context.

Parameters: key the key to map

Returns: the object in context or null if none with specified key

getCurrentContext

public static final ContextMap getCurrentContext()
Get the Current ContextMap. This method returns a ContextMap associated with current thread. If the thread doesn't have a ContextMap associated with it then a new ContextMap is created.

Returns: the current ContextMap

getCurrentContext

public static final ContextMap getCurrentContext(boolean autocreate)
Get the Current ContextMap. This method returns a ContextMap associated with current thread. If the thread doesn't have a ContextMap associated with it and autocreate is true then a new ContextMap is created.

Parameters: autocreate true if a ContextMap is to be created if it doesn't exist

Returns: the current ContextMap

getSize

public int getSize()
Get the number of contexts in map.

Returns: the number of contexts in map

isReadOnly

public boolean isReadOnly()
Determine if context is read-only.

Returns: true if Context is read only, false otherwise

makeReadOnly

public void makeReadOnly()
Make the context read-only. This makes it safe to allow untrusted code reference to ContextMap.

readResolve

private Object readResolve()
Helper method that sets context to read-only after de-serialization.

Returns: the corrected object version

set

public void set(String key, Object value)
Set a value in context

Parameters: key the key value the value (may be null)