View Javadoc

1   /*
2    * $Id: AbstractCreateAction.java 421119 2006-07-12 04:49:11Z wsmoak $
3    *
4    * Copyright 2000-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.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.struts.action.Action;
23  import org.apache.struts.chain.contexts.ActionContext;
24  import org.apache.struts.config.ActionConfig;
25  
26  /***
27   * <p> Create (if necessary) and cache an <code>Action</code> for this
28   * request. </p>
29   *
30   * @version $Rev: 421119 $ $Date: 2005-11-12 13:01:44 -0500 (Sat, 12 Nov 2005)
31   *          $
32   */
33  public abstract class AbstractCreateAction extends ActionCommandBase {
34      // ------------------------------------------------------ Instance Variables
35  
36      /***
37       * Provide a Commons logging instance for this class.
38       */
39      private static final Log LOG =
40          LogFactory.getLog(AbstractCreateAction.class);
41  
42      // ---------------------------------------------------------- Public Methods
43  
44      /***
45       * <p>Create (if necessary) and cache an <code>Action</code> for this
46       * request.</p>
47       *
48       * @param actionCtx The <code>Context</code> for the current request
49       * @return <code>false</code> so that processing continues
50       * @throws Exception if there are any problems instantiating the Action
51       *                   class.
52       */
53      public boolean execute(ActionContext actionCtx)
54          throws Exception {
55          // Skip processing if the current request is not valid
56          Boolean valid = actionCtx.getFormValid();
57  
58          if ((valid == null) || !valid.booleanValue()) {
59              LOG.trace("Invalid form; not going to execute.");
60  
61              return (false);
62          }
63  
64          // Check to see if an action has already been created
65          if (actionCtx.getAction() != null) {
66              LOG.trace("already have an action [" + actionCtx.getAction() + "]");
67  
68              return (false);
69          }
70  
71          // Look up the class name for the desired Action
72          ActionConfig actionConfig = actionCtx.getActionConfig();
73          String type = actionConfig.getType();
74  
75          if (type == null) {
76              LOG.trace("no type for " + actionConfig.getPath());
77  
78              return (false);
79          }
80  
81          // Create (if necessary) and cache an Action instance
82          Action action = getAction(actionCtx, type, actionConfig);
83  
84          if (LOG.isTraceEnabled()) {
85              LOG.trace("setting action to " + action);
86          }
87  
88          actionCtx.setAction(action);
89  
90          return (false);
91      }
92  
93      // ------------------------------------------------------- Protected Methods
94  
95      /***
96       * <p> Create and return the appropriate <code>Action</code> class for the
97       * given <code>type</code> and <code>actionConfig</code>. </p> <p> NOTE:
98       * The dependence on ActionServlet suggests that this should be broken up
99       * along the lines of the other Abstract/concrete pairs in the
100      * org.apache.struts.chain.commands package. </p>
101      *
102      * @param context      The <code>Context</code> for this request
103      * @param type         Name of class to instantiate
104      * @param actionConfig The {@link ActionConfig} for this request
105      * @return Instantiated Action class
106      * @throws Exception if there are any problems instantiating the Action
107      *                   class.
108      */
109     protected abstract Action getAction(ActionContext context, String type,
110         ActionConfig actionConfig)
111         throws Exception;
112 }