com.webmethods.caf.faces.context
Class ContextUtils

java.lang.Object
  extended by com.webmethods.caf.faces.context.ContextUtils

public class ContextUtils
extends Object

Utility functions for dealing with context/user information. Functions include logging, recording messages to the FacesContext, retrieving resource bundle strings and loading resources from the classpath.


Nested Class Summary
static class ContextUtils.FacesContextDetails
          Stores information about FacesContext that can be used to switch back to a previous FacesContext
 
Field Summary
protected static String ATTR_APP_VERSION
           
static String DEFAULT_CURRENCY_FORMAT_EXPRESSION
           
static String DEFAULT_DATE_FORMAT_EXPRESSION
           
static String DEFAULT_NUMBER_FORMAT_EXPRESSION
           
static String DEFAULT_TIME_FORMAT_EXPRESSION
           
static String DEFAULT_TIME_ZONE_EXPRESSION
           
static String FACES_RESOURCES
           
protected static String ICONTEXT_KEY
           
protected static DateFormat LAST_MODIFIED_VERSION_FORMAT
           
protected static Pattern RE_BUILD_TIMESTAMP
           
protected static Pattern RE_BUILD_VERSION
           
protected static Pattern RE_DATE_TIME_ORDER_ELEMENT
           
protected static Pattern RE_FORMATTED_MESSAGE_ELEMENT
           
 
Constructor Summary
ContextUtils()
           
 
Method Summary
protected static String _applyUserPrefsToMessagePattern(FacesContext ctx, String message, Object[] args)
          Applies user preferences for date/time/number formatting to message pattern.
static ContextUtils.FacesContextDetails clearFacesContextDetails()
          Clears out the current faces context and other related objects
static void error(FacesMessage.Severity severity, String message, String details)
           
static void error(String message)
           
static void error(String message, String details)
           
static void error(String message, Throwable throwable)
           
static void error(Throwable throwable)
           
static ResourceBundle findBundle(FacesContext ctx, String name)
          Finds a resource bundle by name using the current response locale
static String formatMessage(FacesContext ctx, String message, Object[] args)
          Formats specified message with the specified arguments using MessageFormat.
static String getAppVersion(FacesContext context)
          Calculate the application version of the current context.
static FacesContext getFacesContext()
          Retrieve the current FacesContext
static InputStream getResourceInputStream(String relPath)
          Returns an InputStream for a resource at the given path
static iContext getResponseIContext(FacesContext ctx)
          Calculates the locale to use for the response.
static Locale getResponseLocale(FacesContext ctx)
          Calculates the locale to use for the response.
static ServletContext getServletContext(String webAppName)
          Gets the servlet context that corresponds to the supplied servlet context path
static ClassLoader getWebAppClassLoader(ServletContext servletContext)
          Gets the classloader for the supplied servlet context
static void initializeResponseWriter(FacesContext context)
          Initialize the ResponseWriter for the servlet context if it has not already been created.
static boolean isCafView(FacesContext ctx)
          Helper to check if the faces context is for a CAF view
static boolean isCafView(FacesContext ctx, String viewId)
          Helper to check if the faces context is for a CAF view
static String localizeMessage(Class bundleClass, String key)
          Returns the message from the specified resource bundle with the specified key.
static String localizeMessage(FacesContext ctx, Class bundleClass, String key)
          Returns the message from the specified resource bundle with the specified key.
static String localizeMessage(FacesContext ctx, Class bundleClass, String key, Object[] args)
          Returns the message from the specified resource bundle with the specified key, formatted with the specified MessageFormat arguments.
static String localizeMessage(FacesContext ctx, Class bundleClass, String key, Object[] args, boolean prependKey)
          Returns the message from the specified resource bundle with the specified key, formatted with the specified MessageFormat arguments.
static String localizeMessage(FacesContext ctx, String bundleName, String key, Object[] args)
          Returns the message from the specified resource bundle with the specified key, formatted with the specified MessageFormat arguments.
static String localizeMessage(FacesContext ctx, String bundleName, String key, Object[] args, boolean prependKey)
          Returns the message from the specified resource bundle with the specified key, formatted with the specified MessageFormat arguments.
static String localizeMessage(FacesContext ctx, String bundleName, String key, Object[] args, boolean prependKey, boolean catchMissingResourceException)
          Returns the message from the specified resource bundle with the specified key, formatted with the specified MessageFormat arguments.
static String localizeMessage(String key)
          Returns the message from the default faces resource bundle with the specified key.
static String localizeMessage(String key, Object[] args)
          Returns the formatted message from the default faces resource bundle with the specified key
static String localizeMessage(String key, Object[] args, boolean prependKey)
          Returns the message from the default faces resource bundle with the specified key.
static void restoreFacesContextDetails(ContextUtils.FacesContextDetails details)
          Restores the current faces context and other related objects
static void setCurrentFacesContext(FacesContext facesContext)
          Set the current faces context for this thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FACES_RESOURCES

public static final String FACES_RESOURCES
See Also:
Constant Field Values

DEFAULT_CURRENCY_FORMAT_EXPRESSION

public static final String DEFAULT_CURRENCY_FORMAT_EXPRESSION
See Also:
Constant Field Values

DEFAULT_NUMBER_FORMAT_EXPRESSION

public static final String DEFAULT_NUMBER_FORMAT_EXPRESSION
See Also:
Constant Field Values

DEFAULT_DATE_FORMAT_EXPRESSION

public static final String DEFAULT_DATE_FORMAT_EXPRESSION
See Also:
Constant Field Values

DEFAULT_TIME_FORMAT_EXPRESSION

public static final String DEFAULT_TIME_FORMAT_EXPRESSION
See Also:
Constant Field Values

DEFAULT_TIME_ZONE_EXPRESSION

public static final String DEFAULT_TIME_ZONE_EXPRESSION
See Also:
Constant Field Values

ICONTEXT_KEY

protected static final String ICONTEXT_KEY

RE_FORMATTED_MESSAGE_ELEMENT

protected static final Pattern RE_FORMATTED_MESSAGE_ELEMENT

RE_DATE_TIME_ORDER_ELEMENT

protected static final Pattern RE_DATE_TIME_ORDER_ELEMENT

ATTR_APP_VERSION

protected static final String ATTR_APP_VERSION

RE_BUILD_TIMESTAMP

protected static final Pattern RE_BUILD_TIMESTAMP

RE_BUILD_VERSION

protected static final Pattern RE_BUILD_VERSION

LAST_MODIFIED_VERSION_FORMAT

protected static final DateFormat LAST_MODIFIED_VERSION_FORMAT
Constructor Detail

ContextUtils

public ContextUtils()
Method Detail

localizeMessage

public static String localizeMessage(String key)
Returns the message from the default faces resource bundle with the specified key.

Parameters:
key - the resource bundle key.

localizeMessage

public static String localizeMessage(String key,
                                     Object[] args)
Returns the formatted message from the default faces resource bundle with the specified key

Parameters:
key - Resource bundle key as String
args - MessageFormat formatted string arguments as an Object[], or null.
Returns:
text from the resource bundle after string arguments have been applied

localizeMessage

public static String localizeMessage(String key,
                                     Object[] args,
                                     boolean prependKey)
Returns the message from the default faces resource bundle with the specified key.

Parameters:
key - Resource bundle key as String
args - MessageFormat formatted string arguments as an Object[], or null.
prependKey - True to prepend the key to formatted message (for an error message).
Returns:
text from the resource bundle after string arguments have been applied

localizeMessage

public static String localizeMessage(Class bundleClass,
                                     String key)
Returns the message from the specified resource bundle with the specified key.

Parameters:
bundleClass - Resource bundle class, or null for default faces resource bundle class.
key - Resource bundle key as String
Returns:
the text from the resource bundle

localizeMessage

public static String localizeMessage(FacesContext ctx,
                                     Class bundleClass,
                                     String key)
Returns the message from the specified resource bundle with the specified key.

Parameters:
ctx - Current context, or null for current tls instance.
bundleClass - Resource bundle class, or null for default faces resource bundle class.
key - Resource bundle key as String
Returns:
text from the resource bundle

localizeMessage

public static String localizeMessage(FacesContext ctx,
                                     Class bundleClass,
                                     String key,
                                     Object[] args)
Returns the message from the specified resource bundle with the specified key, formatted with the specified MessageFormat arguments.

Parameters:
ctx - Current context, or null for current tls instance.
bundleClass - Resource bundle class, or null for default faces resource bundle class.
key - Resource bundle key as String
args - MessageFormat formatted string arguments as an Object[], or null.
Returns:
the text from the resource bundle after string arguments have been applied

localizeMessage

public static String localizeMessage(FacesContext ctx,
                                     Class bundleClass,
                                     String key,
                                     Object[] args,
                                     boolean prependKey)
Returns the message from the specified resource bundle with the specified key, formatted with the specified MessageFormat arguments.

Parameters:
ctx - Current context, or null for current tls instance.
bundleClass - Resource bundle class, or null for default faces resource bundle class.
key - Resource bundle key as String
args - MessageFormat formatted string arguments as an Object[], or null.
prependKey - True to prepend key to formatted message (for an error message).
Returns:
the text from the resource bundle after string arguments have been applied

localizeMessage

public static String localizeMessage(FacesContext ctx,
                                     String bundleName,
                                     String key,
                                     Object[] args)
Returns the message from the specified resource bundle with the specified key, formatted with the specified MessageFormat arguments.

Parameters:
ctx - Current context, or null for current tls instance.
bundleName - Resource bundle name, or null for default faces resource bundle class.
key - Resource bundle key as String
args - MessageFormat formatted string arguments as an Object[], or null.
Returns:
the text from the resource bundle after string arguments have been applied

localizeMessage

public static String localizeMessage(FacesContext ctx,
                                     String bundleName,
                                     String key,
                                     Object[] args,
                                     boolean prependKey)
                              throws MissingResourceException
Returns the message from the specified resource bundle with the specified key, formatted with the specified MessageFormat arguments.

Parameters:
ctx - Current context, or null for current tls instance.
bundleName - Resource bundle name, or null for default faces resource bundle class.
key - Resource bundle key as String
args - MessageFormat formatted string arguments as an Object[], or null.
prependKey - True to prepend key to formatted message (for an error message).
Returns:
the text from the resource bundle after string arguments have been applied
Throws:
MissingResourceException - if bundle could not be found OR if resource could not be found in the bundle.

localizeMessage

public static String localizeMessage(FacesContext ctx,
                                     String bundleName,
                                     String key,
                                     Object[] args,
                                     boolean prependKey,
                                     boolean catchMissingResourceException)
Returns the message from the specified resource bundle with the specified key, formatted with the specified MessageFormat arguments.

Parameters:
ctx - Current context, or null for current tls instance.
bundleName - Resource bundle name, or null for default faces resource bundle class.
key - Resource bundle key as String
args - MessageFormat formatted string arguments as an Object[], or null.
prependKey - True to prepend key to formatted message (for an error message).
catchMissingResourceException - true to catch any MissingResourceExceptions
Returns:
the text from the resource bundle after string arguments have been applied

formatMessage

public static String formatMessage(FacesContext ctx,
                                   String message,
                                   Object[] args)
Formats specified message with the specified arguments using MessageFormat. Uses current user's locale, plus user's preferred default date/time/number patterns.

Parameters:
ctx - Current faces context.
message - Message to format (ie "Distance is {0,number} km.").
args - Arguments with which to format message.
Returns:
Formatted message. Never null.

_applyUserPrefsToMessagePattern

protected static String _applyUserPrefsToMessagePattern(FacesContext ctx,
                                                        String message,
                                                        Object[] args)
Applies user preferences for date/time/number formatting to message pattern.

Parameters:
ctx - Current faces context.
message - Message to format (ie "Distance is {0,number} km.").
args - (optional) Arguments with which to format message.
Returns:
Updated message pattern. Never null.

getResponseLocale

public static Locale getResponseLocale(FacesContext ctx)
Calculates the locale to use for the response.

Parameters:
ctx - Current context, or null for current thread instance.
Returns:
calculated Locale

getResponseIContext

public static iContext getResponseIContext(FacesContext ctx)
Calculates the locale to use for the response.

Parameters:
ctx - Current context, or null for current thread instance.
Returns:
calculated Locale

findBundle

public static ResourceBundle findBundle(FacesContext ctx,
                                        String name)
                                 throws MissingResourceException
Finds a resource bundle by name using the current response locale

Parameters:
ctx - Current context, or null for current thread instance.
name - Name of the resource bundle as String
Throws:
MissingResourceException - if bundle could not be found.

getResourceInputStream

public static InputStream getResourceInputStream(String relPath)
Returns an InputStream for a resource at the given path

Parameters:
relPath - Relative path to the resource as String
Returns:
InputStream containing the contents of the specified resource

error

public static void error(FacesMessage.Severity severity,
                         String message,
                         String details)

error

public static void error(String message,
                         String details)

error

public static void error(String message)

error

public static void error(Throwable throwable)

error

public static void error(String message,
                         Throwable throwable)

getFacesContext

public static FacesContext getFacesContext()
Retrieve the current FacesContext

Returns:
the current FacesContext for this thread

getAppVersion

public static String getAppVersion(FacesContext context)
Calculate the application version of the current context.

Parameters:
context - Current faces context.
Returns:
Version string. Null or empty string to signal unknown.

initializeResponseWriter

public static void initializeResponseWriter(FacesContext context)
                                     throws IOException
Initialize the ResponseWriter for the servlet context if it has not already been created.

Parameters:
context - the context to initialize.
Throws:
IOException

isCafView

public static boolean isCafView(FacesContext ctx)
Helper to check if the faces context is for a CAF view


isCafView

public static boolean isCafView(FacesContext ctx,
                                String viewId)
Helper to check if the faces context is for a CAF view


getServletContext

public static ServletContext getServletContext(String webAppName)
                                        throws Exception
Gets the servlet context that corresponds to the supplied servlet context path

Parameters:
webAppName - the servlet context path to match
Returns:
the found servlet context or null if no match
Throws:
Exception

getWebAppClassLoader

public static ClassLoader getWebAppClassLoader(ServletContext servletContext)
Gets the classloader for the supplied servlet context

Parameters:
servletContext -
Returns:

clearFacesContextDetails

public static ContextUtils.FacesContextDetails clearFacesContextDetails()
Clears out the current faces context and other related objects

Returns:
details of the previous faces context

restoreFacesContextDetails

public static void restoreFacesContextDetails(ContextUtils.FacesContextDetails details)
Restores the current faces context and other related objects

Parameters:
details - details of the previous faces context

setCurrentFacesContext

public static void setCurrentFacesContext(FacesContext facesContext)
Set the current faces context for this thread. NOTE: It is usually safer to use the ContextUtils.clearFacesContextDetails() + ContextUtils.restoreFacesContextDetails(FacesContextDetails) instead.

Parameters:
facesContext - the faces context for this thread