info.magnolia.context
Class MgnlContext

java.lang.Object
  extended by info.magnolia.context.MgnlContext

public class MgnlContext
extends Object

This class allows obtaining of the current Request without passing the request around the world. A ThreadLocal variable is used to manage the request and to make sure it doesn't escape to another processing.

In a later version this class should not depend on servlets. The core should use the context to get and set attributes instead of using the request or session object directly. Magnolia could run then in a neutral and configurable context.

Version:
$Revision$ ($Author$)

Nested Class Summary
static interface MgnlContext.Op<T,E extends Throwable>
          A simple execution interface to be used with the doInSystemContext method.
static interface MgnlContext.SystemContextOperation
          Deprecated. since 4.2 - use the Op interface, which can return values, or extend VoidOp.
static class MgnlContext.VoidOp
          An Op that does not return values and can only throw RuntimeExceptions.
 
Constructor Summary
MgnlContext()
          Do not instantiate this class.
 
Method Summary
static
<T,E extends Throwable>
T
doInSystemContext(MgnlContext.Op<T,E> op)
          Executes the given operation in the system context and sets it back to the original once done (also if an exception is thrown).
static
<T,E extends Throwable>
T
doInSystemContext(MgnlContext.Op<T,E> op, boolean releaseAfterExecution)
          Executes the given operation in the system context and sets it back to the original once done (also if an exception is thrown).
static void doInSystemContext(MgnlContext.SystemContextOperation op)
          Deprecated. since 4.2 - use the Op interface, which can return values, or extend VoidOp.
static void doInSystemContext(MgnlContext.SystemContextOperation op, boolean releaseAfterExecution)
          Deprecated. since 4.2 - use the Op interface, which can return values, or extend VoidOp.
static AccessManager getAccessManager(String name)
          Get access manager for a resource, usually a workspace.
static AggregationState getAggregationState()
          Returns the AggregationState if we're in a WebContext, throws an IllegalStateException otherwise.
static
<T> T
getAttribute(String name)
          Get attribute value.
static
<T> T
getAttribute(String name, int scope)
          Get the attribute from the specified scope.
static String getContextPath()
           
static HierarchyManager getHierarchyManager(String repositoryId)
          Deprecated. since 4.5 - use getJCRSession(String)
static Context getInstance()
          Get the current context of this thread.
static javax.jcr.Session getJCRSession(String workspaceName)
          Note: this is the way to go, if you no longer want to rely on the Content-API.
static Locale getLocale()
          Get the context's locale object.
static Messages getMessages()
           
static Messages getMessages(String basename)
           
static String getParameter(String name)
          Get parameter value as string.
static Map<String,String> getParameters()
          Get parameter value as a Map<String, String>.
static String[] getParameterValues(String name)
           
static MultipartForm getPostedForm()
          Get form object assembled by MultipartRequestFilter.
static QueryManager getQueryManager(String workspaceName)
          Deprecated. since 4.5 - use getJCRSession(String) and acquire the JCR query manager directly from the session.
static Subject getSubject()
           
static SystemContext getSystemContext()
          Deprecated. since 4.5, use IoC, i.e., declare a dependency on SystemContext in your component.
static User getUser()
          A short cut for the current user.
static WebContext getWebContext()
          Throws an IllegalStateException if the current context is not set, or if it is not an instance of WebContext.
static WebContext getWebContext(String exceptionMessage)
          Throws an IllegalStateException if the current context is not set, or if it is not an instance of WebContext.
static WebContext getWebContextOrNull()
          Returns the current context if it is set and is an instance of WebContext, returns null otherwise.
static boolean hasAttribute(String name)
          Check if this attribute exists in the local scope.
static boolean hasAttribute(String name, int scope)
          Check if this attribute exists in the specified scope.
static boolean hasInstance()
          Used to check if an instance is already set since getInstance() will always return a context.
static void initAsWebContext(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.ServletContext servletContext)
          Deprecated. since 4.5, use WebContextFactory.
static boolean isSystemInstance()
           
static boolean isWebContext()
          Returns true if the current context is set and is an instance of WebContext.
static void login(Subject subject)
           
static void pop()
           
static void push(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
static void release()
          Releases the current thread (if not a system context) and calls the releaseThread() method of the system context.
static void removeAttribute(String name)
          Remove an attribute in the local scope.
static void removeAttribute(String name, int scope)
          Remove an attribute in the specified scope.
static void resetAggregationState()
          Resets the current aggregation state if we're in a WebContext, throws an IllegalStateException otherwise.
static void setAttribute(String name, Object value)
          Set attribute value, scope of the attribute is Context.LOCAL_SCOPE.
static void setAttribute(String name, Object value, int scope)
          Set attribute value, scope of the attribute is defined.
static void setInstance(Context context)
          Set context implementation instance.
static void setLocale(Locale locale)
          Set the locale for the current context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MgnlContext

public MgnlContext()
Do not instantiate this class. The constructor must be public to use discovery

Method Detail

getUser

public static User getUser()
A short cut for the current user.

Returns:
the current user

setLocale

public static void setLocale(Locale locale)
Set the locale for the current context.


getLocale

public static Locale getLocale()
Get the context's locale object.

Returns:
the current locale

getMessages

public static Messages getMessages()

getMessages

public static Messages getMessages(String basename)

login

public static void login(Subject subject)

getHierarchyManager

@Deprecated
public static HierarchyManager getHierarchyManager(String repositoryId)
Deprecated. since 4.5 - use getJCRSession(String)

Get hierarchy manager initialized for this user.


getAccessManager

public static AccessManager getAccessManager(String name)
Get access manager for a resource, usually a workspace.


getQueryManager

@Deprecated
public static QueryManager getQueryManager(String workspaceName)
Deprecated. since 4.5 - use getJCRSession(String) and acquire the JCR query manager directly from the session.

Get access manager for the specified repository on the specified workspace.


getPostedForm

public static MultipartForm getPostedForm()
Get form object assembled by MultipartRequestFilter.

Returns:
multipart form object TODO - move to getAggregationState() ?

getParameter

public static String getParameter(String name)
Get parameter value as string.


getParameterValues

public static String[] getParameterValues(String name)

getParameters

public static Map<String,String> getParameters()
Get parameter value as a Map<String, String>.


getContextPath

public static String getContextPath()
Returns:
the context path.

getAggregationState

public static AggregationState getAggregationState()
Returns the AggregationState if we're in a WebContext, throws an IllegalStateException otherwise.


resetAggregationState

public static void resetAggregationState()
Resets the current aggregation state if we're in a WebContext, throws an IllegalStateException otherwise.


setAttribute

public static void setAttribute(String name,
                                Object value)
Set attribute value, scope of the attribute is Context.LOCAL_SCOPE.


setAttribute

public static void setAttribute(String name,
                                Object value,
                                int scope)
Set attribute value, scope of the attribute is defined.

Parameters:
scope - , highest level of scope from which this attribute is visible.

getAttribute

public static <T> T getAttribute(String name)
Get attribute value.


getAttribute

public static <T> T getAttribute(String name,
                                 int scope)
Get the attribute from the specified scope.


hasAttribute

public static boolean hasAttribute(String name)
Check if this attribute exists in the local scope.


hasAttribute

public static boolean hasAttribute(String name,
                                   int scope)
Check if this attribute exists in the specified scope.


removeAttribute

public static void removeAttribute(String name)
Remove an attribute in the local scope.


removeAttribute

public static void removeAttribute(String name,
                                   int scope)
Remove an attribute in the specified scope.


setInstance

public static void setInstance(Context context)
Set context implementation instance.


getInstance

public static Context getInstance()
Get the current context of this thread.


getWebContext

public static WebContext getWebContext()
Throws an IllegalStateException if the current context is not set, or if it is not an instance of WebContext.

See Also:
getWebContext(String)

getWebContext

public static WebContext getWebContext(String exceptionMessage)
Throws an IllegalStateException if the current context is not set, or if it is not an instance of WebContext. Yes, you can specify the exception message if you want. This is useful if you're calling this from a component which only supports WebContext and still care enough to actually throw an exception with a meaningful message.

See Also:
getWebContext()

getWebContextOrNull

public static WebContext getWebContextOrNull()
Returns the current context if it is set and is an instance of WebContext, returns null otherwise.

Returns:

hasInstance

public static boolean hasInstance()
Used to check if an instance is already set since getInstance() will always return a context.

Returns:
true if an instance was set.

isSystemInstance

public static boolean isSystemInstance()

isWebContext

public static boolean isWebContext()
Returns true if the current context is set and is an instance of WebContext. (it might be wrapped in a ContextDecorator)


getSystemContext

@Deprecated
public static SystemContext getSystemContext()
Deprecated. since 4.5, use IoC, i.e., declare a dependency on SystemContext in your component.

Get Magnolia system context. This context has full permissions over all repositories/ workspaces.


doInSystemContext

@Deprecated
public static void doInSystemContext(MgnlContext.SystemContextOperation op)
Deprecated. since 4.2 - use the Op interface, which can return values, or extend VoidOp.


doInSystemContext

public static <T,E extends Throwable> T doInSystemContext(MgnlContext.Op<T,E> op)
                           throws E extends Throwable
Executes the given operation in the system context and sets it back to the original once done (also if an exception is thrown). Also works if there was no context upon calling. (sets it back to null in this case)

Throws:
E extends Throwable

doInSystemContext

@Deprecated
public static void doInSystemContext(MgnlContext.SystemContextOperation op,
                                                boolean releaseAfterExecution)
Deprecated. since 4.2 - use the Op interface, which can return values, or extend VoidOp.


doInSystemContext

public static <T,E extends Throwable> T doInSystemContext(MgnlContext.Op<T,E> op,
                                                          boolean releaseAfterExecution)
                           throws E extends Throwable
Executes the given operation in the system context and sets it back to the original once done (also if an exception is thrown). Also works if there was no context upon calling (sets it back to null in this case)

Parameters:
releaseAfterExecution - set to true if the context should be released once the execution is done (e.g. in workflow operations or scheduled jobs).
Throws:
E extends Throwable

initAsWebContext

@Deprecated
public static void initAsWebContext(javax.servlet.http.HttpServletRequest request,
                                               javax.servlet.http.HttpServletResponse response,
                                               javax.servlet.ServletContext servletContext)
Deprecated. since 4.5, use WebContextFactory.

Sets this context as a web context.

Parameters:
request - HttpServletRequest
response - HttpServletResponse
servletContext - ServletContext instance

release

public static void release()
Releases the current thread (if not a system context) and calls the releaseThread() method of the system context.


push

public static void push(javax.servlet.http.HttpServletRequest request,
                        javax.servlet.http.HttpServletResponse response)

pop

public static void pop()

getJCRSession

public static javax.jcr.Session getJCRSession(String workspaceName)
                                       throws javax.jcr.LoginException,
                                              javax.jcr.RepositoryException
Note: this is the way to go, if you no longer want to rely on the Content-API.

Parameters:
workspaceName - - repository to get session for
Returns:
a JCR session to the provided repository
Throws:
javax.jcr.LoginException
javax.jcr.RepositoryException

getSubject

public static Subject getSubject()


Copyright © 2003-2012 Magnolia International Ltd.. All Rights Reserved.