1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
137 }
138
139 public void loadPackages() throws ConfigurationException {
140
141 }
142
143 public void init(Configuration configuration) throws ConfigurationException {
144
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
167 props.setProperty("devMode", "true");
168 } else {
169 props.setProperty("devMode", "false");
170 }
171
172
173
174
175 LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages");
176
177 String bundles = props.getProperty(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES);
178 if (bundles != null && bundles.length() > 0) {
179 StringTokenizer customBundles = new StringTokenizer(props.getProperty(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES), ", ");
180
181 while (customBundles.hasMoreTokens()) {
182 String name = customBundles.nextToken();
183 try {
184 LOG.info("Loading global messages from " + name);
185 LocalizedTextUtil.addDefaultResourceBundle(name);
186 } catch (Exception e) {
187 LOG.error("Could not find messages file " + name + ".properties. Skipping");
188 }
189 }
190 }
191 }
192
193 void alias(Class type, String key, ContainerBuilder builder, Properties props) {
194 alias(type, key, builder, props, Scope.SINGLETON);
195 }
196
197 void alias(Class type, String key, ContainerBuilder builder, Properties props, Scope scope) {
198 if (!builder.contains(type)) {
199 String foundName = props.getProperty(key, DEFAULT_BEAN_NAME);
200 if (builder.contains(type, foundName)) {
201 if (LOG.isDebugEnabled()) {
202 LOG.info("Choosing bean ("+foundName+") for "+type);
203 }
204 builder.alias(type, foundName, Container.DEFAULT_NAME);
205 } else {
206 try {
207 Class cls = ClassLoaderUtil.loadClass(foundName, this.getClass());
208 if (LOG.isDebugEnabled()) {
209 LOG.debug("Choosing bean ("+cls+") for "+type);
210 }
211 builder.factory(type, cls, scope);
212 } catch (ClassNotFoundException ex) {
213
214 if (LOG.isDebugEnabled()) {
215 LOG.debug("Choosing bean ("+foundName+") for "+type+" to be loaded from the ObjectFactory");
216 }
217 if (DEFAULT_BEAN_NAME.equals(foundName)) {
218
219 } else {
220 if (ObjectFactory.class != type) {
221 builder.factory(type, new ObjectFactoryDelegateFactory(foundName, type), scope);
222 } else {
223 throw new ConfigurationException("Cannot locate the chosen ObjectFactory implementation: "+foundName);
224 }
225 }
226 }
227 }
228 } else {
229 LOG.warn("Unable to alias bean type "+type+", default mapping already assigned.");
230 }
231 }
232
233 class ObjectFactoryDelegateFactory implements Factory {
234 String name;
235 Class type;
236 ObjectFactoryDelegateFactory(String name, Class type) {
237 this.name = name;
238 this.type = type;
239 }
240
241 public Object create(Context context) throws Exception {
242 ObjectFactory objFactory = context.getContainer().getInstance(ObjectFactory.class);
243 try {
244 return objFactory.buildBean(name, null, true);
245 } catch (ClassNotFoundException ex) {
246 throw new ConfigurationException("Unable to load bean "+type.getName()+" ("+name+")");
247 }
248 }
249 }
250 }