%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
nl.toolforge.karma.core.bundle.BundleCache |
|
|
1 | /* |
|
2 | Karma core - Core of the Karma application |
|
3 | Copyright (C) 2004 Toolforge <www.toolforge.nl> |
|
4 | ||
5 | This library is free software; you can redistribute it and/or |
|
6 | modify it under the terms of the GNU Lesser General Public |
|
7 | License as published by the Free Software Foundation; either |
|
8 | version 2.1 of the License, or (at your option) any later version. |
|
9 | ||
10 | This library is distributed in the hope that it will be useful, |
|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 | Lesser General Public License for more details. |
|
14 | ||
15 | You should have received a copy of the GNU Lesser General Public |
|
16 | License along with this library; if not, write to the Free Software |
|
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
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 | 52 | private static Log logger = LogFactory.getLog(BundleCache.class); |
40 | ||
41 | 26 | 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 | 26 | private static final ResourceBundle ERROR_MESSAGES = |
52 | ResourceBundle.getBundle("error-messages", Locale.ENGLISH); |
|
53 | //todo read from system |
|
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 | 26 | private static final ResourceBundle FRONTEND_MESSAGES = |
66 | ResourceBundle.getBundle("frontend-messages", Locale.ENGLISH); |
|
67 | //todo read from system |
|
68 | ||
69 | public static final String FRONTEND_MESSAGES_KEY = "FRONTEND_MESSAGES"; |
|
70 | ||
71 | ||
72 | 26 | 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 | 208 | return (instance == null ? new BundleCache() : instance); |
81 | } |
|
82 | ||
83 | ||
84 | 208 | private BundleCache() { |
85 | 208 | bundles = new Hashtable(); |
86 | ||
87 | 208 | register("ERROR_MESSAGES", ERROR_MESSAGES); |
88 | 208 | register("FRONTEND_MESSAGES", FRONTEND_MESSAGES); |
89 | 208 | } |
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 | 546 | if ((bundleKey == null) || (bundleKey.length() == 0)) { |
101 | 52 | throw new KarmaRuntimeException("Registration key for the resource bundle cannot be null or empty."); |
102 | } |
|
103 | ||
104 | 494 | if (bundle == null) { |
105 | 26 | throw new NullPointerException("Resource bundle should not be null."); |
106 | } |
|
107 | ||
108 | 468 | if (bundles.values().contains(bundleKey)) { |
109 | 0 | throw new KarmaRuntimeException("Registration key " + bundleKey + " already exist in cache."); |
110 | } |
|
111 | ||
112 | 468 | bundles.put(bundleKey, bundle); |
113 | 468 | } |
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 | 78 | if (bundles.keySet().contains(bundleKey)) { |
124 | 52 | return (ResourceBundle) bundles.get(bundleKey); |
125 | } |
|
126 | 26 | 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 | 312 | bundles.keySet().clear(); |
134 | 312 | } |
135 | ||
136 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |