1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.config;
23
24 import java.util.Iterator;
25 import java.util.Locale;
26 import java.util.StringTokenizer;
27
28 import com.opensymphony.xwork2.config.Configuration;
29 import com.opensymphony.xwork2.config.ConfigurationException;
30 import com.opensymphony.xwork2.config.ConfigurationProvider;
31 import com.opensymphony.xwork2.inject.ContainerBuilder;
32 import com.opensymphony.xwork2.inject.Context;
33 import com.opensymphony.xwork2.inject.Factory;
34 import com.opensymphony.xwork2.inject.Inject;
35 import com.opensymphony.xwork2.util.location.LocatableProperties;
36 import com.opensymphony.xwork2.util.logging.Logger;
37 import com.opensymphony.xwork2.util.logging.LoggerFactory;
38 import org.apache.struts2.StrutsConstants;
39
40 public class LegacyPropertiesConfigurationProvider implements ConfigurationProvider {
41
42 /***
43 * The Logging instance for this class.
44 */
45 private static final Logger LOG = LoggerFactory.getLogger(LegacyPropertiesConfigurationProvider.class);
46
47 public void destroy() {
48 Settings.reset();
49 }
50
51 public void init(Configuration configuration)
52 throws ConfigurationException {
53 Settings.reset();
54 }
55
56 public void loadPackages()
57 throws ConfigurationException {
58 }
59
60 public boolean needsReload() {
61 return false;
62 }
63
64 public void register(ContainerBuilder builder, LocatableProperties props)
65 throws ConfigurationException {
66
67 final Settings settings = Settings.getInstance();
68
69 loadSettings(props, settings);
70
71
72 builder.factory(Locale.class, new Factory() {
73 private Locale locale;
74
75 public synchronized Object create(Context context) throws Exception {
76 if (locale == null) {
77 String loc = context.getContainer().getInstance(String.class, StrutsConstants.STRUTS_LOCALE);
78 if (loc != null) {
79 StringTokenizer localeTokens = new StringTokenizer(loc, "_");
80 String lang = null;
81 String country = null;
82
83 if (localeTokens.hasMoreTokens()) {
84 lang = localeTokens.nextToken();
85 }
86
87 if (localeTokens.hasMoreTokens()) {
88 country = localeTokens.nextToken();
89 }
90 locale = new Locale(lang, country);
91 } else {
92 LOG.info("No locale define, substituting the default VM locale");
93 locale = Locale.getDefault();
94 }
95 }
96 return locale;
97 }
98 });
99 }
100
101 /***
102 * @param props
103 * @param settings
104 */
105 protected void loadSettings(LocatableProperties props, final Settings settings) {
106
107 for (Iterator i = settings.listImpl(); i.hasNext(); ) {
108 String name = (String) i.next();
109 props.setProperty(name, settings.getImpl(name), settings.getLocationImpl(name));
110 }
111 }
112 }