KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > LocalStringManagerImpl


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 package com.sun.enterprise.util;
24
25 import java.util.ResourceBundle JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.text.MessageFormat JavaDoc;
28
29 //START OF IASRI 4660742
30
import java.util.logging.*;
31 import com.sun.logging.*;
32 //END OF IASRI 4660742
33

34
35 /**
36  * Implementation of a local string manager.
37  * Provides access to i18n messages for classes that need them.
38  */

39
40 public class LocalStringManagerImpl implements LocalStringManager {
41
42     // START OF IASRI 4660742
43
//WARNING: _logger must be initialized upon demand in this case. The
44
//reason is that this static init happens before the ServerContext
45
//is initialized
46
private static Logger _logger = null;
47     // END OF IASRI 4660742
48

49     private Class JavaDoc defaultClass;
50
51     //No need to worry about synchronization here as getLogger call is
52
//synchronized, worse case logger will get initialized to the same
53
//value 2x.
54
private static Logger getLogger() {
55         if (_logger == null) {
56             _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER);
57         }
58         return _logger;
59     }
60
61     /**
62      * Create a string manager that looks for LocalStrings.properties in
63      * the package of the defaultClass.
64      * @param defaultClass Class whose package has default localized strings
65      */

66     public LocalStringManagerImpl(Class JavaDoc defaultClass) {
67     this.defaultClass = defaultClass;
68     }
69
70     /**
71      * Get a localized string.
72      * Strings are stored in a single property file per package named
73      * LocalStrings[_locale].properties. Starting from the class of the
74      * caller, we walk up the class hierarchy until we find a package
75      * resource bundle that provides a value for the requested key.
76      *
77      * <p>This simplifies access to resources, at the cost of checking for
78      * the resource bundle of several classes upon each call. However, due
79      * to the caching performed by <tt>ResourceBundle</tt> this seems
80      * reasonable.
81      *
82      * <p>Due to that, sub-classes <strong>must</strong> make sure they don't
83      * have conflicting resource naming.
84      * @param callerClass The object making the call, to allow per-package
85      * resource bundles
86      * @param key The name of the resource to fetch
87      * @param defaultValue The default return value if not found
88      * @return The localized value for the resource
89      */

90     public String JavaDoc getLocalString(
91     Class JavaDoc callerClass,
92     String JavaDoc key,
93     String JavaDoc defaultValue
94     ) {
95     Class JavaDoc stopClass = defaultClass.getSuperclass();
96     Class JavaDoc startClass = ((callerClass != null) ? callerClass :
97                 defaultClass);
98     ResourceBundle JavaDoc resources = null;
99     boolean globalDone = false;
100     for (Class JavaDoc c = startClass;
101          c != stopClass && c != null;
102          c = c.getSuperclass()) {
103         globalDone = (c == defaultClass);
104         try {
105         // Construct the bundle name as LocalStrings in the
106
// caller class's package.
107
StringBuffer JavaDoc resFileName = new StringBuffer JavaDoc(
108             c.getName().substring(0, c.getName().lastIndexOf(".")));
109         resFileName.append(".LocalStrings");
110
111         resources = ResourceBundle.getBundle(resFileName.toString());
112         if ( resources != null ) {
113             String JavaDoc value = resources.getString(key);
114             if ( value != null )
115             return value;
116         }
117         } catch (Exception JavaDoc ex) {
118         }
119     }
120
121     // Look for a global resource (defined by defaultClass)
122
if ( ! globalDone ) {
123         return getLocalString(null, key, defaultValue);
124     } else {
125         /** IASRI 4660742
126       System.err.println("No local string for " + key);
127       **/

128         //START OF IASRI 4660742
129
if (getLogger().isLoggable(Level.FINE))
130             getLogger().log(Level.FINE,"No local string for " + key);
131         //END OF IASRI 4660742
132
return defaultValue;
133     }
134     }
135
136     /**
137      * Get a localized string from the package of the default class.
138      * @param key The name of the resource to fetch
139      * @param defaultValue The default return value if not found
140      * @return The localized string
141      */

142     public String JavaDoc getLocalString(String JavaDoc key, String JavaDoc defaultValue) {
143     return getLocalString(null, key, defaultValue);
144     }
145
146     /**
147      * Get a local string for the caller and format the arguments accordingly.
148      * @param callerClass The caller (to walk through its class hierarchy)
149      * @param key The key to the local format string
150      * @param fmt The default format if not found in the resources
151      * @param arguments The set of arguments to provide to the formatter
152      * @return A formatted localized string
153      */

154
155     public String JavaDoc getLocalString(
156     Class JavaDoc callerClass,
157     String JavaDoc key,
158     String JavaDoc defaultFormat,
159     Object JavaDoc arguments[]
160     ) {
161     MessageFormat JavaDoc f = new MessageFormat JavaDoc(
162         getLocalString(callerClass, key, defaultFormat));
163     for (int i = 0; i < arguments.length; i++) {
164         if ( arguments[i] == null ) {
165         arguments[i] = "null";
166         } else if ( !(arguments[i] instanceof String JavaDoc) &&
167          !(arguments[i] instanceof Number JavaDoc) &&
168          !(arguments[i] instanceof java.util.Date JavaDoc)) {
169         arguments[i] = arguments[i].toString();
170         }
171     }
172     return f.format(arguments);
173     }
174
175     /**
176      * Get a local string from the package of the default class and
177      * format the arguments accordingly.
178      * @param key The key to the local format string
179      * @param fmt The default format if not found in the resources
180      * @param arguments The set of arguments to provide to the formatter
181      * @return A formatted localized string
182      */

183     public String JavaDoc getLocalString(
184     String JavaDoc key,
185     String JavaDoc defaultFormat,
186     Object JavaDoc arguments[]
187     ) {
188     return getLocalString(null, key, defaultFormat, arguments);
189     }
190 }
191
192
Popular Tags