View Javadoc

1   /*
2    * $Id: CreateActionForm.java 376300 2006-02-09 14:11:07Z husted $
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.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.struts.action.ActionForm;
23  import org.apache.struts.chain.contexts.ActionContext;
24  import org.apache.struts.chain.contexts.ServletActionContext;
25  import org.apache.struts.config.ActionConfig;
26  import org.apache.struts.config.FormBeanConfig;
27  
28  import java.util.Map;
29  
30  /***
31   * <p>Create (if necessary) and cache a form bean for this request.</p>
32   *
33   * @version $Id: CreateActionForm.java 376300 2006-02-09 14:11:07Z husted $
34   */
35  public class CreateActionForm extends ActionCommandBase {
36      // ------------------------------------------------------ Instance Variables
37  
38      /***
39       * <p> Provide Commons Logging instance for this class. </p>
40       */
41      private static final Log LOG = LogFactory.getLog(CreateActionForm.class);
42  
43      // ---------------------------------------------------------- Public Methods
44  
45      /***
46       * <p>Create (if necessary) and cache a form bean for this 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 on any error
51       */
52      public boolean execute(ActionContext actionCtx)
53          throws Exception {
54          // Is there a form bean associated with this ActionConfig?
55          ActionConfig actionConfig = actionCtx.getActionConfig();
56          String name = actionConfig.getName();
57  
58          if (name == null) {
59              actionCtx.setActionForm(null);
60  
61              return (false);
62          }
63  
64          if (LOG.isTraceEnabled()) {
65              LOG.trace("Look up form-bean " + name);
66          }
67  
68          // Look up the corresponding FormBeanConfig (if any)
69          FormBeanConfig formBeanConfig =
70              actionConfig.getModuleConfig().findFormBeanConfig(name);
71  
72          if (formBeanConfig == null) {
73              LOG.warn("No FormBeanConfig found in module "
74                  + actionConfig.getModuleConfig().getPrefix() + " under name "
75                  + name);
76              actionCtx.setActionForm(null);
77  
78              return (false);
79          }
80  
81          Map 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 (false);
108     }
109 }