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 java.util.Map;
24  
25  import org.apache.struts.action.ActionForm;
26  import org.apache.struts.chain.contexts.ActionContext;
27  import org.apache.struts.chain.contexts.ServletActionContext;
28  import org.apache.struts.config.ActionConfig;
29  import org.apache.struts.config.FormBeanConfig;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * <p>Create (if necessary) and cache a form bean for this request.</p>
35   *
36   * @version $Id$
37   */
38  public class CreateActionForm extends ActionCommandBase {
39      // ------------------------------------------------------ Instance Variables
40  
41      /**
42       * The {@code Log} instance for this class.
43       */
44      private final Logger log =
45          LoggerFactory.getLogger(CreateActionForm.class);
46  
47      // ---------------------------------------------------------- Public Methods
48  
49      /**
50       * <p>Create (if necessary) and cache a form bean for this request.</p>
51       *
52       * @param actionCtx The <code>Context</code> for the current request
53       * @return <code>false</code> so that processing continues
54       * @throws Exception on any error
55       */
56      @Override
57      protected boolean execute_(ActionContext actionCtx)
58          throws Exception {
59          // Is there a form bean associated with this ActionConfig?
60          ActionConfig actionConfig = actionCtx.getActionConfig();
61          String name = actionConfig.getName();
62  
63          if (name == null) {
64              actionCtx.setActionForm(null);
65              return CONTINUE_PROCESSING;
66          }
67  
68          log.trace("Look up form-bean {}", name);
69  
70          // Look up the corresponding FormBeanConfig (if any)
71          FormBeanConfig formBeanConfig =
72              actionConfig.getModuleConfig().findFormBeanConfig(name);
73  
74          if (formBeanConfig == null) {
75              log.warn("No FormBeanConfig found in module {} under name {}",
76                  actionConfig.getModuleConfig().getPrefix(), name);
77              actionCtx.setActionForm(null);
78              return CONTINUE_PROCESSING;
79          }
80  
81          Map<String, Object> scope = actionCtx.getScope(actionConfig.getScope());
82  
83          ActionForm instance;
84  
85          instance = (ActionForm) scope.get(actionConfig.getAttribute());
86  
87          // Can we recycle the existing instance (if any)?
88          if (!formBeanConfig.canReuse(instance)) {
89              instance = formBeanConfig.createActionForm(actionCtx);
90          }
91  
92          // TODO: Remove ServletActionContext when ActionForm no longer
93          //  directly depends on ActionServlet
94          if (actionCtx instanceof ServletActionContext) {
95              // The servlet property of ActionForm is transient, so
96              // ActionForms which are restored from a serialized state
97              // need to have their servlet restored.
98              ServletActionContext sac = (ServletActionContext) actionCtx;
99  
100             instance.setServlet(sac.getActionServlet());
101         }
102 
103         actionCtx.setActionForm(instance);
104 
105         scope.put(actionConfig.getAttribute(), instance);
106 
107         return CONTINUE_PROCESSING;
108     }
109 }