Package com.helger.scope.mgr
Class ScopeManager
- java.lang.Object
-
- com.helger.scope.mgr.ScopeManager
-
@ThreadSafe public final class ScopeManager extends Object
This is the manager class for non-web scope handling. The following scopes are supported:- global
- application
- request
- session
- session application
- Author:
- Philip Helger
-
-
Field Summary
Fields Modifier and Type Field Description static booleanDEFAULT_CREATE_SCOPEstatic StringSCOPE_ATTRIBUTE_PREFIX_INTERNALThe prefix to be used for attribute names in any scope to indicate system internal attributes
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voiddestroySessionScope(ISessionScope aSessionScope)Manually destroy the passed session scope.static IGlobalScopegetGlobalScope()static IGlobalScopegetGlobalScopeOrNull()static IRequestScopegetRequestScope()static IRequestScopegetRequestScopeOrNull()static ISessionScopegetSessionScope()Get the current session scope, based on the current request scope.static ISessionScopegetSessionScope(boolean bCreateIfNotExisting)Get the current session scope, based on the current request scope.static ISessionScopegetSessionScope(boolean bCreateIfNotExisting, Function<? super String,? extends ISessionScope> aFactory)static voidinternalClearRequestScope()Internal method to clear request scope thread local.static voidinternalSetAndInitRequestScope(IRequestScope aRequestScope)This method is only to be called by this class and the web scope manager!static booleanisGlobalScopePresent()static booleanisInternalAttribute(String sAttributeName)Check if the passed attribute name is an internal attribute.static booleanisRequestScopePresent()static IGlobalScopeonGlobalBegin(String sScopeID)This method is used to set the initial global scope.static <T extends IGlobalScope>
TonGlobalBegin(String sScopeID, Function<? super String,T> aFactory)static voidonGlobalEnd()To be called when the singleton global context is to be destroyed.static IRequestScopeonRequestBegin(String sScopeID, String sSessionID)static <T extends IRequestScope>
TonRequestBegin(String sScopeID, String sSessionID, BiFunction<? super String,? super String,T> aFactory)static voidonRequestEnd()To be called after a request finished.static voidsetGlobalScope(IGlobalScope aGlobalScope)This method is only to be called by this class and the web scope manager!
-
-
-
Field Detail
-
SCOPE_ATTRIBUTE_PREFIX_INTERNAL
public static final String SCOPE_ATTRIBUTE_PREFIX_INTERNAL
The prefix to be used for attribute names in any scope to indicate system internal attributes- See Also:
- Constant Field Values
-
DEFAULT_CREATE_SCOPE
public static final boolean DEFAULT_CREATE_SCOPE
- See Also:
- Constant Field Values
-
-
Method Detail
-
setGlobalScope
public static void setGlobalScope(@Nonnull IGlobalScope aGlobalScope)
This method is only to be called by this class and the web scope manager!- Parameters:
aGlobalScope- The scope to be set. May not benull.
-
onGlobalBegin
@Nonnull public static IGlobalScope onGlobalBegin(@Nonnull @Nonempty String sScopeID)
This method is used to set the initial global scope.- Parameters:
sScopeID- The scope ID to use- Returns:
- The created global scope object. Never
null.
-
onGlobalBegin
@Nonnull public static <T extends IGlobalScope> T onGlobalBegin(@Nonnull @Nonempty String sScopeID, @Nonnull Function<? super String,T> aFactory)
-
getGlobalScopeOrNull
@Nullable public static IGlobalScope getGlobalScopeOrNull()
-
isGlobalScopePresent
public static boolean isGlobalScopePresent()
-
getGlobalScope
@Nonnull public static IGlobalScope getGlobalScope()
-
onGlobalEnd
public static void onGlobalEnd()
To be called when the singleton global context is to be destroyed.
-
getSessionScope
@Nonnull public static ISessionScope getSessionScope()
Get the current session scope, based on the current request scope.- Returns:
- Never
null. - Throws:
IllegalStateException- If no request scope is present or if the underlying request scope does not have a session ID.
-
getSessionScope
@Nullable public static ISessionScope getSessionScope(boolean bCreateIfNotExisting)
Get the current session scope, based on the current request scope.- Parameters:
bCreateIfNotExisting-trueto create a new scope, if none is present yet,falseto returnnullif either no request scope or no session scope is present.- Returns:
nullif bCreateIfNotExisting isfalseand either no request scope or no session scope is present, theISessionScopeotherwise.- Throws:
IllegalStateException- if bCreateIfNotExisting istruebut no request scope is present. This exception is also thrown if the underlying request scope does not have a session ID.
-
getSessionScope
@Nullable public static ISessionScope getSessionScope(boolean bCreateIfNotExisting, @Nonnull Function<? super String,? extends ISessionScope> aFactory)
-
destroySessionScope
public static void destroySessionScope(@Nonnull ISessionScope aSessionScope)
Manually destroy the passed session scope.- Parameters:
aSessionScope- The session scope to be destroyed. May not benull.
-
internalSetAndInitRequestScope
public static void internalSetAndInitRequestScope(@Nonnull IRequestScope aRequestScope)
This method is only to be called by this class and the web scope manager!- Parameters:
aRequestScope- The request scope to use. May not benull.
-
onRequestBegin
@Nonnull public static IRequestScope onRequestBegin(@Nonnull @Nonempty String sScopeID, @Nonnull @Nonempty String sSessionID)
-
onRequestBegin
@Nonnull public static <T extends IRequestScope> T onRequestBegin(@Nonnull @Nonempty String sScopeID, @Nonnull @Nonempty String sSessionID, @Nonnull BiFunction<? super String,? super String,T> aFactory)
-
getRequestScopeOrNull
@Nullable public static IRequestScope getRequestScopeOrNull()
- Returns:
- The current request scope or
nullif no request scope is present.
-
isRequestScopePresent
public static boolean isRequestScopePresent()
- Returns:
trueif a request scope is present,falseotherwise
-
getRequestScope
@Nonnull public static IRequestScope getRequestScope()
- Returns:
- The current request scope and never
null. - Throws:
IllegalStateException- If no request scope is present
-
internalClearRequestScope
public static void internalClearRequestScope()
Internal method to clear request scope thread local.- Since:
- 9.0.0
-
onRequestEnd
public static void onRequestEnd()
To be called after a request finished.
-
isInternalAttribute
public static boolean isInternalAttribute(@Nullable String sAttributeName)
Check if the passed attribute name is an internal attribute.- Parameters:
sAttributeName- The name of the attribute to check. May benull.- Returns:
trueif the passed attribute name is notnulland starts with theSCOPE_ATTRIBUTE_PREFIX_INTERNALprefix.
-
-