View Javadoc
1   /*
2    * $Id$
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.struts.dispatcher;
22  
23  import org.apache.struts.action.ActionMapping;
24  import org.apache.struts.chain.contexts.ActionContext;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  /**
29   * This abstract class is a template for choosing the target method that is
30   * named by the <code>parameter</code> attribute of the corresponding
31   * {@link ActionMapping}. The attribute value, if not provided, defaults to
32   * <code>execute</code>.
33   *
34   * @version $Rev$
35   * @since Struts 1.4
36   */
37  public abstract class AbstractMappingDispatcher extends AbstractDispatcher {
38      private static final long serialVersionUID = 1882936058355907522L;
39  
40      /**
41       * The {@code Log} instance for this class.
42       */
43      private transient final Logger log =
44              LoggerFactory.getLogger(AbstractMappingDispatcher.class);
45  
46      private String defaultMappingParameter;
47  
48      /**
49       * Constructs a new dispatcher with the specified method resolver.
50       *
51       * @param methodResolver the method resolver
52       */
53      public AbstractMappingDispatcher(MethodResolver methodResolver) {
54          super(methodResolver);
55          setDefaultMappingParameter(EXECUTE_METHOD_NAME);
56      }
57  
58      /**
59       * Retrieves the default mapping parameter value. This value is used by
60       * {@link #resolveMethodName(ActionContext)} if the mapping did not provide
61       * a value.
62       *
63       * @return the mapping parameter value or <code>null</code>
64       * @see #setDefaultMappingParameter(String)
65       */
66      protected final String getDefaultMappingParameter() {
67          return defaultMappingParameter;
68      }
69  
70      /**
71       * Resolves the method name by obtaining the <code>parameter</code>
72       * attribute from the {@link ActionMapping}.
73       *
74       * @param context the current action context
75       * @throws IllegalStateException if the parameter cannot be resolved
76       * @return the parameter attribute value
77       */
78      protected String resolveMethodName(ActionContext context) {
79          // Null out an empty string parameter
80          ActionMapping mapping = (ActionMapping) context.getActionConfig();
81          String parameter = mapping.getParameter();
82          if ("".equals(parameter)) {
83              parameter = null;
84          }
85  
86          // Assign the default if the mapping did not provide a value
87          if (parameter == null) {
88              parameter = defaultMappingParameter;
89          }
90  
91          // Parameter is required
92          if (parameter == null) {
93              String message = messages.getMessage(MSG_KEY_MISSING_MAPPING_PARAMETER, mapping.getPath());
94              log.error(message);
95              throw new IllegalStateException(message);
96          }
97  
98          return parameter;
99      }
100 
101     /**
102      * Stores the new default mapping parameter value.
103      *
104      * @param defaultMappingParameter the parameter value
105      * @see #getDefaultMappingParameter()
106      */
107     protected final void setDefaultMappingParameter(String defaultMappingParameter) {
108         this.defaultMappingParameter = defaultMappingParameter;
109     }
110 }