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.chain.commands;
22  
23  import org.apache.struts.action.Action;
24  import org.apache.struts.chain.contexts.ActionContext;
25  import org.apache.struts.config.ActionConfig;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  /**
30   * <p> Create (if necessary) and cache an <code>Action</code> for this
31   * request. </p>
32   *
33   * @version $Rev$ $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       * The {@code Log} instance for this class.
41       */
42      private final Logger log =
43          LoggerFactory.getLogger(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      @Override
57      protected boolean execute_(ActionContext actionCtx)
58          throws Exception {
59          // Skip processing if the current request is not valid
60          Boolean valid = actionCtx.getFormValid();
61  
62          if ((valid == null) || !valid.booleanValue()) {
63              log.trace("Invalid form; not going to execute.");
64  
65              return CONTINUE_PROCESSING;
66          }
67  
68          // Check to see if an action has already been created
69          if (actionCtx.getAction() != null) {
70              log.trace("already have an action [{}]", actionCtx.getAction());
71  
72              return CONTINUE_PROCESSING;
73          }
74  
75          // Look up the class name for the desired Action
76          ActionConfig actionConfig = actionCtx.getActionConfig();
77          String type = actionConfig.getType();
78  
79          if (type == null) {
80              String command = actionConfig.getCommand();
81              if ((command == null) && (actionConfig.getForward() == null)
82                  && (actionConfig.getInclude() == null)) {
83                  log.error("no type or command for {}", actionConfig.getPath());
84              } else {
85                  log.trace("no type for {}", actionConfig.getPath());
86              }
87  
88              return CONTINUE_PROCESSING;
89          }
90  
91          // Create (if necessary) and cache an Action instance
92          Action action = getAction(actionCtx, type, actionConfig);
93  
94          log.trace("setting action to {}", action);
95  
96          actionCtx.setAction(action);
97  
98          return CONTINUE_PROCESSING;
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 }