001: /*
002: * $Id: MessageFactory.java,v 1.2 2005/07/15 15:32:47 dg154973 Exp $
003: */
004:
005: /*
006: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
007: *
008: * Redistribution and use in source and binary forms, with or
009: * without modification, are permitted provided that the following
010: * conditions are met:
011: *
012: * - Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * - Redistribution in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials
018: * provided with the distribution.
019: *
020: * Neither the name of Sun Microsystems, Inc. or the names of
021: * contributors may be used to endorse or promote products derived
022: * from this software without specific prior written permission.
023: *
024: * This software is provided "AS IS," without a warranty of any
025: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
026: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
027: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
028: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
029: * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
030: * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
031: * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
032: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
033: * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
034: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
035: * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
036: * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
037: *
038: * You acknowledge that this software is not designed, licensed or
039: * intended for use in the design, construction, operation or
040: * maintenance of any nuclear facility.
041: */
042:
043: package guessnumber;
044:
045: import javax.faces.application.Application;
046: import javax.faces.application.FacesMessage;
047: import javax.faces.context.FacesContext;
048: import java.text.MessageFormat;
049: import java.util.Locale;
050: import java.util.MissingResourceException;
051: import java.util.ResourceBundle;
052:
053: /**
054: * <p>supported filters: <code>package</code> and
055: * <code>protection</code>.</p>
056: */
057:
058: public class MessageFactory extends Object {
059:
060: //
061: // Protected Constants
062: //
063:
064: //
065: // Class Variables
066: //
067:
068: //
069: // Instance Variables
070: //
071:
072: // Attribute Instance Variables
073:
074: // Relationship Instance Variables
075:
076: //
077: // Constructors and Initializers
078: //
079:
080: private MessageFactory() {
081: }
082:
083: //
084: // Class methods
085: //
086:
087: //
088: // General Methods
089: //
090:
091: public static String substituteParams(Locale locale,
092: String msgtext, Object params[]) {
093: String localizedStr = null;
094:
095: if (params == null || msgtext == null) {
096: return msgtext;
097: }
098: StringBuffer b = new StringBuffer(100);
099: MessageFormat mf = new MessageFormat(msgtext);
100: if (locale != null) {
101: mf.setLocale(locale);
102: b.append(mf.format(params));
103: localizedStr = b.toString();
104: }
105: return localizedStr;
106: }
107:
108: /**
109: * This version of getMessage() is used in the RI for localizing RI
110: * specific messages.
111: */
112:
113: public static FacesMessage getMessage(String messageId,
114: Object params[]) {
115: Locale locale = null;
116: FacesContext context = FacesContext.getCurrentInstance();
117: // context.getViewRoot() may not have been initialized at this point.
118: if (context != null && context.getViewRoot() != null) {
119: locale = context.getViewRoot().getLocale();
120: if (locale == null) {
121: locale = Locale.getDefault();
122: }
123: } else {
124: locale = Locale.getDefault();
125: }
126:
127: return getMessage(locale, messageId, params);
128: }
129:
130: public static FacesMessage getMessage(Locale locale,
131: String messageId, Object params[]) {
132: FacesMessage result = null;
133: String summary = null, detail = null, bundleName = null;
134: ResourceBundle bundle = null;
135:
136: // see if we have a user-provided bundle
137: if (null != (bundleName = getApplication().getMessageBundle())) {
138: if (null != (bundle = ResourceBundle.getBundle(bundleName,
139: locale, getCurrentLoader(bundleName)))) {
140: // see if we have a hit
141: try {
142: summary = bundle.getString(messageId);
143: } catch (MissingResourceException e) {
144: }
145: }
146: }
147:
148: // we couldn't find a summary in the user-provided bundle
149: if (null == summary) {
150: // see if we have a summary in the app provided bundle
151: bundle = ResourceBundle.getBundle(
152: FacesMessage.FACES_MESSAGES, locale,
153: getCurrentLoader(bundleName));
154: if (null == bundle) {
155: throw new NullPointerException(" bundle " + bundle);
156: }
157: // see if we have a hit
158: try {
159: summary = bundle.getString(messageId);
160: } catch (MissingResourceException e) {
161: }
162: }
163:
164: // we couldn't find a summary anywhere! Return null
165: if (null == summary) {
166: return null;
167: }
168:
169: // At this point, we have a summary and a bundle.
170: if (null == summary || null == bundle) {
171: throw new NullPointerException(" summary " + summary
172: + " bundle " + bundle);
173: }
174: summary = substituteParams(locale, summary, params);
175:
176: try {
177: detail = substituteParams(locale, bundle
178: .getString(messageId + "_detail"), params);
179: } catch (MissingResourceException e) {
180: }
181:
182: return (new FacesMessage(summary, detail));
183: }
184:
185: //
186: // Methods from MessageFactory
187: //
188: public static FacesMessage getMessage(FacesContext context,
189: String messageId) {
190: return getMessage(context, messageId, null);
191: }
192:
193: public static FacesMessage getMessage(FacesContext context,
194: String messageId, Object params[]) {
195: if (context == null || messageId == null) {
196: throw new NullPointerException(" context " + context
197: + " messageId " + messageId);
198: }
199: Locale locale = null;
200: // viewRoot may not have been initialized at this point.
201: if (context != null && context.getViewRoot() != null) {
202: locale = context.getViewRoot().getLocale();
203: } else {
204: locale = Locale.getDefault();
205: }
206: if (null == locale) {
207: throw new NullPointerException(" locale " + locale);
208: }
209: FacesMessage message = getMessage(locale, messageId, params);
210: if (message != null) {
211: return message;
212: }
213: locale = Locale.getDefault();
214: return (getMessage(locale, messageId, params));
215: }
216:
217: public static FacesMessage getMessage(FacesContext context,
218: String messageId, Object param0) {
219: return getMessage(context, messageId, new Object[] { param0 });
220: }
221:
222: public static FacesMessage getMessage(FacesContext context,
223: String messageId, Object param0, Object param1) {
224: return getMessage(context, messageId, new Object[] { param0,
225: param1 });
226: }
227:
228: public static FacesMessage getMessage(FacesContext context,
229: String messageId, Object param0, Object param1,
230: Object param2) {
231: return getMessage(context, messageId, new Object[] { param0,
232: param1, param2 });
233: }
234:
235: public static FacesMessage getMessage(FacesContext context,
236: String messageId, Object param0, Object param1,
237: Object param2, Object param3) {
238: return getMessage(context, messageId, new Object[] { param0,
239: param1, param2, param3 });
240: }
241:
242: protected static Application getApplication() {
243: return (FacesContext.getCurrentInstance().getApplication());
244: }
245:
246: protected static ClassLoader getCurrentLoader(Object fallbackClass) {
247: ClassLoader loader = Thread.currentThread()
248: .getContextClassLoader();
249: if (loader == null) {
250: loader = fallbackClass.getClass().getClassLoader();
251: }
252: return loader;
253: }
254:
255: } // end of class MessageFactory
|