KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > el > ELUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package javax.el;
25
26 import java.text.MessageFormat JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Locale JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.MissingResourceException JavaDoc;
31 import java.util.ResourceBundle JavaDoc;
32
33 /**
34  *
35  * <p>Utility methods for this portion of the EL implementation</p>
36  *
37  * <p>Methods on this class use a Map instance stored in ThreadLocal storage
38  * to minimize the performance impact on operations that take place multiple
39  * times on a single Thread. The keys and values of the Map
40  * are implementation private.</p>
41  *
42  * @author edburns
43  */

44 class ELUtil {
45     
46     /**
47      * <p>This class may not be constructed.</p>
48      */

49     
50     private ELUtil() {
51     }
52     
53     /**
54      * <p>The <code>ThreadLocal</code> variable used to record the
55      * {@link FacesContext} instance for each processing thread.</p>
56      */

57     private static ThreadLocal JavaDoc instance = new ThreadLocal JavaDoc() {
58             protected Object JavaDoc initialValue() { return (null); }
59         };
60         
61     /**
62      * @return a Map stored in ThreadLocal storage. This may
63      * be used by methods of this class to minimize the performance
64      * impact for operations that may take place multiple times on a given
65      * Thread instance.
66      */

67
68     private static Map JavaDoc getCurrentInstance() {
69         Map JavaDoc result = (Map JavaDoc) instance.get();
70         if (null == result) {
71             result = new HashMap JavaDoc();
72             setCurrentInstance(result);
73         }
74         return result;
75
76     }
77     
78     /**
79      * <p>Replace the Map with the argument context.</p>
80      *
81      * @param context the Map to be stored in ThreadLocal storage.
82      */

83
84     private static void setCurrentInstance(Map JavaDoc context) {
85
86         instance.set(context);
87
88     }
89     
90     /*
91      * <p>Convenience method, calls through to
92      * {@link #getExceptionMessageString(javax.el.ELContext,java.lang.String,Object []).
93      * </p>
94      *
95      * @param context the ELContext from which the Locale for this message
96      * is extracted.
97      *
98      * @param messageId the messageId String in the ResourceBundle
99      *
100      * @return a localized String for the argument messageId
101      */

102     
103     public static String JavaDoc getExceptionMessageString(ELContext context, String JavaDoc messageId) {
104         return getExceptionMessageString(context, messageId, null);
105     }
106     
107     /*
108      * <p>Return a Localized message String suitable for use as an Exception message.
109      * Examine the argument <code>context</code> for a <code>Locale</code>. If
110      * not present, use <code>Locale.getDefault()</code>. Load the
111      * <code>ResourceBundle</code> "javax.el.Messages" using that locale. Get
112      * the message string for argument <code>messageId</code>. If not found
113      * return "Missing Resource in EL implementation ??? messageId ???"
114      * with messageId substituted with the runtime
115      * value of argument <code>messageId</code>. If found, and argument
116      * <code>params</code> is non-null, format the message using the
117      * params. If formatting fails, return a sensible message including
118      * the <code>messageId</code>. If argument <code>params</code> is
119      * <code>null</code>, skip formatting and return the message directly, otherwise
120      * return the formatted message.</p>
121      *
122      * @param context the ELContext from which the Locale for this message
123      * is extracted.
124      *
125      * @param messageId the messageId String in the ResourceBundle
126      *
127      * @param params parameters to the message
128      *
129      * @return a localized String for the argument messageId
130      */

131     
132     public static String JavaDoc getExceptionMessageString(ELContext context,
133             String JavaDoc messageId,
134             Object JavaDoc [] params) {
135         String JavaDoc result = "";
136         Locale JavaDoc locale = null;
137         
138         if (null == context || null == messageId) {
139             return result;
140         }
141         
142         if (null == (locale = context.getLocale())) {
143             locale = Locale.getDefault();
144         }
145         if (null != locale) {
146             Map JavaDoc threadMap = getCurrentInstance();
147             ResourceBundle JavaDoc rb = null;
148             if (null == (rb = (ResourceBundle JavaDoc)
149             threadMap.get(locale.toString()))) {
150                 rb = ResourceBundle.getBundle("javax.el.PrivateMessages",
151                                               locale);
152                 threadMap.put(locale.toString(), rb);
153             }
154             if (null != rb) {
155                 try {
156                     result = rb.getString(messageId);
157                     if (null != params) {
158                         result = MessageFormat.format(result, params);
159                     }
160                 } catch (IllegalArgumentException JavaDoc iae) {
161                     result = "Can't get localized message: parameters to message appear to be incorrect. Message to format: " + messageId;
162                 } catch (MissingResourceException JavaDoc mre) {
163                     result = "Missing Resource in EL implementation: ???" + messageId + "???";
164                 } catch (Exception JavaDoc e) {
165                     result = "Exception resolving message in EL implementation: ???" + messageId + "???";
166                 }
167             }
168         }
169         
170         return result;
171     }
172         
173     
174 }
175
Popular Tags