001: /*
002: * $Id: ExecuteCommand.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.chain.Catalog;
024: import org.apache.commons.chain.CatalogFactory;
025: import org.apache.commons.chain.Command;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.struts.chain.contexts.ActionContext;
029: import org.apache.struts.config.ActionConfig;
030:
031: /**
032: * <p>Invoke the appropriate <code>Command</code> for this request. If the
033: * context's <code>ActionConfig</code> has no <code>command</code> property
034: * defined, no action will be taken. If the specified command cannot be
035: * found, a warning will be logged, but processing will continue. Depending
036: * on how the chain is configured, this can be used in place of an
037: * <code>Action</code> or as a method of performing pre-processing. </p>
038: *
039: * <p>If used instead of an action, the command which is looked up should put
040: * an ActionForward into the context, unless it has already dealt with the
041: * response.</p>
042: *
043: * @version $Id: ExecuteCommand.java 471754 2006-11-06 14:55:09Z husted $
044: */
045: public class ExecuteCommand extends ActionCommandBase {
046: // ------------------------------------------------------ Instance Variables
047:
048: /**
049: * Provide Commons Logging instance for this class.
050: */
051: private static final Log LOG = LogFactory
052: .getLog(ExecuteCommand.class);
053:
054: // ---------------------------------------------------------- Public Methods
055:
056: /**
057: * <p>If the <code>context</code> is "valid", lookup a command and execute
058: * it.</p>
059: *
060: * @param actionCtx The <code>Context</code> for the current request
061: * @return the result of the lookup command's <code>execute</code> method,
062: * if executed, or <code>false</code> if it was not executed.
063: * @throws Exception on any error
064: */
065: public boolean execute(ActionContext actionCtx) throws Exception {
066: if (shouldProcess(actionCtx)) {
067: Command command = getCommand(actionCtx);
068:
069: if (command != null) {
070: return (command.execute(actionCtx));
071: }
072: }
073:
074: return (false);
075: }
076:
077: /**
078: * <p>Evaluate the current context to see if a command should even be
079: * executed.</p>
080: *
081: * @param context A valid ActionContext
082: * @return TRUE if the pending Command should be executed
083: */
084: protected boolean shouldProcess(ActionContext context) {
085: // Skip processing if the current request is not valid
086: Boolean valid = context.getFormValid();
087:
088: return ((valid != null) && valid.booleanValue());
089: }
090:
091: /**
092: * <p>Find the <code>ActionConfig</code> in the current context and, if it
093: * is properly configured, lookup the appropriate <code>commons-chain</code>
094: * command.</p>
095: *
096: * @param context A valid ActionContext
097: * @return a <code>Command</code> to execute, or null if none is specified
098: * or if the specified command cannot be found.
099: */
100: protected Command getCommand(ActionContext context) {
101: ActionConfig actionConfig = context.getActionConfig();
102:
103: String commandName = actionConfig.getCommand();
104:
105: if (commandName == null) {
106: return null;
107: }
108:
109: String catalogName = actionConfig.getCatalog();
110:
111: return getCommand(commandName, catalogName);
112: }
113:
114: /**
115: * <p> Retrieve the specified Command from the specified Catalog. </p>
116: *
117: * @param commandName The Command to retrieve.
118: * @param catalogName The Catalog to search.
119: * @return Instantiated Command, or null
120: */
121: protected Command getCommand(String commandName, String catalogName) {
122: if (commandName == null) {
123: return null;
124: }
125:
126: Catalog catalog;
127:
128: if (catalogName != null) {
129: catalog = CatalogFactory.getInstance().getCatalog(
130: catalogName);
131:
132: if (catalog == null) {
133: LOG.warn("When looking up " + commandName + ","
134: + " no catalog found under " + catalogName);
135:
136: return null;
137: }
138: } else {
139: catalogName = "the default catalog";
140: catalog = CatalogFactory.getInstance().getCatalog();
141:
142: if (catalog == null) {
143: LOG.warn("When looking up " + commandName + ","
144: + " no default catalog found.");
145:
146: return null;
147: }
148: }
149:
150: if (LOG.isDebugEnabled()) {
151: LOG.debug("looking up command " + commandName + " in "
152: + catalogName);
153: }
154:
155: return catalog.getCommand(commandName);
156: }
157: }
|