How to implement a reloadable MessageResources for Struts
Commons Configuration support for reloadable files can be leveraged to implement a dynamic resource bundle for Struts. This is done easily by extending the MessageResourcesFactory and MessageResources classes provided by Struts.
ConfigurationMessageResources
1 /*
2 * Copyright 2004 Emmanuel Bourg
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License")
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 import java.net.URL;
18 import java.util.Locale;
19 import java.util.Map;
20
21 import org.apache.commons.configuration.Configuration;
22 import org.apache.commons.configuration.ConfigurationException;
23 import org.apache.commons.configuration.PropertiesConfiguration;
24 import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
25 import org.apache.struts.util.MessageResources;
26 import org.apache.struts.util.MessageResourcesFactory;
27
28 /**
29 * MessageResources based on reloadble configurations.
30 *
31 * @author Emmanuel Bourg
32 */
33 public class ConfigurationMessageResources extends MessageResources {
34
35 /** Configurations associées à leur Locale respective. */
36 protected Map configurations;
37
38 public ConfigurationMessageResources(MessageResourcesFactory factory, String config, boolean returnNull) {
39 super(factory, config, returnNull);
40 }
41
42 public String getMessage(Locale locale, String key) {
43
44 // get the configuration for the specified locale
45 Configuration resource = getConfiguration(this.config + "_" + locale.getLanguage() + ".properties");
46
47 if (resource == null || !resource.containsKey(key)) {
48 // look for the key in the root configuration
49 resource = getConfiguration(this.config + ".properties");
50 }
51
52 return resource != null ? resource.getString(key, null) : null;
53 }
54
55 /**
56 * Load the specified configuration from the classpath and initialize it.
57 */
58 private Configuration getConfiguration(String name) {
59
60 Configuration configuration = null;
61
62 URL url = Thread.currentThread().getContextClassLoader().getResource(name);
63
64 if (url != null) {
65 PropertiesConfiguration pc = new PropertiesConfiguration();
66 PropertiesConfiguration.setDelimiter('\uffff'); // disable string splitting
67 pc.setURL(url);
68 pc.setReloadingStrategy(new FileChangedReloadingStrategy());
69
70 try {
71 pc.load();
72 configuration = pc;
73 } catch (ConfigurationException e) {
74 e.printStackTrace();
75 }
76 }
77
78 return configuration;
79 }
80 }
ConfigurationMessageResourcesFactory
1 /*
2 * Copyright 2004 Emmanuel Bourg
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License")
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 import org.apache.struts.util.MessageResourcesFactory;
18 import org.apache.struts.util.MessageResources;
19
20 /**
21 * Factory for ConfigurationMessageResources.
22 *
23 * @author Emmanuel Bourg
24 */
25 public class ConfigurationMessageResourceFactory extends MessageResourcesFactory {
26
27 public MessageResources createResources(String config) {
28 return new ConfigurationMessageResources(this, config, this.returnNull);
29 }
30 }
struts-config.xml
The last step is to declare the factory in the struts-config.xml file:
<?xml version="1.0"?>
<struts-config>
...
<message-resources factory="ConfigurationMessageResourceFactory" parameter="yourResourceBundle"/>
</struts-config>