001: /*
002: * $Id: AbstractCreateAction.java 510851 2007-02-23 07:05:18Z pbenedict $
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.Action;
026: import org.apache.struts.chain.contexts.ActionContext;
027: import org.apache.struts.config.ActionConfig;
028:
029: /**
030: * <p> Create (if necessary) and cache an <code>Action</code> for this
031: * request. </p>
032: *
033: * @version $Rev: 510851 $ $Date: 2005-11-12 13:01:44 -0500 (Sat, 12 Nov 2005)
034: * $
035: */
036: public abstract class AbstractCreateAction extends ActionCommandBase {
037: // ------------------------------------------------------ Instance Variables
038:
039: /**
040: * Provide a Commons logging instance for this class.
041: */
042: private static final Log LOG = LogFactory
043: .getLog(AbstractCreateAction.class);
044:
045: // ---------------------------------------------------------- Public Methods
046:
047: /**
048: * <p>Create (if necessary) and cache an <code>Action</code> for this
049: * request.</p>
050: *
051: * @param actionCtx The <code>Context</code> for the current request
052: * @return <code>false</code> so that processing continues
053: * @throws Exception if there are any problems instantiating the Action
054: * class.
055: */
056: public boolean execute(ActionContext actionCtx) throws Exception {
057: // Skip processing if the current request is not valid
058: Boolean valid = actionCtx.getFormValid();
059:
060: if ((valid == null) || !valid.booleanValue()) {
061: LOG.trace("Invalid form; not going to execute.");
062:
063: return (false);
064: }
065:
066: // Check to see if an action has already been created
067: if (actionCtx.getAction() != null) {
068: LOG.trace("already have an action ["
069: + actionCtx.getAction() + "]");
070:
071: return (false);
072: }
073:
074: // Look up the class name for the desired Action
075: ActionConfig actionConfig = actionCtx.getActionConfig();
076: String type = actionConfig.getType();
077:
078: if (type == null) {
079: if ((actionConfig.getForward() == null)
080: && (actionConfig.getInclude() == null)) {
081: LOG.error("no type for " + actionConfig.getPath());
082: } else {
083: LOG.trace("no type for " + actionConfig.getPath());
084: }
085:
086: return (false);
087: }
088:
089: // Create (if necessary) and cache an Action instance
090: Action action = getAction(actionCtx, type, actionConfig);
091:
092: if (LOG.isTraceEnabled()) {
093: LOG.trace("setting action to " + action);
094: }
095:
096: actionCtx.setAction(action);
097:
098: return (false);
099: }
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,
118: String type, ActionConfig actionConfig) throws Exception;
119: }
|