KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > util > Messages


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: Messages.java 1912 2005-06-16 22:29:56Z jlaskowski $
44  */

45 package org.openejb.util;
46
47 import java.text.MessageFormat JavaDoc;
48 import java.util.Enumeration JavaDoc;
49 import java.util.Hashtable JavaDoc;
50 import java.util.Locale JavaDoc;
51 import java.util.MissingResourceException JavaDoc;
52 import java.util.ResourceBundle JavaDoc;
53
54 /**
55  *
56  *
57  * @author <a HREF="arkin@exoffice.com">Assaf Arkin</a>
58  * @version $Revision: 1912 $ $Date: 2005-06-16 15:29:56 -0700 (Thu, 16 Jun 2005) $
59  */

60 public class Messages {
61     static private Hashtable JavaDoc _rbBundles = new Hashtable JavaDoc();
62     static private Hashtable JavaDoc _rbFormats = new Hashtable JavaDoc();
63     static private Locale JavaDoc _globalLocale;
64
65     private ResourceBundle JavaDoc _messages;
66     private Hashtable JavaDoc _formats;
67     private Locale JavaDoc _locale;
68     private String JavaDoc _resourceName;
69
70     public Messages(String JavaDoc resourceName) {
71         synchronized (Messages.class) {
72             _locale = _globalLocale;
73             _resourceName = resourceName + ".Messages";
74
75             ResourceBundle JavaDoc rb = (ResourceBundle JavaDoc) _rbBundles.get(_resourceName);
76             if (rb == null) {
77                 init();
78             } else {
79                 _messages = rb;
80                 _formats = (Hashtable JavaDoc) _rbFormats.get(_resourceName);
81             }
82         }
83
84     }
85
86     protected void init() {
87         try {
88             if (_locale == null)
89                 _messages = ResourceBundle.getBundle(_resourceName);
90             else
91                 _messages = ResourceBundle.getBundle(_resourceName, _locale);
92         } catch (Exception JavaDoc except) {
93             _messages = new EmptyResourceBundle();
94         }
95
96         _formats = new Hashtable JavaDoc();
97
98         _rbBundles.put(_resourceName, _messages);
99         _rbFormats.put(_resourceName, _formats);
100     }
101
102     public String JavaDoc format(String JavaDoc message, Object JavaDoc arg1) {
103         return format(message, new Object JavaDoc[] { arg1 });
104     }
105
106     public String JavaDoc format(String JavaDoc message, Object JavaDoc arg1, Object JavaDoc arg2) {
107         return format(message, new Object JavaDoc[] { arg1, arg2 });
108     }
109
110     public String JavaDoc format(String JavaDoc message, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3) {
111         return format(message, new Object JavaDoc[] { arg1, arg2, arg3 });
112     }
113
114     public String JavaDoc format(String JavaDoc message, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3, Object JavaDoc arg4) {
115         return format(message, new Object JavaDoc[] { arg1, arg2, arg3, arg4 });
116     }
117
118     public String JavaDoc format(String JavaDoc message, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3, Object JavaDoc arg4, Object JavaDoc arg5) {
119         return format(message, new Object JavaDoc[] { arg1, arg2, arg3, arg4, arg5 });
120     }
121
122     public String JavaDoc format(String JavaDoc message) {
123         return message(message);
124     }
125
126     public String JavaDoc format(String JavaDoc message, Object JavaDoc[] args) {
127         if (_locale != _globalLocale) {
128             synchronized (Messages.class) {
129                 init();
130             }
131         }
132
133         MessageFormat JavaDoc mf;
134         String JavaDoc msg;
135
136         try {
137             mf = (MessageFormat JavaDoc) _formats.get(message);
138             if (mf == null) {
139                 try {
140                     msg = _messages.getString(message);
141                 } catch (MissingResourceException JavaDoc except) {
142                     return message;
143                 }
144                 mf = new MessageFormat JavaDoc(msg);
145                 _formats.put(message, mf);
146             }
147             return mf.format(args);
148         } catch (Exception JavaDoc except) {
149             return "An internal error occured while processing message " + message;
150         }
151     }
152
153     public String JavaDoc message(String JavaDoc message) {
154         if (_locale != _globalLocale) {
155             synchronized (Messages.class) {
156                 init();
157             }
158         }
159
160         try {
161             return _messages.getString(message);
162         } catch (MissingResourceException JavaDoc except) {
163             return message;
164         }
165     }
166
167     static public void setLocale(Locale JavaDoc locale) {
168         synchronized (Messages.class) {
169             _globalLocale = locale;
170             _rbBundles = new Hashtable JavaDoc();
171             _rbFormats = new Hashtable JavaDoc();
172         }
173     }
174
175     static {
176         setLocale(Locale.getDefault());
177     }
178
179     private static final class EmptyResourceBundle extends ResourceBundle JavaDoc implements Enumeration JavaDoc {
180
181         public Enumeration JavaDoc getKeys() {
182             return this;
183         }
184
185         protected Object JavaDoc handleGetObject(String JavaDoc name) {
186             return "[Missing message " + name + "]";
187         }
188
189         public boolean hasMoreElements() {
190             return false;
191         }
192
193         public Object JavaDoc nextElement() {
194             return null;
195         }
196
197     }
198
199 }
200
Popular Tags