View Javadoc

1   /*
2    * $Id: BeanSelectionProvider.java 789432 2009-06-29 20:15:24Z musachy $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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         // NO-OP
187     }
188 
189     public void loadPackages() throws ConfigurationException {
190         // NO-OP
191     }
192 
193     public void init(Configuration configuration) throws ConfigurationException {
194         // NO-OP
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             // Convert struts properties into ones that xwork expects
226             props.setProperty("devMode", "true");
227         } else {
228             props.setProperty("devMode", "false");
229         }
230 
231         if (props.containsKey(StrutsConstants.STRUTS_LOG_MISSING_PROPERTIES))
232             props.setProperty("logMissingProperties", props.getProperty(StrutsConstants.STRUTS_LOG_MISSING_PROPERTIES));
233 
234         if (props.containsKey(StrutsConstants.STRUTS_ENABLE_OGNL_EXPRESSION_CACHE))
235             props.setProperty("enableOGNLExpressionCache", props.getProperty(StrutsConstants.STRUTS_ENABLE_OGNL_EXPRESSION_CACHE));
236 
237         String val = props.getProperty(StrutsConstants.STRUTS_ALLOW_STATIC_METHOD_ACCESS);
238         if (val != null) {
239             props.setProperty("allowStaticMethodAccess", val);
240         }
241 
242         // TODO: This should be moved to XWork after 2.0.4
243         // struts.custom.i18n.resources
244 
245         LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages");
246 
247         String bundles = props.getProperty(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES);
248         if (bundles != null && bundles.length() > 0) {
249             StringTokenizer customBundles = new StringTokenizer(props.getProperty(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES), ", ");
250 
251             while (customBundles.hasMoreTokens()) {
252                 String name = customBundles.nextToken();
253                 try {
254                     LOG.info("Loading global messages from " + name);
255                     LocalizedTextUtil.addDefaultResourceBundle(name);
256                 } catch (Exception e) {
257                     LOG.error("Could not find messages file " + name + ".properties. Skipping");
258                 }
259             }
260         }
261     }
262 
263     void alias(Class type, String key, ContainerBuilder builder, Properties props) {
264         alias(type, key, builder, props, Scope.SINGLETON);
265     }
266 
267     void alias(Class type, String key, ContainerBuilder builder, Properties props, Scope scope) {
268         if (!builder.contains(type)) {
269             String foundName = props.getProperty(key, DEFAULT_BEAN_NAME);
270             if (builder.contains(type, foundName)) {
271                 if (LOG.isDebugEnabled()) {
272                     LOG.info("Choosing bean ("+foundName+") for "+type);
273                 }
274                 builder.alias(type, foundName, Container.DEFAULT_NAME);
275             } else {
276                 try {
277                     Class cls = ClassLoaderUtil.loadClass(foundName, this.getClass());
278                     if (LOG.isDebugEnabled()) {
279                         LOG.debug("Choosing bean ("+cls+") for "+type);
280                     }
281                     builder.factory(type, cls, scope);
282                 } catch (ClassNotFoundException ex) {
283                     // Perhaps a spring bean id, so we'll delegate to the object factory at runtime
284                     if (LOG.isDebugEnabled()) {
285                         LOG.debug("Choosing bean ("+foundName+") for "+type+" to be loaded from the ObjectFactory");
286                     }
287                     if (DEFAULT_BEAN_NAME.equals(foundName)) {
288                         // Probably an optional bean, will ignore
289                     } else {
290                         if (ObjectFactory.class != type) {
291                             builder.factory(type, new ObjectFactoryDelegateFactory(foundName, type), scope);
292                         } else {
293                             throw new ConfigurationException("Cannot locate the chosen ObjectFactory implementation: "+foundName);
294                         }
295                     }
296                 }
297             }
298         } else {
299             LOG.warn("Unable to alias bean type "+type+", default mapping already assigned.");
300         }
301     }
302 
303     static class ObjectFactoryDelegateFactory implements Factory {
304         String name;
305         Class type;
306         ObjectFactoryDelegateFactory(String name, Class type) {
307             this.name = name;
308             this.type = type;
309         }
310 
311         public Object create(Context context) throws Exception {
312             ObjectFactory objFactory = context.getContainer().getInstance(ObjectFactory.class);
313             try {
314                 return objFactory.buildBean(name, null, true);
315             } catch (ClassNotFoundException ex) {
316                 throw new ConfigurationException("Unable to load bean "+type.getName()+" ("+name+")");
317             }
318         }
319     }
320 }