View Javadoc

    1   /*
    2    * $Id: BeanSelectionProvider.java 499704 2007-01-25 08:57:59Z mrdon $
    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.StrutsException;
    30  import org.apache.struts2.dispatcher.mapper.ActionMapper;
    31  import org.apache.struts2.dispatcher.multipart.MultiPartRequest;
    32  import org.apache.struts2.views.freemarker.FreemarkerManager;
    33  import org.apache.struts2.views.velocity.VelocityManager;
    34
    35  import com.opensymphony.xwork2.ActionProxyFactory;
    36  import com.opensymphony.xwork2.ObjectFactory;
    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.Inject;
    45  import com.opensymphony.xwork2.inject.Scope;
    46  import com.opensymphony.xwork2.util.ClassLoaderUtil;
    47  import com.opensymphony.xwork2.util.LocalizedTextUtil;
    48  import com.opensymphony.xwork2.util.ObjectTypeDeterminer;
    49  import com.opensymphony.xwork2.util.ObjectTypeDeterminerFactory;
    50  import com.opensymphony.xwork2.util.XWorkConverter;
    51  import com.opensymphony.xwork2.util.location.LocatableProperties;
    52
    53  /***
    54   * Selects the implementations of key framework extension points, using the loaded
    55   * property constants.  The implementations are selected from the container builder
    56   * using the name defined in its associated property.  The default implementation name will
    57   * always be "struts".
    58   *
    59   * <p>
    60   * The following is a list of the allowed extension points:
    61   *
    62   * <!-- START SNIPPET: extensionPoints -->
    63   * <table border="1">
    64   *   <tr>
    65   *     <th>Type</th>
    66   *     <th>Property</th>
    67   *     <th>Scope</th>
    68   *     <th>Description</th>
    69   *   </tr>
    70   *   <tr>
    71   *     <td>com.opensymphony.xwork2.ObjectFactory</td>
    72   *     <td>struts.objectFactory</td>
    73   *     <td>singleton</td>
    74   *     <td>Creates actions, results, and interceptors</td>
    75   *   </tr>
    76   *   <tr>
    77   *     <td>com.opensymphony.xwork2.ActionProxyFactory</td>
    78   *     <td>struts.actionProxyFactory</td>
    79   *     <td>singleton</td>
    80   *     <td>Creates the ActionProxy</td>
    81   *   </tr>
    82   *   <tr>
    83   *     <td>com.opensymphony.xwork2.util.ObjectTypeDeterminer</td>
    84   *     <td>struts.objectTypeDeterminer</td>
    85   *     <td>singleton</td>
    86   *     <td>Determines what the key and and element class of a Map or Collection should be</td>
    87   *   </tr>
    88   *   <tr>
    89   *     <td>org.apache.struts2.dispatcher.mapper.ActionMapper</td>
    90   *     <td>struts.mapper.class</td>
    91   *     <td>singleton</td>
    92   *     <td>Determines the ActionMapping from a request and a URI from an ActionMapping</td>
    93   *   </tr>
    94   *   <tr>
    95   *     <td>org.apache.struts2.dispatcher.multipart.MultiPartRequest</td>
    96   *     <td>struts.multipart.parser</td>
    97   *     <td>per request</td>
    98   *     <td>Parses a multipart request (file upload)</td>
    99   *   </tr>
    100  *   <tr>
    101  *     <td>org.apache.struts2.views.freemarker.FreemarkerManager</td>
    102  *     <td>struts.freemarker.manager.classname</td>
    103  *     <td>singleton</td>
    104  *     <td>Loads and processes Freemarker templates</td>
    105  *   </tr>
    106  *   <tr>
    107  *     <td>org.apache.struts2.views.velocity.VelocityManager</td>
    108  *     <td>struts.velocity.manager.classname</td>
    109  *     <td>singleton</td>
    110  *     <td>Loads and processes Velocity templates</td>
    111  *   </tr>
    112  * </table>
    113  *
    114  * <!-- END SNIPPET: extensionPoints -->
    115  * </p>
    116  * <p>
    117  * Implementations are selected using the value of its associated property.  That property is
    118  * used to determine the implementation by:
    119  * </p>
    120  * <ol>
    121  *   <li>Trying to find an existing bean by that name in the container</li>
    122  *   <li>Trying to find a class by that name, then creating a new bean factory for it</li>
    123  *   <li>Creating a new delegation bean factory that delegates to the configured ObjectFactory at runtime</li>
    124  * </ol>
    125  * <p>
    126  * Finally, this class overrides certain properties if dev mode is enabled:
    127  * </p>
    128  * <ul>
    129  *   <li><code>struts.i18n.reload = true</code></li>
    130  *   <li><code>struts.configuration.xml.reload = true</code></li>
    131  * </ul>
    132  */
    133 public class BeanSelectionProvider implements ConfigurationProvider {
    134     public static final String DEFAULT_BEAN_NAME = "struts";
    135     private static final Log LOG = LogFactory.getLog(BeanSelectionProvider.class);
    136
    137     public void destroy() {
    138         // NO-OP
    139     }
    140
    141     public void loadPackages() throws ConfigurationException {
    142         // NO-OP
    143     }
    144
    145     public void init(Configuration configuration) throws ConfigurationException {
    146         // NO-OP
    147
    148     }
    149
    150     public boolean needsReload() {
    151         return false;
    152     }
    153
    154     public void register(ContainerBuilder builder, LocatableProperties props) {
    155         alias(ObjectFactory.class, StrutsConstants.STRUTS_OBJECTFACTORY, builder, props);
    156         alias(XWorkConverter.class, StrutsConstants.STRUTS_XWORKCONVERTER, builder, props);
    157         alias(ActionProxyFactory.class, StrutsConstants.STRUTS_ACTIONPROXYFACTORY, builder, props);
    158         alias(ObjectTypeDeterminer.class, StrutsConstants.STRUTS_OBJECTTYPEDETERMINER, builder, props);
    159         alias(ActionMapper.class, StrutsConstants.STRUTS_MAPPER_CLASS, builder, props);
    160         alias(MultiPartRequest.class, StrutsConstants.STRUTS_MULTIPART_PARSER, builder, props, Scope.DEFAULT);
    161         alias(FreemarkerManager.class, StrutsConstants.STRUTS_FREEMARKER_MANAGER_CLASSNAME, builder, props);
    162         alias(VelocityManager.class, StrutsConstants.STRUTS_VELOCITY_MANAGER_CLASSNAME, builder, props);
    163
    164         if ("true".equalsIgnoreCase(props.getProperty(StrutsConstants.STRUTS_DEVMODE))) {
    165             props.setProperty(StrutsConstants.STRUTS_I18N_RELOAD, "true");
    166             props.setProperty(StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD, "true");
    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 }