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.DefaultStaticContentLoader;
30 import org.apache.struts2.dispatcher.StaticContentLoader;
31 import org.apache.struts2.dispatcher.mapper.ActionMapper;
32 import org.apache.struts2.dispatcher.multipart.MultiPartRequest;
33 import org.apache.struts2.views.freemarker.FreemarkerManager;
34 import org.apache.struts2.views.velocity.VelocityManager;
35
36 import com.opensymphony.xwork2.ActionProxyFactory;
37 import com.opensymphony.xwork2.ObjectFactory;
38 import com.opensymphony.xwork2.TextProvider;
39 import com.opensymphony.xwork2.UnknownHandlerManager;
40 import com.opensymphony.xwork2.config.Configuration;
41 import com.opensymphony.xwork2.config.ConfigurationException;
42 import com.opensymphony.xwork2.config.ConfigurationProvider;
43 import com.opensymphony.xwork2.conversion.ObjectTypeDeterminer;
44 import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
45 import com.opensymphony.xwork2.inject.Container;
46 import com.opensymphony.xwork2.inject.ContainerBuilder;
47 import com.opensymphony.xwork2.inject.Context;
48 import com.opensymphony.xwork2.inject.Factory;
49 import com.opensymphony.xwork2.inject.Scope;
50 import com.opensymphony.xwork2.util.*;
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 * <tr>
155 * <td>org.apache.struts2.views.dispatcher.DefaultStaticContentLoader</td>
156 * <td>struts.staticContentLoader</td>
157 * <td>singleton</td>
158 * <td>Loads static resources (since 2.1)</td>
159 * </tr>
160 * </table>
161 *
162 * <!-- END SNIPPET: extensionPoints -->
163 * </p>
164 * <p>
165 * Implementations are selected using the value of its associated property. That property is
166 * used to determine the implementation by:
167 * </p>
168 * <ol>
169 * <li>Trying to find an existing bean by that name in the container</li>
170 * <li>Trying to find a class by that name, then creating a new bean factory for it</li>
171 * <li>Creating a new delegation bean factory that delegates to the configured ObjectFactory at runtime</li>
172 * </ol>
173 * <p>
174 * Finally, this class overrides certain properties if dev mode is enabled:
175 * </p>
176 * <ul>
177 * <li><code>struts.i18n.reload = true</code></li>
178 * <li><code>struts.configuration.xml.reload = true</code></li>
179 * </ul>
180 */
181 public class BeanSelectionProvider implements ConfigurationProvider {
182 public static final String DEFAULT_BEAN_NAME = "struts";
183 private static final Logger LOG = LoggerFactory.getLogger(BeanSelectionProvider.class);
184
185 public void destroy() {
186
187 }
188
189 public void loadPackages() throws ConfigurationException {
190
191 }
192
193 public void init(Configuration configuration) throws ConfigurationException {
194
195
196 }
197
198 public boolean needsReload() {
199 return false;
200 }
201
202 public void register(ContainerBuilder builder, LocatableProperties props) {
203 alias(ObjectFactory.class, StrutsConstants.STRUTS_OBJECTFACTORY, builder, props);
204 alias(XWorkConverter.class, StrutsConstants.STRUTS_XWORKCONVERTER, builder, props);
205 alias(TextProvider.class, StrutsConstants.STRUTS_XWORKTEXTPROVIDER, builder, props, Scope.DEFAULT);
206 alias(ActionProxyFactory.class, StrutsConstants.STRUTS_ACTIONPROXYFACTORY, builder, props);
207 alias(ObjectTypeDeterminer.class, StrutsConstants.STRUTS_OBJECTTYPEDETERMINER, builder, props);
208 alias(ActionMapper.class, StrutsConstants.STRUTS_MAPPER_CLASS, builder, props);
209 alias(MultiPartRequest.class, StrutsConstants.STRUTS_MULTIPART_PARSER, builder, props, Scope.DEFAULT);
210 alias(FreemarkerManager.class, StrutsConstants.STRUTS_FREEMARKER_MANAGER_CLASSNAME, builder, props);
211 alias(VelocityManager.class, StrutsConstants.STRUTS_VELOCITY_MANAGER_CLASSNAME, builder, props);
212 alias(UrlRenderer.class, StrutsConstants.STRUTS_URL_RENDERER, builder, props);
213 alias(ActionValidatorManager.class, StrutsConstants.STRUTS_ACTIONVALIDATORMANAGER, builder, props);
214 alias(ValueStackFactory.class, StrutsConstants.STRUTS_VALUESTACKFACTORY, builder, props);
215 alias(ReflectionProvider.class, StrutsConstants.STRUTS_REFLECTIONPROVIDER, builder, props);
216 alias(ReflectionContextFactory.class, StrutsConstants.STRUTS_REFLECTIONCONTEXTFACTORY, builder, props);
217 alias(PatternMatcher.class, StrutsConstants.STRUTS_PATTERNMATCHER, builder, props);
218 alias(StaticContentLoader.class, StrutsConstants.STRUTS_STATIC_CONTENT_LOADER, builder, props);
219 alias(UnknownHandlerManager.class, StrutsConstants.STRUTS_UNKNOWN_HANDLER_MANAGER, builder, props);
220
221 if ("true".equalsIgnoreCase(props.getProperty(StrutsConstants.STRUTS_DEVMODE))) {
222 props.setProperty(StrutsConstants.STRUTS_I18N_RELOAD, "true");
223 props.setProperty(StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD, "true");
224 props.setProperty(StrutsConstants.STRUTS_FREEMARKER_TEMPLATES_CACHE, "false");
225
226 props.setProperty("devMode", "true");
227 } else {
228 props.setProperty("devMode", "false");
229 }
230
231 String val = props.getProperty(StrutsConstants.STRUTS_ALLOW_STATIC_METHOD_ACCESS);
232 if (val != null) {
233 props.setProperty("allowStaticMethodAccess", val);
234 }
235
236
237
238
239 LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages");
240
241 String bundles = props.getProperty(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES);
242 if (bundles != null && bundles.length() > 0) {
243 StringTokenizer customBundles = new StringTokenizer(props.getProperty(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES), ", ");
244
245 while (customBundles.hasMoreTokens()) {
246 String name = customBundles.nextToken();
247 try {
248 LOG.info("Loading global messages from " + name);
249 LocalizedTextUtil.addDefaultResourceBundle(name);
250 } catch (Exception e) {
251 LOG.error("Could not find messages file " + name + ".properties. Skipping");
252 }
253 }
254 }
255 }
256
257 void alias(Class type, String key, ContainerBuilder builder, Properties props) {
258 alias(type, key, builder, props, Scope.SINGLETON);
259 }
260
261 void alias(Class type, String key, ContainerBuilder builder, Properties props, Scope scope) {
262 if (!builder.contains(type)) {
263 String foundName = props.getProperty(key, DEFAULT_BEAN_NAME);
264 if (builder.contains(type, foundName)) {
265 if (LOG.isDebugEnabled()) {
266 LOG.info("Choosing bean ("+foundName+") for "+type);
267 }
268 builder.alias(type, foundName, Container.DEFAULT_NAME);
269 } else {
270 try {
271 Class cls = ClassLoaderUtil.loadClass(foundName, this.getClass());
272 if (LOG.isDebugEnabled()) {
273 LOG.debug("Choosing bean ("+cls+") for "+type);
274 }
275 builder.factory(type, cls, scope);
276 } catch (ClassNotFoundException ex) {
277
278 if (LOG.isDebugEnabled()) {
279 LOG.debug("Choosing bean ("+foundName+") for "+type+" to be loaded from the ObjectFactory");
280 }
281 if (DEFAULT_BEAN_NAME.equals(foundName)) {
282
283 } else {
284 if (ObjectFactory.class != type) {
285 builder.factory(type, new ObjectFactoryDelegateFactory(foundName, type), scope);
286 } else {
287 throw new ConfigurationException("Cannot locate the chosen ObjectFactory implementation: "+foundName);
288 }
289 }
290 }
291 }
292 } else {
293 LOG.warn("Unable to alias bean type "+type+", default mapping already assigned.");
294 }
295 }
296
297 static class ObjectFactoryDelegateFactory implements Factory {
298 String name;
299 Class type;
300 ObjectFactoryDelegateFactory(String name, Class type) {
301 this.name = name;
302 this.type = type;
303 }
304
305 public Object create(Context context) throws Exception {
306 ObjectFactory objFactory = context.getContainer().getInstance(ObjectFactory.class);
307 try {
308 return objFactory.buildBean(name, null, true);
309 } catch (ClassNotFoundException ex) {
310 throw new ConfigurationException("Unable to load bean "+type.getName()+" ("+name+")");
311 }
312 }
313 }
314 }