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.Properties;
25 import java.util.StringTokenizer;
26
27 import org.apache.struts2.StrutsConstants;
28 import org.apache.struts2.components.UrlRenderer;
29 import org.apache.struts2.dispatcher.mapper.ActionMapper;
30 import org.apache.struts2.dispatcher.multipart.MultiPartRequest;
31 import org.apache.struts2.views.freemarker.FreemarkerManager;
32 import org.apache.struts2.views.velocity.VelocityManager;
33
34 import com.opensymphony.xwork2.ActionProxyFactory;
35 import com.opensymphony.xwork2.ObjectFactory;
36 import com.opensymphony.xwork2.TextProvider;
37 import com.opensymphony.xwork2.config.Configuration;
38 import com.opensymphony.xwork2.config.ConfigurationException;
39 import com.opensymphony.xwork2.config.ConfigurationProvider;
40 import com.opensymphony.xwork2.conversion.ObjectTypeDeterminer;
41 import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
42 import com.opensymphony.xwork2.inject.Container;
43 import com.opensymphony.xwork2.inject.ContainerBuilder;
44 import com.opensymphony.xwork2.inject.Context;
45 import com.opensymphony.xwork2.inject.Factory;
46 import com.opensymphony.xwork2.inject.Scope;
47 import com.opensymphony.xwork2.util.ClassLoaderUtil;
48 import com.opensymphony.xwork2.util.LocalizedTextUtil;
49 import com.opensymphony.xwork2.util.PatternMatcher;
50 import com.opensymphony.xwork2.util.ValueStackFactory;
51 import com.opensymphony.xwork2.util.location.LocatableProperties;
52 import com.opensymphony.xwork2.util.logging.Logger;
53 import com.opensymphony.xwork2.util.logging.LoggerFactory;
54 import com.opensymphony.xwork2.util.reflection.ReflectionContextFactory;
55 import com.opensymphony.xwork2.util.reflection.ReflectionProvider;
56 import com.opensymphony.xwork2.validator.ActionValidatorManager;
57
58 /***
59 * Selects the implementations of key framework extension points, using the loaded
60 * property constants. The implementations are selected from the container builder
61 * using the name defined in its associated property. The default implementation name will
62 * always be "struts".
63 *
64 * <p>
65 * The following is a list of the allowed extension points:
66 *
67 * <!-- START SNIPPET: extensionPoints -->
68 * <table border="1">
69 * <tr>
70 * <th>Type</th>
71 * <th>Property</th>
72 * <th>Scope</th>
73 * <th>Description</th>
74 * </tr>
75 * <tr>
76 * <td>com.opensymphony.xwork2.ObjectFactory</td>
77 * <td>struts.objectFactory</td>
78 * <td>singleton</td>
79 * <td>Creates actions, results, and interceptors</td>
80 * </tr>
81 * <tr>
82 * <td>com.opensymphony.xwork2.ActionProxyFactory</td>
83 * <td>struts.actionProxyFactory</td>
84 * <td>singleton</td>
85 * <td>Creates the ActionProxy</td>
86 * </tr>
87 * <tr>
88 * <td>com.opensymphony.xwork2.util.ObjectTypeDeterminer</td>
89 * <td>struts.objectTypeDeterminer</td>
90 * <td>singleton</td>
91 * <td>Determines what the key and element class of a Map or Collection should be</td>
92 * </tr>
93 * <tr>
94 * <td>org.apache.struts2.dispatcher.mapper.ActionMapper</td>
95 * <td>struts.mapper.class</td>
96 * <td>singleton</td>
97 * <td>Determines the ActionMapping from a request and a URI from an ActionMapping</td>
98 * </tr>
99 * <tr>
100 * <td>org.apache.struts2.dispatcher.multipart.MultiPartRequest</td>
101 * <td>struts.multipart.parser</td>
102 * <td>per request</td>
103 * <td>Parses a multipart request (file upload)</td>
104 * </tr>
105 * <tr>
106 * <td>org.apache.struts2.views.freemarker.FreemarkerManager</td>
107 * <td>struts.freemarker.manager.classname</td>
108 * <td>singleton</td>
109 * <td>Loads and processes Freemarker templates</td>
110 * </tr>
111 * <tr>
112 * <td>org.apache.struts2.views.velocity.VelocityManager</td>
113 * <td>struts.velocity.manager.classname</td>
114 * <td>singleton</td>
115 * <td>Loads and processes Velocity templates</td>
116 * </tr>
117 * <tr>
118 * <td>com.opensymphony.xwork2.validator.ActionValidatorManager</td>
119 * <td>struts.actionValidatorManager</td>
120 * <td>singleton</td>
121 * <td>Main interface for validation managers (regular and annotation based). Handles both the loading of
122 * configuration and the actual validation (since 2.1)</td>
123 * </tr>
124 * <tr>
125 * <td>com.opensymphony.xwork2.util.ValueStackFactory</td>
126 * <td>struts.valueStackFactory</td>
127 * <td>singleton</td>
128 * <td>Creates value stacks (since 2.1)</td>
129 * </tr>
130 * <tr>
131 * <td>com.opensymphony.xwork2.reflection.ReflectionProvider</td>
132 * <td>struts.reflectionProvider</td>
133 * <td>singleton</td>
134 * <td>Provides reflection services, key place to plug in a custom expression language (since 2.1)</td>
135 * </tr>
136 * <tr>
137 * <td>com.opensymphony.xwork2.reflection.ReflectionContextFactory</td>
138 * <td>struts.reflectionContextFactory</td>
139 * <td>singleton</td>
140 * <td>Creates reflection context maps used for reflection and expression language operations (since 2.1)</td>
141 * </tr>
142 * <tr>
143 * <td>com.opensymphony.xwork2.config.PackageProvider</td>
144 * <td>N/A</td>
145 * <td>singleton</td>
146 * <td>All beans registered as PackageProvider implementations will be automatically included in configuration building (since 2.1)</td>
147 * </tr>
148 * <tr>
149 * <td>com.opensymphony.xwork2.util.PatternMatcher</td>
150 * <td>struts.patternMatcher</td>
151 * <td>singleton</td>
152 * <td>Matches patterns, such as action names, generally used in configuration (since 2.1)</td>
153 * </tr>
154 * </table>
155 *
156 * <!-- END SNIPPET: extensionPoints -->
157 * </p>
158 * <p>
159 * Implementations are selected using the value of its associated property. That property is
160 * used to determine the implementation by:
161 * </p>
162 * <ol>
163 * <li>Trying to find an existing bean by that name in the container</li>
164 * <li>Trying to find a class by that name, then creating a new bean factory for it</li>
165 * <li>Creating a new delegation bean factory that delegates to the configured ObjectFactory at runtime</li>
166 * </ol>
167 * <p>
168 * Finally, this class overrides certain properties if dev mode is enabled:
169 * </p>
170 * <ul>
171 * <li><code>struts.i18n.reload = true</code></li>
172 * <li><code>struts.configuration.xml.reload = true</code></li>
173 * </ul>
174 */
175 public class BeanSelectionProvider implements ConfigurationProvider {
176 public static final String DEFAULT_BEAN_NAME = "struts";
177 private static final Logger LOG = LoggerFactory.getLogger(BeanSelectionProvider.class);
178
179 public void destroy() {
180
181 }
182
183 public void loadPackages() throws ConfigurationException {
184
185 }
186
187 public void init(Configuration configuration) throws ConfigurationException {
188
189
190 }
191
192 public boolean needsReload() {
193 return false;
194 }
195
196 public void register(ContainerBuilder builder, LocatableProperties props) {
197 alias(ObjectFactory.class, StrutsConstants.STRUTS_OBJECTFACTORY, builder, props);
198 alias(XWorkConverter.class, StrutsConstants.STRUTS_XWORKCONVERTER, builder, props);
199 alias(TextProvider.class, StrutsConstants.STRUTS_XWORKTEXTPROVIDER, builder, props, Scope.DEFAULT);
200 alias(ActionProxyFactory.class, StrutsConstants.STRUTS_ACTIONPROXYFACTORY, builder, props);
201 alias(ObjectTypeDeterminer.class, StrutsConstants.STRUTS_OBJECTTYPEDETERMINER, builder, props);
202 alias(ActionMapper.class, StrutsConstants.STRUTS_MAPPER_CLASS, builder, props);
203 alias(MultiPartRequest.class, StrutsConstants.STRUTS_MULTIPART_PARSER, builder, props, Scope.DEFAULT);
204 alias(FreemarkerManager.class, StrutsConstants.STRUTS_FREEMARKER_MANAGER_CLASSNAME, builder, props);
205 alias(VelocityManager.class, StrutsConstants.STRUTS_VELOCITY_MANAGER_CLASSNAME, builder, props);
206 alias(UrlRenderer.class, StrutsConstants.STRUTS_URL_RENDERER, builder, props);
207 alias(ActionValidatorManager.class, StrutsConstants.STRUTS_ACTIONVALIDATORMANAGER, builder, props);
208 alias(ValueStackFactory.class, StrutsConstants.STRUTS_VALUESTACKFACTORY, builder, props);
209 alias(ReflectionProvider.class, StrutsConstants.STRUTS_REFLECTIONPROVIDER, builder, props);
210 alias(ReflectionContextFactory.class, StrutsConstants.STRUTS_REFLECTIONCONTEXTFACTORY, builder, props);
211 alias(PatternMatcher.class, StrutsConstants.STRUTS_PATTERNMATCHER, builder, props);
212
213 if ("true".equalsIgnoreCase(props.getProperty(StrutsConstants.STRUTS_DEVMODE))) {
214 props.setProperty(StrutsConstants.STRUTS_I18N_RELOAD, "true");
215 props.setProperty(StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD, "true");
216 props.setProperty(StrutsConstants.STRUTS_FREEMARKER_TEMPLATES_CACHE, "false");
217
218 props.setProperty("devMode", "true");
219 } else {
220 props.setProperty("devMode", "false");
221 }
222
223 String val = props.getProperty(StrutsConstants.STRUTS_ALLOW_STATIC_METHOD_ACCESS);
224 if (val != null) {
225 props.setProperty("allowStaticMethodAccess", val);
226 }
227
228
229
230
231 LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages");
232
233 String bundles = props.getProperty(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES);
234 if (bundles != null && bundles.length() > 0) {
235 StringTokenizer customBundles = new StringTokenizer(props.getProperty(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES), ", ");
236
237 while (customBundles.hasMoreTokens()) {
238 String name = customBundles.nextToken();
239 try {
240 LOG.info("Loading global messages from " + name);
241 LocalizedTextUtil.addDefaultResourceBundle(name);
242 } catch (Exception e) {
243 LOG.error("Could not find messages file " + name + ".properties. Skipping");
244 }
245 }
246 }
247 }
248
249 void alias(Class type, String key, ContainerBuilder builder, Properties props) {
250 alias(type, key, builder, props, Scope.SINGLETON);
251 }
252
253 void alias(Class type, String key, ContainerBuilder builder, Properties props, Scope scope) {
254 if (!builder.contains(type)) {
255 String foundName = props.getProperty(key, DEFAULT_BEAN_NAME);
256 if (builder.contains(type, foundName)) {
257 if (LOG.isDebugEnabled()) {
258 LOG.info("Choosing bean ("+foundName+") for "+type);
259 }
260 builder.alias(type, foundName, Container.DEFAULT_NAME);
261 } else {
262 try {
263 Class cls = ClassLoaderUtil.loadClass(foundName, this.getClass());
264 if (LOG.isDebugEnabled()) {
265 LOG.debug("Choosing bean ("+cls+") for "+type);
266 }
267 builder.factory(type, cls, scope);
268 } catch (ClassNotFoundException ex) {
269
270 if (LOG.isDebugEnabled()) {
271 LOG.debug("Choosing bean ("+foundName+") for "+type+" to be loaded from the ObjectFactory");
272 }
273 if (DEFAULT_BEAN_NAME.equals(foundName)) {
274
275 } else {
276 if (ObjectFactory.class != type) {
277 builder.factory(type, new ObjectFactoryDelegateFactory(foundName, type), scope);
278 } else {
279 throw new ConfigurationException("Cannot locate the chosen ObjectFactory implementation: "+foundName);
280 }
281 }
282 }
283 }
284 } else {
285 LOG.warn("Unable to alias bean type "+type+", default mapping already assigned.");
286 }
287 }
288
289 static class ObjectFactoryDelegateFactory implements Factory {
290 String name;
291 Class type;
292 ObjectFactoryDelegateFactory(String name, Class type) {
293 this.name = name;
294 this.type = type;
295 }
296
297 public Object create(Context context) throws Exception {
298 ObjectFactory objFactory = context.getContainer().getInstance(ObjectFactory.class);
299 try {
300 return objFactory.buildBean(name, null, true);
301 } catch (ClassNotFoundException ex) {
302 throw new ConfigurationException("Unable to load bean "+type.getName()+" ("+name+")");
303 }
304 }
305 }
306 }