001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * glassfish/bootstrap/legal/CDDLv1.0.txt or
009: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
010: * See the License for the specific language governing
011: * permissions and limitations under the License.
012: *
013: * When distributing Covered Code, include this CDDL
014: * HEADER in each file and include the License file at
015: * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
016: * add the following below this CDDL HEADER, with the
017: * fields enclosed by brackets "[]" replaced with your
018: * own identifying information: Portions Copyright [yyyy]
019: * [name of copyright owner]
020: *
021: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
022: */
023:
024: package javax.el;
025:
026: import java.text.MessageFormat;
027: import java.util.HashMap;
028: import java.util.Locale;
029: import java.util.Map;
030: import java.util.MissingResourceException;
031: import java.util.ResourceBundle;
032:
033: /**
034: *
035: * <p>Utility methods for this portion of the EL implementation</p>
036: *
037: * <p>Methods on this class use a Map instance stored in ThreadLocal storage
038: * to minimize the performance impact on operations that take place multiple
039: * times on a single Thread. The keys and values of the Map
040: * are implementation private.</p>
041: *
042: * @author edburns
043: */
044: class ELUtil {
045:
046: /**
047: * <p>This class may not be constructed.</p>
048: */
049:
050: private ELUtil() {
051: }
052:
053: /**
054: * <p>The <code>ThreadLocal</code> variable used to record the
055: * {@link javax.faces.context.FacesContext} instance for each
056: * processing thread.</p>
057: */
058: private static ThreadLocal instance = new ThreadLocal() {
059: protected Object initialValue() {
060: return (null);
061: }
062: };
063:
064: /**
065: * @return a Map stored in ThreadLocal storage. This may
066: * be used by methods of this class to minimize the performance
067: * impact for operations that may take place multiple times on a given
068: * Thread instance.
069: */
070:
071: private static Map getCurrentInstance() {
072: Map result = (Map) instance.get();
073: if (null == result) {
074: result = new HashMap();
075: setCurrentInstance(result);
076: }
077: return result;
078:
079: }
080:
081: /**
082: * <p>Replace the Map with the argument context.</p>
083: *
084: * @param context the Map to be stored in ThreadLocal storage.
085: */
086:
087: private static void setCurrentInstance(Map context) {
088:
089: instance.set(context);
090:
091: }
092:
093: /*
094: * <p>Convenience method, calls through to
095: * {@link #getExceptionMessageString(javax.el.ELContext,java.lang.String,Object []).
096: * </p>
097: *
098: * @param context the ELContext from which the Locale for this message
099: * is extracted.
100: *
101: * @param messageId the messageId String in the ResourceBundle
102: *
103: * @return a localized String for the argument messageId
104: */
105:
106: public static String getExceptionMessageString(ELContext context,
107: String messageId) {
108: return getExceptionMessageString(context, messageId, null);
109: }
110:
111: /*
112: * <p>Return a Localized message String suitable for use as an Exception message.
113: * Examine the argument <code>context</code> for a <code>Locale</code>. If
114: * not present, use <code>Locale.getDefault()</code>. Load the
115: * <code>ResourceBundle</code> "javax.el.Messages" using that locale. Get
116: * the message string for argument <code>messageId</code>. If not found
117: * return "Missing Resource in EL implementation ??? messageId ???"
118: * with messageId substituted with the runtime
119: * value of argument <code>messageId</code>. If found, and argument
120: * <code>params</code> is non-null, format the message using the
121: * params. If formatting fails, return a sensible message including
122: * the <code>messageId</code>. If argument <code>params</code> is
123: * <code>null</code>, skip formatting and return the message directly, otherwise
124: * return the formatted message.</p>
125: *
126: * @param context the ELContext from which the Locale for this message
127: * is extracted.
128: *
129: * @param messageId the messageId String in the ResourceBundle
130: *
131: * @param params parameters to the message
132: *
133: * @return a localized String for the argument messageId
134: */
135:
136: public static String getExceptionMessageString(ELContext context,
137: String messageId, Object[] params) {
138: String result = "";
139: Locale locale = null;
140:
141: if (null == context || null == messageId) {
142: return result;
143: }
144:
145: if (null == (locale = context.getLocale())) {
146: locale = Locale.getDefault();
147: }
148: if (null != locale) {
149: Map threadMap = getCurrentInstance();
150: ResourceBundle rb = null;
151: if (null == (rb = (ResourceBundle) threadMap.get(locale
152: .toString()))) {
153: rb = ResourceBundle.getBundle(
154: "javax.el.PrivateMessages", locale);
155: threadMap.put(locale.toString(), rb);
156: }
157: if (null != rb) {
158: try {
159: result = rb.getString(messageId);
160: if (null != params) {
161: result = MessageFormat.format(result, params);
162: }
163: } catch (IllegalArgumentException iae) {
164: result = "Can't get localized message: parameters to message appear to be incorrect. Message to format: "
165: + messageId;
166: } catch (MissingResourceException mre) {
167: result = "Missing Resource in EL implementation: ???"
168: + messageId + "???";
169: } catch (Exception e) {
170: result = "Exception resolving message in EL implementation: ???"
171: + messageId + "???";
172: }
173: }
174: }
175:
176: return result;
177: }
178:
179: }
|