Class ThreadContext

java.lang.Object
com.groiss.util.ThreadContext

public class ThreadContext extends Object
This class is designed to hold thread specific information which can then be requested anywhere in the code.
  • Field Details

  • Method Details

    • setThreadLocale

      public static void setThreadLocale(Locale l)
      Set the locale of the thread.
      Parameters:
      l - a locale object
    • getThreadLocale

      public static Locale getThreadLocale()
      Get the thread locale. If the thread has no locale set, the method returns the default locale.
      Returns:
      a Locale object
    • setThreadPrincipal

      public static void setThreadPrincipal(Principal p)
      Set the principal of the thread.
      Parameters:
      p - a Principal object
    • switchThreadPrincipal

      public static SilentCloseable switchThreadPrincipal(Principal p)
      Change the principal of the thread.
      Parameters:
      p - a Principal object
      Returns:
      SilentCloseable may be used in combination with a try-with-resources statement
      try(SilentCloseable closable = switchThreadPrincipal(Principal)) {
              ...
              }
      Upon leaving the block, the principal is automatically re-set to the old value it had when entering the block.
    • switchThreadLocale

      public static SilentCloseable switchThreadLocale(Locale locale)
      Temporarily change the locale of the thread.
      Parameters:
      locale - a Locale object
      Returns:
      SilentCloseable: should be used in combination with a try-with-resources statement
      try(SilentCloseable closable = switchThreadLocale(Locale)) {
              ...
              }
      Upon leaving the block, the locale is automatically re-set to the old value it had when entering the block.
    • switchThreadTimeZone

      public static SilentCloseable switchThreadTimeZone(TimeZone tz)
      Temporarily change the time zone of the thread.
      Parameters:
      tz - a TimeZone object
      Returns:
      SilentCloseable: should be used in combination with a try-with-resources statement
      try(SilentCloseable closable = switchThreadTimeZone(TimeZone)) {
              ...
              }
      Upon leaving the block, the time zone is automatically re-set to the old value it had when entering the block.
    • switchAttribute

      public static SilentCloseable switchAttribute(Object key, Object value)
      Temporarily change an attribute of the thread. If the key is already contained in the context, the old value is replaced.
      Parameters:
      key - key with which the specified value is to be associated
      value - value to be associated with the specified key
      Returns:
      SilentCloseable: should be used in combination with a try-with-resources statement
      try(SilentCloseable closable = switchAttribute(Object,Object)) {
              ...
              }
      Upon leaving the block, the attribute is automatically re-set to the old value it had when entering the block; if there was no such value, the attribute is removed.
    • getThreadPrincipal

      public static <T extends Principal> T getThreadPrincipal()
      Get the thread principal, the user who performs the action.
      Returns:
      a Principal object
    • setSessionId

      public static void setSessionId(String s)
      Set the session id of the thread.
      Parameters:
      s - a string object
    • getSessionId

      public static String getSessionId()
      Get the thread sessionId.
      Returns:
      a String object
    • setThreadTimeZone

      public static void setThreadTimeZone(TimeZone t)
      Set the TimeZone of the thread.
      Parameters:
      t - a TimeZone object
    • getThreadTimeZone

      public static TimeZone getThreadTimeZone()
      Get the thread TimeZone
      Returns:
      a TimeZone object
    • getSessions

      public static ThreadLocal2<String> getSessions()
      Get the sessions.
      Returns:
      a ThreadLocal2 object
    • setPrivileged

      public static void setPrivileged()
      Set the thread privileged.
    • isPrivileged

      public static boolean isPrivileged()
      Is the thread privileged.
      Returns:
      true if user of this thread is sysadm
    • setThreadRequest

      public static void setThreadRequest(HttpServletRequest req)
      Set the HttpServletRequest of the thread.
      Parameters:
      req - a HttpServletRequest object
    • getThreadRequest

      public static HttpServletRequest getThreadRequest()
      Get the HttpServletRequest of this thread.
      Returns:
      a HttpServletRequest object
    • setThreadResponse

      public static void setThreadResponse(HttpServletResponse req)
      Set the HttpServletResponse of the thread.
      Parameters:
      req - a HttpServletResponse object
    • getThreadResponse

      public static HttpServletResponse getThreadResponse()
      Get the HttpServletResponse of this thread. Be careful when manipulating the response. Setting cookies, headers etc. can only be done on uncommitted responses. Calling ServletResponse.getOutputStream() or ServletResponse.getWriter() in a servletcontext where actually a Page is returned, is not recommended
      Returns:
      a HttpServletResponse object
    • getSessionType

      public static short getSessionType()
      Get the type of the Session
      Returns:
      the Session type.
    • setSessionType

      public static void setSessionType(short s)
      Set the type of this thread.
      Parameters:
      s - either HTTP_SESSION, RMI_SESSION or INTERNAL_SESSION
    • setAttribute

      public static void setAttribute(Object key, Object value)
      Adds a key-value pair to the thread's context. If the key is already contained, the old value is replaced.
      Parameters:
      key - key with which the specified value is to be associated
      value - value to be associated with the specified key
    • getAttribute

      public static <T> T getAttribute(Object key)
      Gets the value identified by the passed key
      Parameters:
      key - key whose value should be retrieved
      Returns:
      the value object
    • removeAttribute

      public static void removeAttribute(Object key)
      Removes the key-value pair identified by the passed key
      Parameters:
      key - key whose value should be removed
    • getAttributeKeys

      public static Iterator<Object> getAttributeKeys()
      Returns an iterator over all keys which were set via method or #switchAttribute(Object, Object)
      Returns:
      iterator containing the key objects or null if no attributes were set
    • clean

      public static void clean()
      Clean the thread context.
    • addThreadLocale

      public static void addThreadLocale(ThreadLocal<?> local)