19 package com.mysql.clusterj.core.util;
22 import java.text.MessageFormat;
23 import java.security.AccessController;
24 import java.security.PrivilegedAction;
26 import com.mysql.clusterj.ClusterJFatalInternalException;
56 private static Hashtable<String, ResourceBundle> bundles =
new Hashtable<String, ResourceBundle>();
60 private static Hashtable<String, I18NHelper> helpers =
new Hashtable<String, I18NHelper>();
64 private static Locale locale = Locale.getDefault();
68 private final String bundleName;
72 private ResourceBundle bundle = null;
76 private Throwable failure = null;
79 private static final String bundleSuffix =
".Bundle";
83 this.bundleName = null;
92 this.bundleName = bundleName;
94 bundle = loadBundle (bundleName, bundleName, loader);
118 ClassLoader classLoader = AccessController.doPrivileged (
119 new PrivilegedAction<ClassLoader> () {
120 public ClassLoader
run () {
121 return cls.getClassLoader();
125 String bundle = getPackageName (cls.getName()) + bundleSuffix;
138 ClassLoader loader) {
140 if (helper != null) {
144 helpers.put (bundleName, helper);
147 return helpers.get (bundleName);
155 assertBundle (messageKey);
156 return getMessage (bundle, messageKey);
165 assertBundle (messageKey);
166 return getMessage (bundle, messageKey, arg1);
176 assertBundle (messageKey);
177 return getMessage (bundle, messageKey, arg1, arg2);
186 assertBundle (messageKey);
187 return getMessage (bundle, messageKey, args);
196 assertBundle (messageKey);
197 return getMessage(bundle, messageKey, arg);
206 assertBundle (messageKey);
207 return getMessage(bundle, messageKey, arg);
226 final private static ResourceBundle loadBundle(
227 String original,
String bundleName, ClassLoader loader) {
228 ResourceBundle messages = bundles.get(bundleName);
230 if (messages == null)
233 if (loader != null) {
234 messages = ResourceBundle.getBundle(bundleName, locale, loader);
237 messages = ResourceBundle.getBundle(bundleName, locale,
238 getSystemClassLoaderPrivileged());
240 bundles.put(bundleName, messages);
241 }
catch (java.util.MissingResourceException ex) {
243 String superBundleName = removeDirectoryName(bundleName);
244 if (superBundleName == null) {
245 throw new ClusterJFatalInternalException(
246 "Missing resource bundle " + original);
248 messages = loadBundle(original, superBundleName, loader);
258 private void assertBundle () {
260 throw new ClusterJFatalInternalException (
261 "No resources could be found for bundle:\"" +
262 bundleName +
"\" ", failure);
270 private void assertBundle (
String key) {
272 throw new ClusterJFatalInternalException (
273 "No resources could be found for bundle: " + bundleName
274 +
" to annotate error message key:\""
275 + key +
"\"", failure);
284 final private static String getMessage(ResourceBundle messages,
String messageKey)
286 return messages.getString(messageKey);
296 final private static String getMessage(ResourceBundle messages,
297 String messageKey, Object[] msgArgs)
299 for (
int i=0;
i<msgArgs.length;
i++) {
300 if (msgArgs[
i] == null) msgArgs[
i] =
"";
302 MessageFormat formatter =
new MessageFormat(messages.getString(messageKey));
303 return formatter.format(msgArgs);
313 final private static String getMessage(ResourceBundle messages,
314 String messageKey, Object arg)
316 Object []args = {arg};
317 return getMessage(messages, messageKey, args);
328 final private static String getMessage(ResourceBundle messages,
329 String messageKey, Object arg1, Object arg2)
331 Object []args = {arg1, arg2};
332 return getMessage(messages, messageKey, args);
342 final private static String getMessage(ResourceBundle messages,
343 String messageKey,
int arg)
345 Object []args = {
new Integer(arg)};
346 return getMessage(messages, messageKey, args);
356 final private static String getMessage(ResourceBundle messages,
357 String messageKey,
boolean arg)
359 Object []args = {
String.valueOf(arg)};
360 return getMessage(messages, messageKey, args);
369 final private static String getPackageName(
final String className)
371 final int index = className.lastIndexOf(
'.');
372 return ((index != -1) ? className.substring(0, index) :
"");
381 private static String removeDirectoryName(
String bundleName) {
383 int lastDot = bundleName.lastIndexOf(
".");
384 String packageName = bundleName.substring(0, lastDot);
385 String suffix = bundleName.substring(lastDot);
386 int index = packageName.lastIndexOf(
".");
390 String superPackageName = packageName.substring(0, index);
391 result = superPackageName + suffix;
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);
407 private static ClassLoader getSystemClassLoaderPrivileged() {
408 return AccessController.doPrivileged (
409 new PrivilegedAction<ClassLoader> () {
410 public ClassLoader
run () {
411 return ClassLoader.getSystemClassLoader();