1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.config;
19
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.StringTokenizer;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.struts2.StrutsConstants;
27
28 import com.opensymphony.xwork2.util.LocalizedTextUtil;
29
30
31 /***
32 * Default implementation of Settings - creates and delegates to other settingss by using an internal
33 * {@link DelegatingSettings}.
34 */
35 public class DefaultSettings extends Settings {
36
37 protected Log log = LogFactory.getLog(this.getClass());
38 Settings config;
39
40
41 /***
42 * Creates a new DefaultSettings object by loading all property files
43 * and creating an internal {@link DelegatingSettings} object. All calls to get and set
44 * in this class will call that settings object.
45 */
46 public DefaultSettings() {
47
48
49 ArrayList<Settings> list = new ArrayList<Settings>();
50
51 try {
52 list.add(new PropertiesSettings("struts"));
53 } catch (Exception e) {
54 log.warn("Could not find or error in struts.properties", e);
55 }
56
57 try {
58 list.add(new PropertiesSettings("org/apache/struts2/default"));
59 } catch (Exception e) {
60 log.error("Could not find org/apache/struts2/default.properties", e);
61 }
62
63 Settings[] configList = new Settings[list.size()];
64 config = new DelegatingSettings((Settings[]) list.toArray(configList));
65
66
67 try {
68 StringTokenizer configFiles = new StringTokenizer((String) config.getImpl(StrutsConstants.STRUTS_CUSTOM_PROPERTIES), ",");
69
70 while (configFiles.hasMoreTokens()) {
71 String name = configFiles.nextToken();
72
73 try {
74 list.add(new PropertiesSettings(name));
75 } catch (Exception e) {
76 log.error("Could not find " + name + ".properties. Skipping");
77 }
78 }
79
80 configList = new Settings[list.size()];
81 config = new DelegatingSettings((Settings[]) list.toArray(configList));
82 } catch (IllegalArgumentException e) {
83
84
85
86 }
87
88
89 try {
90
91 LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages");
92 StringTokenizer bundleFiles = new StringTokenizer((String) config.getImpl(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES), ", ");
93
94 while (bundleFiles.hasMoreTokens()) {
95 String name = bundleFiles.nextToken();
96 try {
97 log.info("Loading global messages from " + name);
98 LocalizedTextUtil.addDefaultResourceBundle(name);
99 } catch (Exception e) {
100 log.error("Could not find " + name + ".properties. Skipping");
101 }
102 }
103 } catch (IllegalArgumentException e) {
104
105 }
106 }
107
108
109 /***
110 * Sets the given property - delegates to the internal config implementation.
111 *
112 * @see #set(String, String)
113 */
114 public void setImpl(String aName, String aValue) throws IllegalArgumentException, UnsupportedOperationException {
115 config.setImpl(aName, aValue);
116 }
117
118 /***
119 * Gets the specified property - delegates to the internal config implementation.
120 *
121 * @see #get(String)
122 */
123 public String getImpl(String aName) throws IllegalArgumentException {
124
125 return config.getImpl(aName);
126 }
127
128 /***
129 * Determines whether or not a value has been set - delegates to the internal config implementation.
130 *
131 * @see #isSet(String)
132 */
133 public boolean isSetImpl(String aName) {
134 return config.isSetImpl(aName);
135 }
136
137 /***
138 * Returns a list of all property names - delegates to the internal config implementation.
139 *
140 * @see #list()
141 */
142 public Iterator listImpl() {
143 return config.listImpl();
144 }
145 }