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 FacesContext} instance for each processing thread.</p>
056: */
057: private static ThreadLocal instance = new ThreadLocal() {
058: protected Object initialValue() {
059: return (null);
060: }
061: };
062:
063: /**
064: * @return a Map stored in ThreadLocal storage. This may
065: * be used by methods of this class to minimize the performance
066: * impact for operations that may take place multiple times on a given
067: * Thread instance.
068: */
069:
070: private static Map getCurrentInstance() {
071: Map result = (Map) instance.get();
072: if (null == result) {
073: result = new HashMap();
074: setCurrentInstance(result);
075: }
076: return result;
077:
078: }
079:
080: /**
081: * <p>Replace the Map with the argument context.</p>
082: *
083: * @param context the Map to be stored in ThreadLocal storage.
084: */
085:
086: private static void setCurrentInstance(Map context) {
087:
088: instance.set(context);
089:
090: }
091:
092: /*
093: * <p>Convenience method, calls through to
094: * {@link #getExceptionMessageString(javax.el.ELContext,java.lang.String,Object []).
095: * </p>
096: *
097: * @param context the ELContext from which the Locale for this message
098: * is extracted.
099: *
100: * @param messageId the messageId String in the ResourceBundle
101: *
102: * @return a localized String for the argument messageId
103: */
104:
105: public static String getExceptionMessageString(ELContext context,
106: String messageId) {
107: return getExceptionMessageString(context, messageId, null);
108: }
109:
110: /*
111: * <p>Return a Localized message String suitable for use as an Exception message.
112: * Examine the argument <code>context</code> for a <code>Locale</code>. If
113: * not present, use <code>Locale.getDefault()</code>. Load the
114: * <code>ResourceBundle</code> "javax.el.Messages" using that locale. Get
115: * the message string for argument <code>messageId</code>. If not found
116: * return "Missing Resource in EL implementation ??? messageId ???"
117: * with messageId substituted with the runtime
118: * value of argument <code>messageId</code>. If found, and argument
119: * <code>params</code> is non-null, format the message using the
120: * params. If formatting fails, return a sensible message including
121: * the <code>messageId</code>. If argument <code>params</code> is
122: * <code>null</code>, skip formatting and return the message directly, otherwise
123: * return the formatted message.</p>
124: *
125: * @param context the ELContext from which the Locale for this message
126: * is extracted.
127: *
128: * @param messageId the messageId String in the ResourceBundle
129: *
130: * @param params parameters to the message
131: *
132: * @return a localized String for the argument messageId
133: */
134:
135: public static String getExceptionMessageString(ELContext context,
136: String messageId, Object[] params) {
137: String result = "";
138: Locale locale = null;
139:
140: if (null == context || null == messageId) {
141: return result;
142: }
143:
144: if (null == (locale = context.getLocale())) {
145: locale = Locale.getDefault();
146: }
147: if (null != locale) {
148: Map threadMap = getCurrentInstance();
149: ResourceBundle rb = null;
150: if (null == (rb = (ResourceBundle) threadMap.get(locale
151: .toString()))) {
152: rb = ResourceBundle.getBundle(
153: "javax.el.PrivateMessages", locale);
154: threadMap.put(locale.toString(), rb);
155: }
156: if (null != rb) {
157: try {
158: result = rb.getString(messageId);
159: if (null != params) {
160: result = MessageFormat.format(result, params);
161: }
162: } catch (IllegalArgumentException iae) {
163: result = "Can't get localized message: parameters to message appear to be incorrect. Message to format: "
164: + messageId;
165: } catch (MissingResourceException mre) {
166: result = "Missing Resource in EL implementation: ???"
167: + messageId + "???";
168: } catch (Exception e) {
169: result = "Exception resolving message in EL implementation: ???"
170: + messageId + "???";
171: }
172: }
173: }
174:
175: return result;
176: }
177:
178: }
|