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.management.system;
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 implements com.sun.jbi.StringTranslator {
044: /**
045: * Logger name
046: */
047: private static final String LOGGER_NAME = "com.sun.jbi.framework.i18n";
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: * Message text used when a stack trace cannot be converted to a string.
100: */
101: private static final String STACK_TRACE_PRINT_FAILED = "<unable to print stack trace>";
102:
103: /**
104: * Logger for this class. This is used only to log messages concerning the
105: * setup of an instance, so one static logger is all that is necessary.
106: */
107: private static Logger sLog;
108:
109: /**
110: * ResourceBundle for a single package name.
111: */
112: private ResourceBundle mResourceBundle;
113:
114: /**
115: * The default locale at the time this StringTranslator was created.
116: */
117: private Locale mDefaultLocale;
118:
119: /**
120: * Static initializer. This sets up the static logger.
121: */
122: {
123: sLog = Logger.getLogger(LOGGER_NAME);
124: }
125:
126: /**
127: * Constructor. This loads the Resource Bundle for the current locale,
128: * and if the current locale is not Locale.US, it loads the Resource
129: * Bundle for Locale.US and stores it as the backup for string lookup.
130: * @param packageName - the name of the package that contains the resource
131: * bundle.
132: * @param classLoader - the class loader to be used for loading the resource
133: * bundle. If this parameter is null, the current class loader is used.
134: */
135: StringTranslator(String packageName, ClassLoader classLoader) {
136: sLog.finest(MessageFormat.format(LOG_NEW_INSTANCE,
137: new Object[] { packageName, classLoader }));
138: String bundleName = packageName + "." + RESOURCE_BUNDLE_NAME;
139: mDefaultLocale = Locale.getDefault();
140: sLog.finest(MessageFormat.format(LOG_CURRENT_LOCALE,
141: new Object[] { mDefaultLocale }));
142:
143: try {
144: if (null == classLoader) {
145: mResourceBundle = ResourceBundle.getBundle(bundleName);
146: } else {
147: mResourceBundle = ResourceBundle.getBundle(bundleName,
148: mDefaultLocale, classLoader);
149: }
150: } catch (java.util.MissingResourceException mrEx) {
151: sLog.warning(MessageFormat.format(
152: LOG_UNABLE_TO_LOAD_BUNDLE, new Object[] {
153: bundleName, mDefaultLocale, mrEx }));
154: }
155: }
156:
157: /**
158: * Get a localized string using the specified resource key.
159: * @param key - the key to the localized string in the resource bundle.
160: * @return the localized string.
161: */
162: public String getString(String key) {
163: Object[] inserts = new Object[0];
164: return getString(key, inserts);
165: }
166:
167: /**
168: * Get a localized string using the specified resource key. Handle one
169: * message insert.
170: * @param key - the key to the localized string in the resource bundle.
171: * @param insert1 - the message insert.
172: * @return the localized string formatted with the message insert.
173: */
174: public String getString(String key, Object insert1) {
175: Object[] inserts = { insert1 };
176: return getString(key, inserts);
177: }
178:
179: /**
180: * Get a localized string using the specified resource key. Handle two
181: * message inserts.
182: * @param key - the key to the localized string in the resource bundle.
183: * @param insert1 - the first message insert.
184: * @param insert2 - the second message insert.
185: * @return the localized string formatted with the message inserts.
186: */
187: public String getString(String key, Object insert1, Object insert2) {
188: Object[] inserts = { insert1, insert2 };
189: return getString(key, inserts);
190: }
191:
192: /**
193: * Get a localized string using the specified resource key. Handle three
194: * message inserts.
195: * @param key - the key to the localized string in the resource bundle.
196: * @param insert1 - the first message insert.
197: * @param insert2 - the second message insert.
198: * @param insert3 - the third message insert.
199: * @return the localized string formatted with the message inserts.
200: */
201: public String getString(String key, Object insert1, Object insert2,
202: Object insert3) {
203: Object[] inserts = { insert1, insert2, insert3 };
204: return getString(key, inserts);
205: }
206:
207: /**
208: * Get a localized string using the specified resource key. Handle four
209: * message inserts.
210: * @param key - the key to the localized string in the resource bundle.
211: * @param insert1 - the first message insert.
212: * @param insert2 - the second message insert.
213: * @param insert3 - the third message insert.
214: * @param insert4 - the fourth message insert.
215: * @return the localized string formatted with the message inserts.
216: */
217: public String getString(String key, Object insert1, Object insert2,
218: Object insert3, Object insert4) {
219: Object[] inserts = { insert1, insert2, insert3, insert4 };
220: return getString(key, inserts);
221: }
222:
223: /**
224: * Get a localized string using the specified resource key. Handle five
225: * message inserts.
226: * @param key - the key to the localized string in the resource bundle.
227: * @param insert1 - the first message insert.
228: * @param insert2 - the second message insert.
229: * @param insert3 - the third message insert.
230: * @param insert4 - the fourth message insert.
231: * @param insert5 - the fifth message insert.
232: * @return the localized string formatted with the message inserts.
233: */
234: public String getString(String key, Object insert1, Object insert2,
235: Object insert3, Object insert4, Object insert5) {
236: Object[] inserts = { insert1, insert2, insert3, insert4,
237: insert5 };
238: return getString(key, inserts);
239: }
240:
241: /**
242: * Get a localized string using the specified resource key. Handle any
243: * number of message inserts. This method is called by all the other
244: * getString() methods in the class. The procedure for string lookup is
245: * to first look in the primary resource bundle, then in the fallback
246: * resource bundle. If the string is found in the primary, return the
247: * translated string quietly. If the string is not found in the primary
248: * but in the fallback, log a warning and return the translated string.
249: * If the string is not found in either bundle, log an error and return
250: * a message formatted with the key and insert values provided by the
251: * caller. If there is no resource bundle available, just return a message
252: * formatted with the key and insert values provided by the caller.
253: * @param key - the key to the localized string in the resource bundle.
254: * @param inserts - the array of message inserts.
255: * @return the localized string formatted with the message inserts.
256: */
257: public String getString(String key, Object[] inserts) {
258: String translated = null;
259:
260: if (null != mResourceBundle) {
261: try {
262: translated = mResourceBundle.getString(key);
263: translated = MessageFormat.format(translated, inserts);
264: } catch (java.util.MissingResourceException mrEx) {
265: String fi = formatInserts(inserts);
266: translated = MessageFormat.format(MSG_NO_TRANSLATION,
267: new Object[] { key, fi });
268: sLog.warning(MessageFormat.format(
269: LOG_NO_TRANSLATION_FOR_KEY_IN_BUNDLE,
270: new Object[] { key, mDefaultLocale, fi }));
271: }
272: } else {
273: translated = MessageFormat.format(MSG_NO_TRANSLATION,
274: new Object[] { key, formatInserts(inserts) });
275: }
276:
277: return translated;
278: }
279:
280: /**
281: * Return an exception's stack trace as a String. If the trace cannot
282: * be produced, returns an error message in the string.
283: * @param exception The exception to process.
284: * @return The stack trace for the exception.
285: */
286: public String stackTraceToString(Throwable exception) {
287: try {
288: java.io.StringWriter sw = new java.io.StringWriter();
289: java.io.PrintWriter pw = new java.io.PrintWriter(sw);
290: exception.printStackTrace(pw);
291: return sw.toString();
292: } catch (Exception ex) {
293: return STACK_TRACE_PRINT_FAILED;
294: }
295: }
296:
297: /**
298: * Format an array of message inserts into a string. The ouptut string is
299: * in the format "insert1,insert2,....,insertn".
300: * @param inserts - the array of message inserts.
301: * @return the string formatted with the message inserts.
302: */
303: private String formatInserts(Object[] inserts) {
304: StringBuffer formatted = new StringBuffer("");
305: for (int i = 0; i < inserts.length; i++) {
306: if (i > 0) {
307: formatted.append(",");
308: }
309: formatted.append(inserts[i].toString());
310: }
311: return formatted.toString();
312: }
313: }
|