View Javadoc

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