KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > carstore > MessageFactory


1 /*
2  * $Id: MessageFactory.java,v 1.1 2005/04/26 17:41:16 russo Exp $
3  */

4
5 /*
6  * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
7  *
8  * Redistribution and use in source and binary forms, with or
9  * without modification, are permitted provided that the following
10  * conditions are met:
11  *
12  * - Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * - Redistribution in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials
18  * provided with the distribution.
19  *
20  * Neither the name of Sun Microsystems, Inc. or the names of
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * This software is provided "AS IS," without a warranty of any
25  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
26  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
27  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
28  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
29  * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
30  * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
31  * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
32  * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
33  * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
34  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
35  * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
36  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
37  *
38  * You acknowledge that this software is not designed, licensed or
39  * intended for use in the design, construction, operation or
40  * maintenance of any nuclear facility.
41  */

42
43 package carstore;
44
45 import javax.faces.application.Application;
46 import javax.faces.application.FacesMessage;
47 import javax.faces.context.FacesContext;
48
49 import java.text.MessageFormat JavaDoc;
50 import java.util.Locale JavaDoc;
51 import java.util.MissingResourceException JavaDoc;
52 import java.util.ResourceBundle JavaDoc;
53
54 /**
55  * <p>supported filters: <code>package</code> and
56  * <code>protection</code>.</p>
57  */

58
59 public class MessageFactory extends Object JavaDoc {
60
61     //
62
// Protected Constants
63
//
64

65     //
66
// Class Variables
67
//
68

69     //
70
// Instance Variables
71
//
72

73     // Attribute Instance Variables
74

75     // Relationship Instance Variables
76

77
78     //
79
// Constructors and Initializers
80
//
81

82     private MessageFactory() {
83     }
84
85     //
86
// Class methods
87
//
88

89     //
90
// General Methods
91
//
92

93     public static String JavaDoc substituteParams(Locale JavaDoc locale, String JavaDoc msgtext, Object JavaDoc params[]) {
94         String JavaDoc localizedStr = null;
95
96         if (params == null || msgtext == null) {
97             return msgtext;
98         }
99         StringBuffer JavaDoc b = new StringBuffer JavaDoc(100);
100         MessageFormat JavaDoc mf = new MessageFormat JavaDoc(msgtext);
101         if (locale != null) {
102             mf.setLocale(locale);
103             b.append(mf.format(params));
104             localizedStr = b.toString();
105         }
106         return localizedStr;
107     }
108
109
110     /**
111      * This version of getMessage() is used in the RI for localizing RI
112      * specific messages.
113      */

114
115     public static FacesMessage getMessage(String JavaDoc messageId, Object JavaDoc params[]) {
116         Locale JavaDoc locale = null;
117         FacesContext context = FacesContext.getCurrentInstance();
118         // context.getViewRoot() may not have been initialized at this point.
119
if (context != null && context.getViewRoot() != null) {
120             locale = context.getViewRoot().getLocale();
121             if (locale == null) {
122                 locale = Locale.getDefault();
123             }
124         } else {
125             locale = Locale.getDefault();
126         }
127
128         return getMessage(locale, messageId, params);
129     }
130
131
132     public static FacesMessage getMessage(Locale JavaDoc locale, String JavaDoc messageId,
133                                           Object JavaDoc params[]) {
134         FacesMessage result = null;
135         String JavaDoc
136             summary = null,
137             detail = null,
138             bundleName = null;
139         ResourceBundle JavaDoc bundle = null;
140
141         // see if we have a user-provided bundle
142
if (null != (bundleName = getApplication().getMessageBundle())) {
143             if (null !=
144                 (bundle =
145                 ResourceBundle.getBundle(bundleName, locale,
146                                          getCurrentLoader(bundleName)))) {
147                 // see if we have a hit
148
try {
149                     summary = bundle.getString(messageId);
150                 } catch (MissingResourceException JavaDoc e) {
151                 }
152             }
153         }
154
155         // we couldn't find a summary in the user-provided bundle
156
if (null == summary) {
157             // see if we have a summary in the app provided bundle
158
bundle = ResourceBundle.getBundle(FacesMessage.FACES_MESSAGES,
159                                               locale,
160                                               getCurrentLoader(bundleName));
161             if (null == bundle) {
162                 throw new NullPointerException JavaDoc();
163             }
164             // see if we have a hit
165
try {
166                 summary = bundle.getString(messageId);
167             } catch (MissingResourceException JavaDoc e) {
168             }
169         }
170
171         // we couldn't find a summary anywhere! Return null
172
if (null == summary) {
173             return null;
174         }
175
176         // At this point, we have a summary and a bundle.
177
if (null == summary || null == bundle) {
178             throw new NullPointerException JavaDoc();
179         }
180         summary = substituteParams(locale, summary, params);
181
182         try {
183             detail = substituteParams(locale,
184                                       bundle.getString(messageId + "_detail"),
185                                       params);
186         } catch (MissingResourceException JavaDoc e) {
187         }
188
189         return (new FacesMessage(summary, detail));
190     }
191
192
193     //
194
// Methods from MessageFactory
195
//
196
public static FacesMessage getMessage(FacesContext context, String JavaDoc messageId) {
197         return getMessage(context, messageId, null);
198     }
199
200
201     public static FacesMessage getMessage(FacesContext context, String JavaDoc messageId,
202                                           Object JavaDoc params[]) {
203         if (context == null || messageId == null) {
204             throw new NullPointerException JavaDoc(
205                 "One or more parameters could be null");
206         }
207         Locale JavaDoc locale = null;
208         // viewRoot may not have been initialized at this point.
209
if (context != null && context.getViewRoot() != null) {
210             locale = context.getViewRoot().getLocale();
211         } else {
212             locale = Locale.getDefault();
213         }
214         if (null == locale) {
215             throw new NullPointerException JavaDoc();
216         }
217         FacesMessage message = getMessage(locale, messageId, params);
218         if (message != null) {
219             return message;
220         }
221         locale = Locale.getDefault();
222         return (getMessage(locale, messageId, params));
223     }
224
225
226     public static FacesMessage getMessage(FacesContext context, String JavaDoc messageId,
227                                           Object JavaDoc param0) {
228         return getMessage(context, messageId, new Object JavaDoc[]{param0});
229     }
230
231
232     public static FacesMessage getMessage(FacesContext context, String JavaDoc messageId,
233                                           Object JavaDoc param0, Object JavaDoc param1) {
234         return getMessage(context, messageId, new Object JavaDoc[]{param0, param1});
235     }
236
237
238     public static FacesMessage getMessage(FacesContext context, String JavaDoc messageId,
239                                           Object JavaDoc param0, Object JavaDoc param1,
240                                           Object JavaDoc param2) {
241         return getMessage(context, messageId,
242                           new Object JavaDoc[]{param0, param1, param2});
243     }
244
245
246     public static FacesMessage getMessage(FacesContext context, String JavaDoc messageId,
247                                           Object JavaDoc param0, Object JavaDoc param1,
248                                           Object JavaDoc param2, Object JavaDoc param3) {
249         return getMessage(context, messageId,
250                           new Object JavaDoc[]{param0, param1, param2, param3});
251     }
252
253
254     protected static Application getApplication() {
255         return (FacesContext.getCurrentInstance().getApplication());
256     }
257
258
259     protected static ClassLoader JavaDoc getCurrentLoader(Object JavaDoc fallbackClass) {
260         ClassLoader JavaDoc loader =
261             Thread.currentThread().getContextClassLoader();
262         if (loader == null) {
263             loader = fallbackClass.getClass().getClassLoader();
264         }
265         return loader;
266     }
267
268
269 } // end of class MessageFactory
270
Popular Tags