1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.config;
19
20 import java.io.Serializable;
21
22 import java.util.Enumeration;
23 import java.util.Properties;
24
25 /***
26 * <p> A abstract base class for all config classes. Provide basic support
27 * for arbitrary properties </p>
28 *
29 * @since Struts 1.3
30 */
31 public abstract class BaseConfig implements Serializable {
32 /***
33 * Indicates if configuration of this component been completed. TODO
34 * change protected to private and use methods provided by extenders?
35 */
36 protected boolean configured = false;
37
38 /***
39 * A map of arbitrary properties configured for this component.
40 *
41 * @since Struts 1.3
42 */
43 private Properties properties = new Properties();
44
45 /***
46 * Freeze the configuration of this action.
47 */
48 public void freeze() {
49 configured = true;
50 }
51
52 /***
53 * Throw <code>IllegalStateException</code> if configuration is frozen.
54 *
55 * @throws IllegalStateException if configuration is frozen
56 */
57 public void throwIfConfigured() {
58 if (configured) {
59 throw new IllegalStateException("Configuration is frozen");
60 }
61 }
62
63 /***
64 * <p> Set an arbitary key/value pair which can be retrieved by this
65 * config class. This facility should eliminate many use cases for
66 * subclassing <code>*Config</code> classes by providing a mechanism to
67 * pass any amount of arbitrary configuration information into an config
68 * class. <p /> This method must not be called after configuration is
69 * complete, or an <code>IllegalStateException</code> will be thrown.</p>
70 *
71 * <p><b>Example</b>
72 * <code><pre>
73 * <action path="/example" type="com.example.MyAction">
74 * <set-property key="foo" property="bar" />
75 * </action>
76 * </pre></code>
77 * </p>
78 *
79 * @param key the key by which this value will be retrieved
80 * @param value the value to store with the supplied key
81 * @throws IllegalStateException if this module configuration has been
82 * frozen
83 * @since Struts 1.3
84 */
85 public void setProperty(String key, String value) {
86 throwIfConfigured();
87 properties.setProperty(key, value);
88 }
89
90 /***
91 * Return the property-value for the specified key, if any; otherwise
92 * return <code>null</code>.
93 *
94 * @param key a key specified in the <code>struts-config</code> file
95 * @return the value stored with the supplied key
96 * @since Struts 1.3
97 */
98 public String getProperty(String key) {
99 return properties.getProperty(key);
100 }
101
102 /***
103 * <p> Return the entire set of properties configured for this object. At
104 * this time, this only needs to be exposed to support inheritance, so
105 * choosing a conservative access modifier ("protected"). </p>
106 *
107 * @return set of properties configured for this object
108 */
109 protected Properties getProperties() {
110 return this.properties;
111 }
112
113 /***
114 * Set the entire set of properties configured for this object. At this
115 * time, this only needs to be exposed to support inheritance, so choosing
116 * a conservative access modifier ("protected").
117 */
118 protected void setProperties(Properties properties) {
119 this.properties = properties;
120 }
121
122 /***
123 * <p>Compare the properties of this config with that of the given and
124 * copy those that are not present. This method is used by subclasses
125 * that support configuration inheritance.</p>
126 *
127 * @param baseConfig The config object to copy properties from.
128 */
129 protected void inheritProperties(BaseConfig baseConfig) {
130 throwIfConfigured();
131
132
133 Properties baseProperties = baseConfig.getProperties();
134 Enumeration keys = baseProperties.propertyNames();
135
136 while (keys.hasMoreElements()) {
137 String key = (String) keys.nextElement();
138
139
140 String value = this.getProperty(key);
141
142 if (value == null) {
143 value = baseProperties.getProperty(key);
144 setProperty(key, value);
145 }
146 }
147 }
148
149 /***
150 * <p>Return a copy of the properties held by this object.</p>
151 */
152 protected Properties copyProperties() {
153 Properties copy = new Properties();
154
155 Enumeration keys = properties.propertyNames();
156
157 while (keys.hasMoreElements()) {
158 String key = (String) keys.nextElement();
159
160 copy.setProperty(key, properties.getProperty(key));
161 }
162
163 return copy;
164 }
165 }