View Javadoc

1   /*
2    * $Id: AbstractSelectAction.java 421119 2006-07-12 04:49:11Z wsmoak $
3    *
4    * Copyright 2003-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.chain.commands;
19  
20  import org.apache.struts.chain.contexts.ActionContext;
21  import org.apache.struts.config.ActionConfig;
22  import org.apache.struts.config.ModuleConfig;
23  
24  /***
25   * <p>Cache the <code>ActionConfig</code> instance for the action to be used
26   * for processing this request.</p>
27   *
28   * @version $Rev: 421119 $ $Date: 2005-11-05 21:44:59 -0500 (Sat, 05 Nov 2005)
29   *          $
30   */
31  public abstract class AbstractSelectAction extends ActionCommandBase {
32      // ---------------------------------------------------------- Public Methods
33  
34      /***
35       * <p>Cache the <code>ActionConfig</code> instance for the action to be
36       * used for processing this request.</p>
37       *
38       * @param actionCtx The <code>Context</code> for the current request
39       * @return <code>false</code> so that processing continues
40       * @throws InvalidPathException if no valid action can be identified for
41       *                              this request
42       * @throws Exception            if thrown by the Action class
43       */
44      public boolean execute(ActionContext actionCtx)
45          throws Exception {
46          // Identify the matching path for this request
47          String path = getPath(actionCtx);
48  
49          // Cache the corresponding ActonConfig instance
50          ModuleConfig moduleConfig = actionCtx.getModuleConfig();
51          ActionConfig actionConfig = moduleConfig.findActionConfig(path);
52  
53          if (actionConfig == null) {
54              // NOTE Shouldn't this be the responsibility of ModuleConfig?
55              // Locate the mapping for unknown paths (if any)
56              ActionConfig[] configs = moduleConfig.findActionConfigs();
57  
58              for (int i = 0; i < configs.length; i++) {
59                  if (configs[i].getUnknown()) {
60                      actionConfig = configs[i];
61  
62                      break;
63                  }
64              }
65          }
66  
67          if (actionConfig == null) {
68              throw new InvalidPathException("No action config found for the specified url.",
69                  path);
70          }
71  
72          actionCtx.setActionConfig(actionConfig);
73  
74          return (false);
75      }
76  
77      // ------------------------------------------------------- Protected Methods
78  
79      /***
80       * <p>Return the path to be used to select the <code>ActionConfig</code>
81       * for this request.</p>
82       *
83       * @param context The <code>Context</code> for this request
84       * @return Path to be used to select the ActionConfig
85       */
86      protected abstract String getPath(ActionContext context);
87  }