001: /*
002: * $Id: CreateActionForm.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.chain.commands;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.struts.action.ActionForm;
026: import org.apache.struts.chain.contexts.ActionContext;
027: import org.apache.struts.chain.contexts.ServletActionContext;
028: import org.apache.struts.config.ActionConfig;
029: import org.apache.struts.config.FormBeanConfig;
030:
031: import java.util.Map;
032:
033: /**
034: * <p>Create (if necessary) and cache a form bean for this request.</p>
035: *
036: * @version $Id: CreateActionForm.java 471754 2006-11-06 14:55:09Z husted $
037: */
038: public class CreateActionForm extends ActionCommandBase {
039: // ------------------------------------------------------ Instance Variables
040:
041: /**
042: * <p> Provide Commons Logging instance for this class. </p>
043: */
044: private static final Log LOG = LogFactory
045: .getLog(CreateActionForm.class);
046:
047: // ---------------------------------------------------------- Public Methods
048:
049: /**
050: * <p>Create (if necessary) and cache a form bean for this request.</p>
051: *
052: * @param actionCtx The <code>Context</code> for the current request
053: * @return <code>false</code> so that processing continues
054: * @throws Exception on any error
055: */
056: public boolean execute(ActionContext actionCtx) throws Exception {
057: // Is there a form bean associated with this ActionConfig?
058: ActionConfig actionConfig = actionCtx.getActionConfig();
059: String name = actionConfig.getName();
060:
061: if (name == null) {
062: actionCtx.setActionForm(null);
063:
064: return (false);
065: }
066:
067: if (LOG.isTraceEnabled()) {
068: LOG.trace("Look up form-bean " + name);
069: }
070:
071: // Look up the corresponding FormBeanConfig (if any)
072: FormBeanConfig formBeanConfig = actionConfig.getModuleConfig()
073: .findFormBeanConfig(name);
074:
075: if (formBeanConfig == null) {
076: LOG.warn("No FormBeanConfig found in module "
077: + actionConfig.getModuleConfig().getPrefix()
078: + " under name " + name);
079: actionCtx.setActionForm(null);
080:
081: return (false);
082: }
083:
084: Map scope = actionCtx.getScope(actionConfig.getScope());
085:
086: ActionForm instance;
087:
088: instance = (ActionForm) scope.get(actionConfig.getAttribute());
089:
090: // Can we recycle the existing instance (if any)?
091: if (!formBeanConfig.canReuse(instance)) {
092: instance = formBeanConfig.createActionForm(actionCtx);
093: }
094:
095: // TODO: Remove ServletActionContext when ActionForm no longer
096: // directly depends on ActionServlet
097: if (actionCtx instanceof ServletActionContext) {
098: // The servlet property of ActionForm is transient, so
099: // ActionForms which are restored from a serialized state
100: // need to have their servlet restored.
101: ServletActionContext sac = (ServletActionContext) actionCtx;
102:
103: instance.setServlet(sac.getActionServlet());
104: }
105:
106: actionCtx.setActionForm(instance);
107:
108: scope.put(actionConfig.getAttribute(), instance);
109:
110: return (false);
111: }
112: }
|