View Javadoc

1   /*
2    * $Id: BeanSelectionProvider.java 560881 2007-07-30 07:05:55Z rgielen $
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  package org.apache.struts2.config;
22  
23  import java.util.Properties;
24  import java.util.StringTokenizer;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.struts2.StrutsConstants;
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.inject.Container;
41  import com.opensymphony.xwork2.inject.ContainerBuilder;
42  import com.opensymphony.xwork2.inject.Context;
43  import com.opensymphony.xwork2.inject.Factory;
44  import com.opensymphony.xwork2.inject.Scope;
45  import com.opensymphony.xwork2.util.ClassLoaderUtil;
46  import com.opensymphony.xwork2.util.LocalizedTextUtil;
47  import com.opensymphony.xwork2.util.ObjectTypeDeterminer;
48  import com.opensymphony.xwork2.util.XWorkConverter;
49  import com.opensymphony.xwork2.util.location.LocatableProperties;
50  
51  /***
52   * Selects the implementations of key framework extension points, using the loaded
53   * property constants.  The implementations are selected from the container builder
54   * using the name defined in its associated property.  The default implementation name will
55   * always be "struts".
56   *
57   * <p>
58   * The following is a list of the allowed extension points:
59   *
60   * <!-- START SNIPPET: extensionPoints -->
61   * <table border="1">
62   *   <tr>
63   *     <th>Type</th>
64   *     <th>Property</th>
65   *     <th>Scope</th>
66   *     <th>Description</th>
67   *   </tr>
68   *   <tr>
69   *     <td>com.opensymphony.xwork2.ObjectFactory</td>
70   *     <td>struts.objectFactory</td>
71   *     <td>singleton</td>
72   *     <td>Creates actions, results, and interceptors</td>
73   *   </tr>
74   *   <tr>
75   *     <td>com.opensymphony.xwork2.ActionProxyFactory</td>
76   *     <td>struts.actionProxyFactory</td>
77   *     <td>singleton</td>
78   *     <td>Creates the ActionProxy</td>
79   *   </tr>
80   *   <tr>
81   *     <td>com.opensymphony.xwork2.util.ObjectTypeDeterminer</td>
82   *     <td>struts.objectTypeDeterminer</td>
83   *     <td>singleton</td>
84   *     <td>Determines what the key and and element class of a Map or Collection should be</td>
85   *   </tr>
86   *   <tr>
87   *     <td>org.apache.struts2.dispatcher.mapper.ActionMapper</td>
88   *     <td>struts.mapper.class</td>
89   *     <td>singleton</td>
90   *     <td>Determines the ActionMapping from a request and a URI from an ActionMapping</td>
91   *   </tr>
92   *   <tr>
93   *     <td>org.apache.struts2.dispatcher.multipart.MultiPartRequest</td>
94   *     <td>struts.multipart.parser</td>
95   *     <td>per request</td>
96   *     <td>Parses a multipart request (file upload)</td>
97   *   </tr>
98   *   <tr>
99   *     <td>org.apache.struts2.views.freemarker.FreemarkerManager</td>
100  *     <td>struts.freemarker.manager.classname</td>
101  *     <td>singleton</td>
102  *     <td>Loads and processes Freemarker templates</td>
103  *   </tr>
104  *   <tr>
105  *     <td>org.apache.struts2.views.velocity.VelocityManager</td>
106  *     <td>struts.velocity.manager.classname</td>
107  *     <td>singleton</td>
108  *     <td>Loads and processes Velocity templates</td>
109  *   </tr>
110  * </table>
111  *
112  * <!-- END SNIPPET: extensionPoints -->
113  * </p>
114  * <p>
115  * Implementations are selected using the value of its associated property.  That property is
116  * used to determine the implementation by:
117  * </p>
118  * <ol>
119  *   <li>Trying to find an existing bean by that name in the container</li>
120  *   <li>Trying to find a class by that name, then creating a new bean factory for it</li>
121  *   <li>Creating a new delegation bean factory that delegates to the configured ObjectFactory at runtime</li>
122  * </ol>
123  * <p>
124  * Finally, this class overrides certain properties if dev mode is enabled:
125  * </p>
126  * <ul>
127  *   <li><code>struts.i18n.reload = true</code></li>
128  *   <li><code>struts.configuration.xml.reload = true</code></li>
129  * </ul>
130  */
131 public class BeanSelectionProvider implements ConfigurationProvider {
132     public static final String DEFAULT_BEAN_NAME = "struts";
133     private static final Log LOG = LogFactory.getLog(BeanSelectionProvider.class);
134     
135     public void destroy() {
136         // NO-OP
137     }
138 
139     public void loadPackages() throws ConfigurationException {
140         // NO-OP
141     }
142     
143     public void init(Configuration configuration) throws ConfigurationException {
144         // NO-OP
145         
146     }
147 
148     public boolean needsReload() {
149         return false;
150     }
151 
152     public void register(ContainerBuilder builder, LocatableProperties props) {
153         alias(ObjectFactory.class, StrutsConstants.STRUTS_OBJECTFACTORY, builder, props);
154         alias(XWorkConverter.class, StrutsConstants.STRUTS_XWORKCONVERTER, builder, props);
155         alias(TextProvider.class, StrutsConstants.STRUTS_XWORKTEXTPROVIDER, builder, props);
156         alias(ActionProxyFactory.class, StrutsConstants.STRUTS_ACTIONPROXYFACTORY, builder, props);
157         alias(ObjectTypeDeterminer.class, StrutsConstants.STRUTS_OBJECTTYPEDETERMINER, builder, props);
158         alias(ActionMapper.class, StrutsConstants.STRUTS_MAPPER_CLASS, builder, props);
159         alias(MultiPartRequest.class, StrutsConstants.STRUTS_MULTIPART_PARSER, builder, props, Scope.DEFAULT);
160         alias(FreemarkerManager.class, StrutsConstants.STRUTS_FREEMARKER_MANAGER_CLASSNAME, builder, props);
161         alias(VelocityManager.class, StrutsConstants.STRUTS_VELOCITY_MANAGER_CLASSNAME, builder, props);
162         
163         if ("true".equalsIgnoreCase(props.getProperty(StrutsConstants.STRUTS_DEVMODE))) {
164             props.setProperty(StrutsConstants.STRUTS_I18N_RELOAD, "true");
165             props.setProperty(StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD, "true");
166             props.setProperty(StrutsConstants.STRUTS_FREEMARKER_TEMPLATES_CACHE, "false");
167             // Convert struts properties into ones that xwork expects
168             props.setProperty("devMode", "true");
169         } else {
170             props.setProperty("devMode", "false");
171         }
172         
173         // TODO: This should be moved to XWork after 2.0.4
174         // struts.custom.i18n.resources
175 
176         LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages");
177         
178         String bundles = props.getProperty(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES);
179         if (bundles != null && bundles.length() > 0) { 
180             StringTokenizer customBundles = new StringTokenizer(props.getProperty(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES), ", ");
181             
182             while (customBundles.hasMoreTokens()) {
183                 String name = customBundles.nextToken();
184                 try {
185                     LOG.info("Loading global messages from " + name);
186                     LocalizedTextUtil.addDefaultResourceBundle(name);
187                 } catch (Exception e) {
188                     LOG.error("Could not find messages file " + name + ".properties. Skipping");
189                 }
190             } 
191         }
192     }
193     
194     void alias(Class type, String key, ContainerBuilder builder, Properties props) {
195         alias(type, key, builder, props, Scope.SINGLETON);
196     }
197     
198     void alias(Class type, String key, ContainerBuilder builder, Properties props, Scope scope) {
199         if (!builder.contains(type)) {
200             String foundName = props.getProperty(key, DEFAULT_BEAN_NAME);
201             if (builder.contains(type, foundName)) {
202                 if (LOG.isDebugEnabled()) {
203                     LOG.info("Choosing bean ("+foundName+") for "+type);
204                 }
205                 builder.alias(type, foundName, Container.DEFAULT_NAME);
206             } else {
207                 try {
208                     Class cls = ClassLoaderUtil.loadClass(foundName, this.getClass());
209                     if (LOG.isDebugEnabled()) {
210                         LOG.debug("Choosing bean ("+cls+") for "+type);
211                     }
212                     builder.factory(type, cls, scope);
213                 } catch (ClassNotFoundException ex) {
214                     // Perhaps a spring bean id, so we'll delegate to the object factory at runtime
215                     if (LOG.isDebugEnabled()) {
216                         LOG.debug("Choosing bean ("+foundName+") for "+type+" to be loaded from the ObjectFactory");
217                     }
218                     if (DEFAULT_BEAN_NAME.equals(foundName)) {
219                         // Probably an optional bean, will ignore
220                     } else {
221                         if (ObjectFactory.class != type) {
222                             builder.factory(type, new ObjectFactoryDelegateFactory(foundName, type), scope);
223                         } else {
224                             throw new ConfigurationException("Cannot locate the chosen ObjectFactory implementation: "+foundName);
225                         }
226                     }
227                 }
228             }
229         } else {
230             LOG.warn("Unable to alias bean type "+type+", default mapping already assigned.");
231         }
232     }
233     
234     class ObjectFactoryDelegateFactory implements Factory {
235         String name;
236         Class type;
237         ObjectFactoryDelegateFactory(String name, Class type) {
238             this.name = name;
239             this.type = type;
240         }
241         
242         public Object create(Context context) throws Exception {
243             ObjectFactory objFactory = context.getContainer().getInstance(ObjectFactory.class);
244             try {
245                 return objFactory.buildBean(name, null, true);
246             } catch (ClassNotFoundException ex) {
247                 throw new ConfigurationException("Unable to load bean "+type.getName()+" ("+name+")");
248             }
249         }
250     }
251 }