1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.oness.common.webapp.controller.struts;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Locale;
25 import java.util.Properties;
26
27 import org.apache.struts.util.MessageResources;
28
29 /***
30 * <p>
31 * This class is a Custom Message Resource implementation for Struts. This class
32 * searches through a series of resource files passed to it via the constructor
33 * to obtain the localised value for a key passed. If the key cannot be
34 * resolved, it prints a warning message to the logger output.
35 * </p>
36 *
37 * <pre>
38 * <message-resources null="false"
39 * parameter="net.sf.oness.common.webapp.messages.Messages,net.sf.oness.common.webapp.messages.OtherMessages"/>
40 * </pre>
41 *
42 * @todo improve this class
43 *
44 * @author Carlos Sanchez
45 * @version $Revision: 1.1 $
46 */
47 public class CustomMessageResources extends MessageResources {
48
49 /***
50 * hashmap holding the resource bundle for which we have already loaded the
51 * values
52 */
53 protected HashMap bundlesLoaded = new HashMap();
54
55 /*** hashmap for holding ther resource bundle key and value */
56 protected HashMap resourceMap = new HashMap();
57
58 /*** the array containing the names of the resource bundles to look for */
59 List bundles = new ArrayList();
60
61 /***
62 * Constructor accepting a list of resource bundle files.
63 *
64 * @param configFiles
65 * A List of resouce bundle files.
66 */
67 public CustomMessageResources(List configFiles) {
68 super(null, null);
69 this.bundles = configFiles;
70 }
71
72 /***
73 * @see org.apache.struts.util.MessageResources#getMessage(java.util.Locale,
74 * java.lang.String)
75 */
76 public String getMessage(Locale locale, String key) {
77
78 final String methodSig = "getMessage";
79 String message = null;
80
81 String localeKey = "";
82 if (!super.defaultLocale.equals(locale)) {
83 this.localeKey(locale);
84 }
85
86
87 for (int i = 0; i < bundles.size(); i++) {
88 String config = (String) bundles.get(i);
89 this.loadBundle(localeKey, config);
90
91 message = (String) resourceMap.get(key);
92
93 if (message != null) {
94 break;
95 } else {
96 System.out
97 .println("COULD NOT FIND RESOURCE FILE VALUE FOR KEY: "
98 + key);
99 }
100 }
101 return message;
102 }
103
104 /***
105 * Loads the resource bundle based on the locale.
106 *
107 * @param localeKey
108 * The locale
109 * @param config
110 * The resource file.
111 */
112 protected void loadBundle(String localeKey, String config) {
113 final String methodSig = "loadBundle";
114 String fileName = null;
115 InputStream inStream = null;
116 Properties props = new Properties();
117
118 if (bundlesLoaded.get(config) != null) {
119 return;
120 }
121
122
123 this.bundlesLoaded.put(config, localeKey);
124
125
126 fileName = config.replace('.', '/');
127 if (localeKey.length() > 0) {
128 fileName += "_" + localeKey;
129 }
130 fileName += ".properties";
131
132
133 ClassLoader classLoader = Thread.currentThread()
134 .getContextClassLoader();
135
136
137
138
139 if (classLoader == null) {
140 classLoader = this.getClass().getClassLoader();
141 }
142
143
144 inStream = classLoader.getResourceAsStream(fileName);
145
146
147 if (inStream != null) {
148
149 try {
150 props.load(inStream);
151 } catch (IOException e) {
152
153 e.printStackTrace();
154 } finally {
155 try {
156 inStream.close();
157 } catch (IOException e1) {
158
159 e1.printStackTrace();
160 }
161 }
162
163
164
165
166
167 synchronized (this.resourceMap) {
168 Iterator iterator = props.keySet().iterator();
169
170 while (iterator.hasNext()) {
171 String key = (String) iterator.next();
172
173 String value = props.getProperty(key);
174
175 this.resourceMap.put(key, value);
176 }
177 }
178
179 } else {
180 System.out.println("COULD NOT LOAD RESOURCE FILE: " + fileName);
181 }
182
183 }
184
185 }