1 package org.kuali.student.ap.i18n;
2
3
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
6
7 import java.util.Collections;
8 import java.util.Enumeration;
9 import java.util.HashSet;
10 import java.util.List;
11 import java.util.Locale;
12 import java.util.MissingResourceException;
13 import java.util.ResourceBundle;
14 import java.util.Set;
15
16
17
18
19
20
21
22
23 public class MergedPropertiesResourceBundleImpl extends ResourceBundle {
24
25 List<ResourceBundle> bundles;
26 private Locale locale;
27
28 private static final Logger LOG = LoggerFactory.getLogger(MergedPropertiesResourceBundleImpl.class);
29
30 public MergedPropertiesResourceBundleImpl(List<ResourceBundle> bundles, Locale locale) {
31 this.bundles = bundles;
32 this.locale = locale;
33 validateKeys();
34 validateLocales();
35 LOG.debug("MergedPropertiesResourceBundleImpl({}, {})", bundles.size(), locale);
36 }
37
38 @Override
39 public Locale getLocale() {
40 return locale;
41 }
42
43 @Override
44 protected Object handleGetObject(String key) {
45 Object retVal = null;
46 for (ResourceBundle bundle : bundles) {
47 validateLocale(this.locale.toString(), bundle);
48 try {
49 retVal = bundle.getObject(key);
50 }
51 catch (MissingResourceException mre) {
52
53 }
54 if (retVal != null) {
55 LOG.debug("Key({}) found in bundle with locale:'{}'", key, bundle.getLocale());
56 break;
57 }
58 }
59 if (retVal == null) {
60 throw new MissingResourceException("Can't find resource for bundle "
61 +this.getClass().getName()
62 +", key "+key,
63 this.getClass().getName(),
64 key);
65 }
66 return retVal;
67 }
68
69 @Override
70 public Enumeration<String> getKeys() {
71 Set<String> keys = new HashSet<String>();
72 for (ResourceBundle bundle : bundles) {
73 List<String> bundleKeys = Collections.list(bundle.getKeys());
74 keys.addAll(bundleKeys);
75 }
76 return Collections.enumeration(keys);
77 }
78
79
80
81
82 private void validateKeys() {
83 int totalCount = 0;
84
85 Set<String> keys = new HashSet<String>();
86 for (ResourceBundle bundle : bundles) {
87 boolean doValidate = true;
88 List<String> bundleKeys = Collections.list(bundle.getKeys());
89
90 if (bundle instanceof DBResourceBundleImpl) {
91
92 doValidate = false;
93 }
94 if (doValidate) {
95 keys.addAll(bundleKeys);
96 totalCount+=bundleKeys.size();
97 }
98 }
99 if (totalCount != keys.size())
100 throw new RuntimeException("Duplicate keys found. Expected: " + totalCount + "; Found: " + keys.size());
101 }
102
103
104
105
106 private void validateLocales() {
107 String the_locale = this.getLocale().toString();
108 for (ResourceBundle bundle : bundles) {
109 validateLocale(the_locale, bundle);
110 }
111 }
112
113 private void validateLocale(String expected, ResourceBundle bundle) {
114 String bundle_locale = bundle.getLocale().toString();
115 if (!expected.equals(bundle_locale) && !bundle_locale.equals("")) {
116
117 LOG.warn("Mismatched locales. Expected '{}' but got '{}' from {}", expected, bundle_locale, bundle);
118 }
119 else {
120 LOG.debug("Expected '{}' and got '{}' from {}", expected, bundle_locale, bundle);
121 }
122 }
123 }