@Api public class ContextHolder<T extends Context> extends Object
Context
holder. Stores same type contexts per thread on stack (this allows us to use
nested contexts). Any new threads created from within thread with active context, will inherit
this context.
Usually used by context implementations that allows static access to context holder.
Constructor and Description |
---|
ContextHolder(String contextName) |
ContextHolder(String contextName,
boolean single) |
Modifier and Type | Method and Description |
---|---|
void |
activate(T newContext)
Activates given context and puts it on top of stack.
|
void |
activate(T newContext,
InContext<T> action)
Activates given context and puts it on top of stack and automatically removes it after
invoking provided action.
|
T |
current()
Returns current context from top of stack.
|
boolean |
isActive()
Returns true if there is at least 1 context on stack.
|
T |
remove()
Removes and returns current context.
|
public ContextHolder(String contextName)
public ContextHolder(String contextName, boolean single)
public boolean isActive()
public T current() throws ContextNotActiveException
ContextNotActiveException
public void activate(T newContext)
newContext
- new contextpublic void activate(T newContext, InContext<T> action)
newContext
- new contextaction
- actionpublic T remove() throws ContextNotActiveException
ContextNotActiveException
Copyright © 2019 Suncode. All rights reserved.