Debian Codegraph @

ssl-proxies/src/main/java/org/globus/util/I18n.java

raw
1 /*
2 * Copyright 1999-2010 University of Chicago
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied.
12 *
13 * See the License for the specific language governing permissions and limitations under the License.
14 */
15 package org.globus.util;
16
17 import java.text.MessageFormat;
18 import java.util.HashMap;
19 import java.util.Locale;
20 import java.util.Map;
21 import java.util.MissingResourceException;
22 import java.util.ResourceBundle;
23
24 /**
25 * An utility class for internationalized message handling.
26 * Example usage::
27 * <pre>
28 * private static I18n i18n = I18n.getI18n("org.globus.resource");
29 * ...
30 * public void hello() {
31 * String filename = "file1";
32 * String msg = i18n.getMessage("noFile", new String[]{filename});
33 * ...
34 * }
35 * </pre>
36 */
37 public class I18n {
38
39 private static Map mapping = new HashMap();
40
41 private ResourceBundle messages = null;
42
43 protected I18n(ResourceBundle messages) {
44 this.messages = messages;
45 }
46
47 /**
48 * Retrieve a I18n instance by resource name.
49 *
50 * @param resource resource name. See {@link
51 * ResourceBundle#getBundle(String) ResourceBundle.getBundle()}
52 */
53 public static synchronized I18n getI18n(String resource) {
54 I18n instance = (I18n)mapping.get(resource);
55 if (instance == null) {
56 instance = new I18n(ResourceBundle.getBundle(resource,
57 Locale.getDefault(),
58 getClassLoader()));
59 mapping.put(resource, instance);
60 }
61 return instance;
62 }
63
64 /**
65 * Retrieve a I18n instance by resource name
66 *
67 * @param resource resource name. See {@link
68 * ResourceBundle#getBundle(String) ResourceBundle.getBundle()}
69 * @param loader the class loader to be used to load
70 * the resource. This parameter is only used
71 * initially to load the actual resource. Once the resource
72 * is loaded, this argument is ignored.
73 */
74 public static synchronized I18n getI18n(String resource,
75 ClassLoader loader) {
76 I18n instance = (I18n)mapping.get(resource);
77 if (instance == null) {
78 if (loader == null) {
79 loader = getClassLoader();
80 }
81 instance = new I18n(ResourceBundle.getBundle(resource,
82 Locale.getDefault(),
83 loader));
84 mapping.put(resource, instance);
85 }
86 return instance;
87 }
88
89 private static ClassLoader getClassLoader() {
90 // try to get caller's classloader otherwise use context classloader
91 ClassLoader loader = ClassLoaderUtils.getClassLoaderContextAt(4);
92 return (loader == null) ? Thread.currentThread().getContextClassLoader() : loader;
93 }
94
95 /**
96 * Gets a message from resource bundle.
97 */
98 public String getMessage(String key)
99 throws MissingResourceException {
100 return messages.getString(key);
101 }
102
103 /**
104 * Gets a formatted message from resource bundle
105 */
106 public String getMessage(String key, Object arg)
107 throws MissingResourceException {
108 return getMessage(key, new Object[] {arg});
109 }
110
111 /**
112 * Gets a formatted message from resource bundle
113 */
114 public String getMessage(String key, Object[] vars)
115 throws MissingResourceException {
116 return MessageFormat.format(messages.getString(key), vars);
117 }
118
119 }