MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
I18NHelper.java
1 /*
2  Copyright 2010 Sun Microsystems, Inc.
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 package com.mysql.clusterj.core.util;
20 
21 import java.util.*;
22 import java.text.MessageFormat;
23 import java.security.AccessController;
24 import java.security.PrivilegedAction;
25 
26 import com.mysql.clusterj.ClusterJFatalInternalException;
27 
48 public class I18NHelper {
49 
51  private static Logger logger = LoggerFactoryService.getFactory()
52  .getInstance(I18NHelper.class);
53 
56  private static Hashtable<String, ResourceBundle> bundles = new Hashtable<String, ResourceBundle>();
57 
60  private static Hashtable<String, I18NHelper> helpers = new Hashtable<String, I18NHelper>();
61 
64  private static Locale locale = Locale.getDefault();
65 
68  private final String bundleName;
69 
72  private ResourceBundle bundle = null;
73 
76  private Throwable failure = null;
77 
79  private static final String bundleSuffix = ".Bundle"; // NOI18N
80 
82  private I18NHelper() {
83  this.bundleName = null;
84  }
85 
91  private I18NHelper (String bundleName, ClassLoader loader) {
92  this.bundleName = bundleName;
93  try {
94  bundle = loadBundle (bundleName, bundleName, loader);
95  }
96  catch (Throwable e) {
97  failure = e;
98  }
99  }
100 
106  public static I18NHelper getInstance (String bundleName) {
107  return getInstance (bundleName, I18NHelper.class.getClassLoader());
108  }
109 
117  public static I18NHelper getInstance (final Class<?> cls) {
118  ClassLoader classLoader = AccessController.doPrivileged (
119  new PrivilegedAction<ClassLoader> () {
120  public ClassLoader run () {
121  return cls.getClassLoader();
122  }
123  }
124  );
125  String bundle = getPackageName (cls.getName()) + bundleSuffix;
126  return getInstance (bundle, classLoader);
127  }
128 
137  public static I18NHelper getInstance (String bundleName,
138  ClassLoader loader) {
139  I18NHelper helper = helpers.get (bundleName);
140  if (helper != null) {
141  return helper;
142  }
143  helper = new I18NHelper(bundleName, loader);
144  helpers.put (bundleName, helper);
145  // if two threads simultaneously create the same helper, return the first
146  // one to be put into the Hashtable. The other will be garbage collected.
147  return helpers.get (bundleName);
148  }
149 
154  public String message (String messageKey) {
155  assertBundle (messageKey);
156  return getMessage (bundle, messageKey);
157  }
158 
164  public String message (String messageKey, Object arg1) {
165  assertBundle (messageKey);
166  return getMessage (bundle, messageKey, arg1);
167  }
168 
175  public String message (String messageKey, Object arg1, Object arg2) {
176  assertBundle (messageKey);
177  return getMessage (bundle, messageKey, arg1, arg2);
178  }
179 
185  public String message (String messageKey, Object... args) {
186  assertBundle (messageKey);
187  return getMessage (bundle, messageKey, args);
188  }
189 
195  public String message (String messageKey, int arg) {
196  assertBundle (messageKey);
197  return getMessage(bundle, messageKey, arg);
198  }
199 
205  public String message (String messageKey, boolean arg) {
206  assertBundle (messageKey);
207  return getMessage(bundle, messageKey, arg);
208  }
209 
213  public ResourceBundle getResourceBundle () {
214  assertBundle ();
215  return bundle;
216  }
217 
218  //========= Internal helper methods ==========
219 
226  final private static ResourceBundle loadBundle(
227  String original, String bundleName, ClassLoader loader) {
228  ResourceBundle messages = bundles.get(bundleName);
229 
230  if (messages == null) //not found as loaded - add
231  {
232  try {
233  if (loader != null) {
234  messages = ResourceBundle.getBundle(bundleName, locale, loader);
235  } else {
236  // the library was loaded by the boostrap class loader
237  messages = ResourceBundle.getBundle(bundleName, locale,
238  getSystemClassLoaderPrivileged());
239  }
240  bundles.put(bundleName, messages);
241  } catch (java.util.MissingResourceException ex) {
242  // recursively try to find the Bundle in the next higher package
243  String superBundleName = removeDirectoryName(bundleName);
244  if (superBundleName == null) {
245  throw new ClusterJFatalInternalException(
246  "Missing resource bundle " + original);
247  }
248  messages = loadBundle(original, superBundleName, loader);
249  }
250  }
251  return messages;
252  }
253 
258  private void assertBundle () {
259  if (failure != null)
260  throw new ClusterJFatalInternalException (
261  "No resources could be found for bundle:\"" +
262  bundleName + "\" ", failure);
263  }
264 
270  private void assertBundle (String key) {
271  if (failure != null)
272  throw new ClusterJFatalInternalException (
273  "No resources could be found for bundle: " + bundleName
274  + " to annotate error message key:\""
275  + key + "\"", failure);
276  }
277 
284  final private static String getMessage(ResourceBundle messages, String messageKey)
285  {
286  return messages.getString(messageKey);
287  }
288 
296  final private static String getMessage(ResourceBundle messages,
297  String messageKey, Object[] msgArgs)
298  {
299  for (int i=0; i<msgArgs.length; i++) {
300  if (msgArgs[i] == null) msgArgs[i] = ""; // NOI18N
301  }
302  MessageFormat formatter = new MessageFormat(messages.getString(messageKey));
303  return formatter.format(msgArgs);
304  }
305 
313  final private static String getMessage(ResourceBundle messages,
314  String messageKey, Object arg)
315  {
316  Object []args = {arg};
317  return getMessage(messages, messageKey, args);
318  }
319 
328  final private static String getMessage(ResourceBundle messages,
329  String messageKey, Object arg1, Object arg2)
330  {
331  Object []args = {arg1, arg2};
332  return getMessage(messages, messageKey, args);
333  }
334 
342  final private static String getMessage(ResourceBundle messages,
343  String messageKey, int arg)
344  {
345  Object []args = {new Integer(arg)};
346  return getMessage(messages, messageKey, args);
347  }
348 
356  final private static String getMessage(ResourceBundle messages,
357  String messageKey, boolean arg)
358  {
359  Object []args = {String.valueOf(arg)};
360  return getMessage(messages, messageKey, args);
361  }
362 
369  final private static String getPackageName(final String className)
370  {
371  final int index = className.lastIndexOf('.');
372  return ((index != -1) ? className.substring(0, index) : ""); // NOI18N
373  }
374 
381  private static String removeDirectoryName(String bundleName) {
382  String result;
383  int lastDot = bundleName.lastIndexOf(".");
384  String packageName = bundleName.substring(0, lastDot);
385  String suffix = bundleName.substring(lastDot);
386  int index = packageName.lastIndexOf(".");
387  if (index == -1) {
388  return null;
389  }
390  String superPackageName = packageName.substring(0, index);
391  result = superPackageName + suffix;
392 
393  if (logger.isDebugEnabled()) {
394  logger.debug("bundleName is: " + bundleName +
395  "; superPackageName is: " + superPackageName +
396  "; suffix is: " + suffix +
397  "; packageName is: " + packageName +
398  "; returning: " + result);
399  }
400  return result;
401  }
402 
407  private static ClassLoader getSystemClassLoaderPrivileged() {
408  return AccessController.doPrivileged (
409  new PrivilegedAction<ClassLoader> () {
410  public ClassLoader run () {
411  return ClassLoader.getSystemClassLoader();
412  }
413  }
414  );
415  }
416 }