KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > i18n > StringManagerBase


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 in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
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 Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * @(#) StringManagerBase.java
26  *
27  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  *
31  * This software is the confidential and proprietary information
32  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
33  * You shall not disclose such Confidential Information and shall
34  * use it only in accordance with the terms of the license
35  * agreement you entered into with iPlanet/Sun Microsystems.
36  */

37 package com.sun.enterprise.util.i18n;
38
39 import java.util.ResourceBundle JavaDoc;
40 import java.util.Locale JavaDoc;
41 import java.util.Hashtable JavaDoc;
42 import java.text.MessageFormat JavaDoc;
43
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46 import com.sun.logging.LogDomains;
47
48 /**
49  * Implementation of a local string manager. Provides access to i18n messages
50  * for classes that need them.
51  *
52  * <p> One StringManagerBase per resource bundle name can be created and accessed by the
53  * getManager method call.
54  *
55  * <xmp>
56  * Example:
57  *
58  * [LocalStrings.properties]
59  * test=At {1,time} on {1,date}, there was {2} on planet {0,number,integer}
60  *
61  *
62  * StringManagerBase sm = StringManagerBase.getStringManager("LocalStrings.properties");
63  * .....
64  *
65  *
66  * try {
67  * ....
68  * } catch (Exception e) {
69  * String localizedMsg = sm.getString("test",
70  * new Integer(7), new java.util.Date(System.currentTimeMillis()),
71  * "a disturbance in the Force");
72  *
73  * throw new MyException(localizedMsg, e);
74  * }
75  *
76  * Localized message:
77  * At 2:27:41 PM on Jul 8, 2002, there was a disturbance in the Force
78  * on planet 7
79  *
80  * </xmp>
81  *
82  * @author Nazrul Islam
83  * @since JDK1.4
84  */

85 public class StringManagerBase {
86
87     /** logger used for this class */
88     private static Logger JavaDoc _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER);
89
90     /** resource bundle to be used by this manager */
91     private ResourceBundle JavaDoc _resourceBundle = null;
92     
93     /** default value used for undefined local string */
94     private static final String JavaDoc NO_DEFAULT = "No local string defined";
95
96     /** cache for all the local string managers (per pkg) */
97     private static Hashtable JavaDoc managers = new Hashtable JavaDoc();
98
99     /**
100      * Initializes the resource bundle.
101      *
102      * @param resourceBundleName name of the resource bundle
103      */

104     protected StringManagerBase(String JavaDoc resourceBundleName) {
105         try {
106             _resourceBundle = ResourceBundle.getBundle(resourceBundleName);
107         } catch (Exception JavaDoc e) {
108             _logger.log(Level.SEVERE, "iplanet_util.no_resource_bundle", e);
109         }
110     }
111
112     /**
113      * Returns a local string manager for the given resourceBundle name.
114      *
115      * @param resourceBundleName name of the resource bundle
116      *
117      * @return a local string manager for the given package name
118      */

119     public synchronized static StringManagerBase getStringManager(String JavaDoc resourceBundleName) {
120         StringManagerBase mgr = (StringManagerBase) managers.get(resourceBundleName);
121         if (mgr == null) {
122             mgr = new StringManagerBase(resourceBundleName);
123             try {
124                 managers.put(resourceBundleName, mgr);
125             } catch (Exception JavaDoc e) {
126                 _logger.log(Level.SEVERE,"iplanet_util.error_while_caching",e);
127             }
128         }
129         return mgr;
130     }
131
132     /**
133      * Returns a localized string.
134      *
135      * @param key the name of the resource to fetch
136      *
137      * @return the localized string
138      */

139     public String JavaDoc getString(String JavaDoc key) {
140         return getStringWithDefault(key, NO_DEFAULT);
141     }
142
143     /**
144      * Returns a localized string. If the key is not found, it will
145      * return the default given value.
146      *
147      * @param key the name of the resource to fetch
148      * @param defaultValue the default return value if not found
149      *
150      * @return the localized string
151      */

152     public String JavaDoc getStringWithDefault(String JavaDoc key, String JavaDoc defaultValue) {
153
154         String JavaDoc value = null;
155
156         try {
157             value = this._resourceBundle.getString(key);
158         } catch (Exception JavaDoc e) {
159             _logger.log(Level.FINE,"No local string for: " + key, e);
160         }
161
162         if (value != null) {
163             return value;
164         } else {
165             return defaultValue;
166         }
167     }
168
169     /**
170      * Returns a local string for the caller and format the arguments
171      * accordingly. If the key is not found, it will use the given
172      * default format.
173      *
174      * @param key the key to the local format string
175      * @param defaultFormat the default format if not found in the resources
176      * @param arguments the set of arguments to provide to the formatter
177      *
178      * @return a formatted localized string
179      */

180     public String JavaDoc getStringWithDefault(String JavaDoc key, String JavaDoc defaultFormat,
181             Object JavaDoc arguments[]) {
182
183         MessageFormat JavaDoc f =
184             new MessageFormat JavaDoc( getStringWithDefault(key, defaultFormat) );
185
186         for (int i=0; i<arguments.length; i++) {
187
188             if ( arguments[i] == null ) {
189
190                 arguments[i] = "null";
191
192             } else if ( !(arguments[i] instanceof String JavaDoc) &&
193                      !(arguments[i] instanceof Number JavaDoc) &&
194                      !(arguments[i] instanceof java.util.Date JavaDoc)) {
195
196                 arguments[i] = arguments[i].toString();
197             }
198         }
199
200         String JavaDoc fmtStr = null;
201         try {
202             fmtStr = f.format(arguments);
203         } catch (Exception JavaDoc e) {
204             _logger.log(Level.WARNING, "iplanet_util.error_while_formating", e);
205
206             // returns default format
207
fmtStr = defaultFormat;
208         }
209         return fmtStr;
210     }
211
212     /**
213      * Returns a local string for the caller and format the arguments
214      * accordingly.
215      *
216      * @param key the key to the local format string
217      * @param arg1 the one argument to be provided to the formatter
218      *
219      * @return a formatted localized string
220      */

221     public String JavaDoc getString(String JavaDoc key, Object JavaDoc arg1) {
222
223         return getStringWithDefault(key, NO_DEFAULT, new Object JavaDoc[] {arg1});
224     }
225
226     /**
227      * Returns a local string for the caller and format the arguments
228      * accordingly.
229      *
230      * @param key the key to the local format string
231      * @param arg1 first argument to be provided to the formatter
232      * @param arg2 second argument to be provided to the formatter
233      *
234      * @return a formatted localized string
235      */

236     public String JavaDoc getString(String JavaDoc key, Object JavaDoc arg1, Object JavaDoc arg2) {
237
238         return getStringWithDefault(key, NO_DEFAULT, new Object JavaDoc[] {arg1, arg2});
239     }
240
241     /**
242      * Returns a local string for the caller and format the arguments
243      * accordingly.
244      *
245      * @param key the key to the local format string
246      * @param arg1 first argument to be provided to the formatter
247      * @param arg2 second argument to be provided to the formatter
248      * @param arg3 third argument to be provided to the formatter
249      *
250      * @return a formatted localized string
251      */

252     public String JavaDoc getString(String JavaDoc key, Object JavaDoc arg1, Object JavaDoc arg2,
253             Object JavaDoc arg3) {
254
255         return getStringWithDefault(key, NO_DEFAULT,
256                                     new Object JavaDoc[] {arg1, arg2, arg3});
257     }
258
259     /**
260      * Returns a local string for the caller and format the arguments
261      * accordingly.
262      *
263      * @param key the key to the local format string
264      * @param arg1 first argument to be provided to the formatter
265      * @param arg2 second argument to be provided to the formatter
266      * @param arg3 third argument to be provided to the formatter
267      * @param arg4 fourth argument to be provided to the formatter
268      *
269      * @return a formatted localized string
270      */

271     public String JavaDoc getString(String JavaDoc key, Object JavaDoc arg1, Object JavaDoc arg2,
272             Object JavaDoc arg3, Object JavaDoc arg4) {
273
274         return getStringWithDefault(key, NO_DEFAULT,
275                                     new Object JavaDoc[] {arg1, arg2, arg3, arg4});
276     }
277
278     /**
279      * Returns a local string for the caller and format the arguments
280      * accordingly.
281      *
282      * @param key the key to the local format string
283      * @param args the array of arguments to be provided to the formatter
284      *
285      * @return a formatted localized string
286      */

287     public String JavaDoc getString(String JavaDoc key, Object JavaDoc[] args) {
288
289         return getStringWithDefault(key, NO_DEFAULT, args);
290     }
291
292
293     /**
294      * Unit test code.
295      *
296      * @param args arguments from command line
297      */

298     public static void main(String JavaDoc[] args) {
299
300         long b = System.currentTimeMillis();
301         try {
302             StringManagerBase sm =
303                 StringManagerBase.getStringManager("com.sun.enterprise.util.i18n.LocalStrings");
304
305             String JavaDoc ls = sm.getString(
306                 "test",
307                 new Integer JavaDoc(7), new java.util.Date JavaDoc(System.currentTimeMillis()),
308                 "a disturbance in the Force");
309
310             System.out.println(ls);
311
312             System.out.println( sm.getString("bad") );
313
314         } catch (Exception JavaDoc e) {
315             System.out.println("---- Error ---- ");
316             e.printStackTrace();
317         } finally {
318             long a = System.currentTimeMillis();
319             System.out.println("Time: " + (a-b) );
320         }
321     }
322 }
323
Popular Tags