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.chain.contexts.ActionContext;
24  
25  /**
26   * This abstract class is a template for choosing the target method that is
27   * identified by a submission parameter.
28   *
29   * @version $Rev$
30   * @since Struts 1.4
31   */
32  public abstract class AbstractParameterDispatcher extends AbstractDispatcher {
33      private static final long serialVersionUID = -9048942314641198669L;
34  
35      /**
36       * The default parameter name if no parameter is set.
37       *
38       * @see #getDefaultParameterName()
39       */
40      protected static final String DEFAULT_PARAMETER_NAME = "method";
41  
42      /**
43       * Constructs a new dispatcher with the specified method resolver.
44       *
45       * @param methodResolver the method resolver
46       */
47      public AbstractParameterDispatcher(MethodResolver methodResolver) {
48          super(methodResolver);
49      }
50  
51      /**
52       * Retrieves the name of the parameter to fallback upon if the action
53       * configuration did not set the <code>parameter</code> attribute. The
54       * default implementation returns {@link #DEFAULT_PARAMETER_NAME}.
55       *
56       * @return the fallback parameter name; can be <code>null</code>
57       * @see #DEFAULT_PARAMETER_NAME
58       * @see #getParameter(ActionContext)
59       */
60      protected String getDefaultParameterName() {
61          return DEFAULT_PARAMETER_NAME;
62      }
63  
64      /**
65       * Retrieves the parameter name whose value will identity the intended
66       * method to dispatch. The value is not necessarily a method name, but a key
67       * that can resolve to one. The default implementation returns the mapping's
68       * <code>parameter</code> attribute; otherwise fallback to the default
69       * parameter name.
70       *
71       * @param context the current action context
72       * @return the mapping's parameter name
73       * @see #getDefaultParameterName()
74       */
75      protected String getParameter(ActionContext context) {
76          String parameter = context.getActionConfig().getParameter();
77          if ((parameter != null) && (parameter.trim().length() > 0)) {
78              return parameter;
79          }
80          return getDefaultParameterName();
81      }
82  
83      protected final String resolveMethodName(ActionContext context) {
84          String parameter = getParameter(context);
85          if ("".equals(parameter)) {
86              parameter = null;
87          }
88  
89          if (parameter != null) {
90              return resolveParameterValue(context, parameter);
91          }
92  
93          return null;
94      }
95  
96      /**
97       * Extracts the value that is keyed by the specified parameter.
98       *
99       * @param context the current action context
100      * @param parameter the parameter name
101      * @return the parameter value
102      */
103     protected abstract String resolveParameterValue(ActionContext context, String parameter);
104 
105 }