1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.messages.providers;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.exception.RiceRuntimeException;
20 import org.kuali.rice.krad.messages.Message;
21 import org.kuali.rice.krad.messages.MessageProvider;
22 import org.kuali.rice.krad.messages.MessageService;
23 import org.kuali.rice.krad.service.KRADServiceLocator;
24 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
25 import org.kuali.rice.krad.service.ModuleService;
26
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.Enumeration;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Locale;
34 import java.util.Map;
35 import java.util.ResourceBundle;
36
37
38
39
40
41
42 public class ResourceMessageProvider implements MessageProvider {
43
44 private static final String COMPONENT_PLACEHOLDER_BEGIN = "@cmp{";
45 private static final String COMPONENT_PLACEHOLDER_END = "}";
46
47 protected Map<String, List<ResourceBundle>> cachedResourceBundles;
48
49 public ResourceMessageProvider() {
50 cachedResourceBundles = new HashMap<String, List<ResourceBundle>>();
51 }
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public Message getMessage(String namespace, String component, String key, String locale) {
70 Message message = null;
71
72 List<ResourceBundle> bundles = getCachedResourceBundles(namespace, locale);
73
74
75
76 String messageText = null;
77 for (ResourceBundle bundle : bundles) {
78
79 String resourceKey = COMPONENT_PLACEHOLDER_BEGIN + component + COMPONENT_PLACEHOLDER_END + key;
80 if (bundle.containsKey(resourceKey)) {
81 messageText = bundle.getString(resourceKey);
82 }
83
84 else if (MessageService.DEFAULT_COMPONENT_CODE.equals(component) && bundle.containsKey(key)) {
85 messageText = bundle.getString(key);
86 }
87 }
88
89
90 if (StringUtils.isNotBlank(messageText)) {
91 message = buildMessage(namespace, component, key, messageText, locale);
92 }
93
94 return message;
95 }
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 public Collection<Message> getAllMessagesForComponent(String namespace, String component, String locale) {
115 List<ResourceBundle> bundles = getCachedResourceBundles(namespace, locale);
116
117 Map<String, Message> messagesByKey = new HashMap<String, Message>();
118 for (ResourceBundle bundle : bundles) {
119 Enumeration<String> resourceKeys = bundle.getKeys();
120 while (resourceKeys.hasMoreElements()) {
121 String resourceKey = resourceKeys.nextElement();
122
123 boolean match = false;
124 if (StringUtils.contains(resourceKey,
125 COMPONENT_PLACEHOLDER_BEGIN + component + COMPONENT_PLACEHOLDER_END)) {
126 match = true;
127 } else if (MessageService.DEFAULT_COMPONENT_CODE.equals(component) && !StringUtils.contains(resourceKey,
128 COMPONENT_PLACEHOLDER_BEGIN)) {
129 match = true;
130 }
131
132 if (match) {
133 String messageText = bundle.getString(resourceKey);
134
135 resourceKey = cleanResourceKey(resourceKey);
136 Message message = buildMessage(namespace, component, resourceKey, messageText, locale);
137 messagesByKey.put(resourceKey, message);
138 }
139 }
140 }
141
142 return messagesByKey.values();
143 }
144
145
146
147
148
149
150
151 protected String cleanResourceKey(String resourceKey) {
152 String cleanedKey = resourceKey;
153
154 String component = StringUtils.substringBetween(cleanedKey, COMPONENT_PLACEHOLDER_BEGIN,
155 COMPONENT_PLACEHOLDER_END);
156 if (StringUtils.isNotBlank(component)) {
157 cleanedKey = StringUtils.remove(cleanedKey,
158 COMPONENT_PLACEHOLDER_BEGIN + component + COMPONENT_PLACEHOLDER_END);
159 }
160
161 return cleanedKey;
162 }
163
164
165
166
167
168
169
170
171
172
173
174 protected Message buildMessage(String namespace, String component, String key, String messageText, String locale) {
175 Message message = new Message();
176
177 message.setNamespaceCode(namespace);
178 message.setComponentCode(component);
179
180 key = cleanResourceKey(key);
181 message.setKey(key);
182
183 message.setText(messageText);
184 message.setLocale(locale);
185
186 return message;
187 }
188
189
190
191
192
193
194
195
196
197 protected List<ResourceBundle> getCachedResourceBundles(String namespace, String localeCode) {
198 if (StringUtils.isBlank(namespace)) {
199 namespace = MessageService.DEFAULT_NAMESPACE_CODE;
200 }
201
202 String cacheKey = namespace + "|" + localeCode;
203 if (cachedResourceBundles.containsKey(cacheKey)) {
204 return cachedResourceBundles.get(cacheKey);
205 }
206
207 List<ResourceBundle> bundles = null;
208 if (StringUtils.isBlank(namespace) || MessageService.DEFAULT_NAMESPACE_CODE.equals(namespace)) {
209 bundles = getResourceBundlesForApplication(localeCode);
210 } else {
211 bundles = getResourceBundlesForNamespace(namespace, localeCode);
212 }
213
214 cachedResourceBundles.put(cacheKey, bundles);
215
216 return bundles;
217 }
218
219
220
221
222
223
224
225
226 protected List<ResourceBundle> getResourceBundlesForNamespace(String namespace, String localeCode) {
227 List<String> resourceBundleNames = getResourceBundleNamesForNamespace(namespace);
228
229 return getResourceBundles(resourceBundleNames, localeCode);
230 }
231
232
233
234
235
236
237
238 protected List<ResourceBundle> getResourceBundlesForApplication(String localeCode) {
239 List<String> resourceBundleNames = getResourceBundleNamesForApplication();
240
241 return getResourceBundles(resourceBundleNames, localeCode);
242 }
243
244
245
246
247
248
249
250
251
252
253
254
255
256 protected List<ResourceBundle> getResourceBundles(List<String> resourceBundleNames, String localeCode) {
257 List<ResourceBundle> resourceBundles = new ArrayList<ResourceBundle>();
258
259 String[] localeIdentifiers = StringUtils.split(localeCode, "-");
260 if ((localeIdentifiers == null) || (localeIdentifiers.length != 2)) {
261 throw new RiceRuntimeException("Invalid locale code: " + (localeCode == null ? "Null" : localeCode));
262 }
263
264 Locale locale = new Locale(localeIdentifiers[0], localeIdentifiers[1]);
265
266 if (resourceBundleNames != null) {
267 for (String bundleName : resourceBundleNames) {
268 ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale);
269 if (bundle != null) {
270 resourceBundles.add(bundle);
271 }
272 }
273 }
274
275 return resourceBundles;
276 }
277
278
279
280
281
282
283
284
285
286
287
288
289 protected List<String> getResourceBundleNamesForNamespace(String namespace) {
290 ModuleService moduleService = KRADServiceLocatorWeb.getKualiModuleService().getModuleServiceByNamespaceCode(
291 namespace);
292 if (moduleService != null) {
293 return moduleService.getModuleConfiguration().getResourceBundleNames();
294 }
295
296 return null;
297 }
298
299
300
301
302
303
304
305
306
307
308
309 protected List<String> getResourceBundleNamesForApplication() {
310 String resourceBundleNamesConfig = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
311 "resourceBundleNames");
312 if (StringUtils.isNotBlank(resourceBundleNamesConfig)) {
313 String[] resourceBundleNames = StringUtils.split(resourceBundleNamesConfig, ",");
314
315 return Arrays.asList(resourceBundleNames);
316 }
317
318 return null;
319 }
320 }