001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)StringTranslator.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.security.auth.module;
030:
031: import java.text.MessageFormat;
032: import java.util.Locale;
033: import java.util.logging.Logger;
034: import java.util.ResourceBundle;
035:
036: /**
037: * This is the implementation of the String Translator, which provides services
038: * for internationalization of messages to all services running inside the
039: * JBI environment.
040: *
041: * @author Sun Microsystems, Inc.
042: */
043: public class StringTranslator {
044: /**
045: * Logger name
046: */
047: private static final String LOGGER_NAME = "com.sun.jbi.internal.security";
048:
049: /**
050: * Unqualified name for resource bundles.
051: */
052: public static final String RESOURCE_BUNDLE_NAME = "LocalStrings";
053:
054: /**
055: * Log message for creation of new instance.
056: */
057: private static final String LOG_NEW_INSTANCE = "New StringTranslator for package {0}, classLoader is {1}";
058:
059: /**
060: * Log message for locale.
061: */
062: private static final String LOG_CURRENT_LOCALE = "Current locale is {0}";
063:
064: /**
065: * Log message for failure loading resource bundle.
066: */
067: private static final String LOG_UNABLE_TO_LOAD_BUNDLE = "Unable to load resource bundle {0} for locale {1}: {2}";
068:
069: /**
070: * Log message for using alternate resource bundle.
071: */
072: private static final String LOG_USING_BUNDLE = "Using resource bundle for locale {0} instead.";
073:
074: /**
075: * Log message for using fallback resource bundle to look up a message.
076: */
077: private static final String LOG_TRANSLATION_USING_FALLBACK = "No translation for key={0} found in resource bundle for locale {1}, "
078: + "using locale {2} instead.";
079: /**
080: * Log message for no translation available for a message key in any
081: * resource bundle.
082: */
083: private static final String LOG_NO_TRANSLATION_FOR_KEY = "No translation for key={0} found in any resource bundle. "
084: + "Insert data is [{1}].";
085:
086: /**
087: * Log message for no translation available for a message key in a
088: * particular resource bundle.
089: */
090: private static final String LOG_NO_TRANSLATION_FOR_KEY_IN_BUNDLE = "No translation for key={0} found in resource bundle for locale {1}. "
091: + "Insert data is [{2}].";
092:
093: /**
094: * Message text used when no translation is available for a message key.
095: */
096: private static final String MSG_NO_TRANSLATION = "No translation available for message with key={0} and inserts=[{1}].";
097:
098: /**
099: * Logger for this instance
100: */
101: private Logger mLog;
102:
103: /**
104: * ResourceBundle for a single package name.
105: */
106: private ResourceBundle mResourceBundle;
107:
108: /**
109: * The default locale at the time this StringTranslator was created.
110: */
111: private Locale mDefaultLocale;
112:
113: /**
114: * Constructor. This loads the Resource Bundle for the current locale,
115: * and if the current locale is not Locale.US, it loads the Resource
116: * Bundle for Locale.US and stores it as the backup for string lookup.
117: * @param packageName - the name of the package that contains the resource
118: * bundle.
119: * @param classLoader - the class loader to be used for loading the resource
120: * bundle. If this parameter is null, the current class loader is used.
121: */
122: StringTranslator(String packageName, ClassLoader classLoader) {
123: mLog = Logger.getLogger(LOGGER_NAME);
124: mLog.fine(MessageFormat.format(LOG_NEW_INSTANCE, new Object[] {
125: packageName, classLoader }));
126: String bundleName = packageName + "." + RESOURCE_BUNDLE_NAME;
127: mDefaultLocale = Locale.getDefault();
128: mLog.finer(MessageFormat.format(LOG_CURRENT_LOCALE,
129: new Object[] { mDefaultLocale }));
130:
131: try {
132: if (null == classLoader) {
133: mResourceBundle = ResourceBundle.getBundle(bundleName);
134: } else {
135: mResourceBundle = ResourceBundle.getBundle(bundleName,
136: mDefaultLocale, classLoader);
137:
138: }
139: } catch (java.util.MissingResourceException mrEx) {
140: mLog.warning(MessageFormat.format(
141: LOG_UNABLE_TO_LOAD_BUNDLE, new Object[] {
142: bundleName, mDefaultLocale, mrEx }));
143: }
144: }
145:
146: /**
147: * Get a localized string using the specified resource key.
148: * @param key - the key to the localized string in the resource bundle.
149: * @return the localized string.
150: */
151: public String getString(String key) {
152: Object[] inserts = new Object[0];
153: return getString(key, inserts);
154: }
155:
156: /**
157: * Get a localized string using the specified resource key. Handle one
158: * message insert.
159: * @param key - the key to the localized string in the resource bundle.
160: * @param insert1 - the message insert.
161: * @return the localized string formatted with the message insert.
162: */
163: public String getString(String key, Object insert1) {
164: Object[] inserts = { insert1 };
165: return getString(key, inserts);
166: }
167:
168: /**
169: * Get a localized string using the specified resource key. Handle two
170: * message inserts.
171: * @param key - the key to the localized string in the resource bundle.
172: * @param insert1 - the first message insert.
173: * @param insert2 - the second message insert.
174: * @return the localized string formatted with the message inserts.
175: */
176: public String getString(String key, Object insert1, Object insert2) {
177: Object[] inserts = { insert1, insert2 };
178: return getString(key, inserts);
179: }
180:
181: /**
182: * Get a localized string using the specified resource key. Handle three
183: * message inserts.
184: * @param key - the key to the localized string in the resource bundle.
185: * @param insert1 - the first message insert.
186: * @param insert2 - the second message insert.
187: * @param insert3 - the third message insert.
188: * @return the localized string formatted with the message inserts.
189: */
190: public String getString(String key, Object insert1, Object insert2,
191: Object insert3) {
192: Object[] inserts = { insert1, insert2, insert3 };
193: return getString(key, inserts);
194: }
195:
196: /**
197: * Get a localized string using the specified resource key. Handle four
198: * message inserts.
199: * @param key - the key to the localized string in the resource bundle.
200: * @param insert1 - the first message insert.
201: * @param insert2 - the second message insert.
202: * @param insert3 - the third message insert.
203: * @param insert4 - the fourth message insert.
204: * @return the localized string formatted with the message inserts.
205: */
206: public String getString(String key, Object insert1, Object insert2,
207: Object insert3, Object insert4) {
208: Object[] inserts = { insert1, insert2, insert3, insert4 };
209: return getString(key, inserts);
210: }
211:
212: /**
213: * Get a localized string using the specified resource key. Handle any
214: * number of message inserts. This method is called by all the other
215: * getString() methods in the class. The procedure for string lookup is
216: * to first look in the primary resource bundle, then in the fallback
217: * resource bundle. If the string is found in the primary, return the
218: * translated string quietly. If the string is not found in the primary
219: * but in the fallback, log a warning and return the translated string.
220: * If the string is not found in either bundle, log an error and return
221: * a message formatted with the key and insert values provided by the
222: * caller. If there is no resource bundle available, just return a message
223: * formatted with the key and insert values provided by the caller.
224: * @param key - the key to the localized string in the resource bundle.
225: * @param inserts - the array of message inserts.
226: * @return the localized string formatted with the message inserts.
227: */
228: public String getString(String key, Object[] inserts) {
229: String translated = null;
230:
231: if (null != mResourceBundle) {
232: try {
233: translated = mResourceBundle.getString(key);
234: translated = MessageFormat.format(translated, inserts);
235: } catch (java.util.MissingResourceException mrEx) {
236: String fi = formatInserts(inserts);
237: translated = MessageFormat.format(MSG_NO_TRANSLATION,
238: new Object[] { key, fi });
239: mLog.warning(MessageFormat.format(
240: LOG_NO_TRANSLATION_FOR_KEY_IN_BUNDLE,
241: new Object[] { key, mDefaultLocale, fi }));
242: }
243: } else {
244: translated = MessageFormat.format(MSG_NO_TRANSLATION,
245: new Object[] { key, formatInserts(inserts) });
246: }
247:
248: return translated;
249: }
250:
251: /**
252: * Format an array of message inserts into a string. The ouptut string is
253: * in the format "insert1,insert2,....,insertn".
254: * @param inserts - the array of message inserts.
255: * @return the string formatted with the message inserts.
256: */
257: private String formatInserts(Object[] inserts) {
258: StringBuffer formatted = new StringBuffer("");
259: for (int i = 0; i < inserts.length; i++) {
260: if (i > 0) {
261: formatted.append(",");
262: }
263: formatted.append(inserts[i].toString());
264: }
265: return formatted.toString();
266: }
267: }
|