Source Code Cross Referenced for MessageFactory.java in  » Portal » Open-Portal » guessnumber » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » Open Portal » guessnumber 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.