1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package nl.toolforge.karma.core.bundle;
20
21 import nl.toolforge.karma.core.KarmaRuntimeException;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import java.util.Hashtable;
26 import java.util.Locale;
27 import java.util.Map;
28 import java.util.ResourceBundle;
29
30 /***
31 * <p>Helper class initializing <code>ResourceBundle</code> and caching instances to enable localized messages.
32 * Interface applications can extend this class to add and retrieve cached bundle.
33 *
34 * @author D.A. Smedes
35 * @version $Id: BundleCache.java,v 1.14 2004/08/29 18:00:31 hippe Exp $
36 */
37 public class BundleCache {
38
39 private static Log logger = LogFactory.getLog(BundleCache.class);
40
41 private static Map bundles = null;
42
43 /***
44 * <p>All messages that can be generated by <code>KarmaException</code> instances.
45 * <p/>
46 * <p>This <code>final</code> thingie expects <code>error-messages_<locale>.properties</code> to be present
47 * in the classpath. The locale is read from the <code>locale</code> property in <code>karma.properties</code>.
48 * <p/>
49 * <p>This bundle can also be obtained using <code>getBundle("ERROR_MESSAGES")</code>.
50 */
51 private static final ResourceBundle ERROR_MESSAGES =
52 ResourceBundle.getBundle("error-messages", Locale.ENGLISH);
53
54
55 public static final String ERROR_MESSAGES_KEY = "ERROR_MESSAGES";
56
57 /***
58 * <p>All messages that are sent to a frontend interface application (messages not coming from an exception).
59 * <p/>
60 * <p>This <code>final</code> thingie expects <code>frontend-messages_<locale>.properties</code> to be present
61 * in the classpath. The locale is read from the <code>locale</code> property in <code>karma.properties</code>.
62 * <p/>
63 * <p>This bundle can also be obtained using <code>getBundle("FRONTEND_MESSAGES")</code>
64 */
65 private static final ResourceBundle FRONTEND_MESSAGES =
66 ResourceBundle.getBundle("frontend-messages", Locale.ENGLISH);
67
68
69 public static final String FRONTEND_MESSAGES_KEY = "FRONTEND_MESSAGES";
70
71
72 private static BundleCache instance = null;
73
74 /***
75 * Initializes the cache or returns the cache.
76 *
77 * @return The cache instance.
78 */
79 public synchronized static BundleCache getInstance() {
80 return (instance == null ? new BundleCache() : instance);
81 }
82
83
84 private BundleCache() {
85 bundles = new Hashtable();
86
87 register("ERROR_MESSAGES", ERROR_MESSAGES);
88 register("FRONTEND_MESSAGES", FRONTEND_MESSAGES);
89 }
90
91
92 /***
93 * Registers a resource bundle in the cache.
94 *
95 * @param bundleKey The unique key to the bundle. <b>All keys are transformed into uppercase.</b>
96 * @param bundle The resource bundle to register in this cache.
97 */
98 public final void register(String bundleKey, ResourceBundle bundle) {
99
100 if ((bundleKey == null) || (bundleKey.length() == 0)) {
101 throw new KarmaRuntimeException("Registration key for the resource bundle cannot be null or empty.");
102 }
103
104 if (bundle == null) {
105 throw new NullPointerException("Resource bundle should not be null.");
106 }
107
108 if (bundles.values().contains(bundleKey)) {
109 throw new KarmaRuntimeException("Registration key " + bundleKey + " already exist in cache.");
110 }
111
112 bundles.put(bundleKey, bundle);
113 }
114
115 /***
116 * Retrieves a bundle from the cache by <code>bundleKey</code>.
117 *
118 * @param bundleKey The unique key to this bundle.
119 * @return The resource bundle as identified by <code>bundleKey</code> in the cache.
120 */
121 public final ResourceBundle getBundle(String bundleKey) {
122
123 if (bundles.keySet().contains(bundleKey)) {
124 return (ResourceBundle) bundles.get(bundleKey);
125 }
126 throw new KarmaRuntimeException("Resource bundle for key " + bundleKey + " does not exist in this cache.");
127 }
128
129 /***
130 * Flushes this cache.
131 */
132 public synchronized final void flush() {
133 bundles.keySet().clear();
134 }
135
136 }