craigmcc 2003/08/31 15:42:45
Modified: contrib/struts-chain/src/conf chain-config.xml
contrib/struts-chain/src/java/org/apache/struts/chain
AbstractValidateActionForm.java
contrib/struts-chain/src/java/org/apache/struts/chain/servlet
ValidateActionForm.java
Added: contrib/struts-chain/src/java/org/apache/struts/chain
AbstractSelectInput.java
contrib/struts-chain/src/java/org/apache/struts/chain/servlet
SelectInput.java
Log:
Use the "delegate to a named command" design pattern for validation failures
like we do for exception handling, instead of a conditionally executed
child chain.
Revision Changes Path
1.4 +29 -12 jakarta-struts/contrib/struts-chain/src/conf/chain-config.xml
Index: chain-config.xml
===================================================================
RCS file: /home/cvs/jakarta-struts/contrib/struts-chain/src/conf/chain-config.xml,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- chain-config.xml 31 Aug 2003 21:53:00 -0000 1.3
+++ chain-config.xml 31 Aug 2003 22:42:45 -0000 1.4
@@ -84,7 +84,7 @@
processPopulate PopulateActionForm
- processValidate ValidateActionForm
+ processValidate ValidateActionForm / SelectInput
processForward NOT SUPPORTED YET
@@ -134,14 +134,9 @@
<!-- Validate the ActionForm for this request -->
- <chain
- className="org.apache.struts.chain.servlet.ValidateActionForm">
-
- <!-- Nested chain executed on validation failure only -->
- <command
- className="org.apache.struts.chain.servlet.PerformForward"/>
-
- </chain>
+ <command
+ className="org.apache.struts.chain.servlet.ValidateActionForm"
+ failureCommand="servlet-validation-failure"/>
<!-- Create (if needed) the Action for this request -->
@@ -154,7 +149,7 @@
className="org.apache.struts.chain.servlet.ExecuteAction"/>
- <!-- Follow the returned ActionForward (if any) -->
+ <!-- Follow the returned ForwardConfig (if any) -->
<command
className="org.apache.struts.chain.servlet.PerformForward"/>
@@ -179,7 +174,29 @@
<command
className="org.apache.struts.chain.servlet.ExceptionHandler"/>
- <!-- Follow the returned ActionForward (if any) -->
+ <!-- Follow the returned ForwardConfig (if any) -->
+ <command
+ className="org.apache.struts.chain.servlet.PerformForward"/>
+
+ </chain>
+
+
+ <!-- ========== Servlet Validation Failure Chain ======================= -->
+
+ <chain name="servlet-validation-failure">
+
+ <!--
+ This chain is designed to be invoked (by o.a.s.c.ValidateActionForm)
+ if a validation failure occurs. The standard behavior is to obey
+ the "input" property of the current ActionConfig, and forward back
+ to the input page.
+ -->
+
+ <!-- Select the appropriate ForwardConfig for return to input page -->
+ <command
+ className="org.apache.struts.chain.servlet.SelectInput"/>
+
+ <!-- Follow the returned ForwardConfig (if any) -->
<command
className="org.apache.struts.chain.servlet.PerformForward"/>
1.2 +60 -47
jakarta-struts/contrib/struts-chain/src/java/org/apache/struts/chain/AbstractValidateActionForm.java
Index: AbstractValidateActionForm.java
===================================================================
RCS file:
/home/cvs/jakarta-struts/contrib/struts-chain/src/java/org/apache/struts/chain/AbstractValidateActionForm.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- AbstractValidateActionForm.java 11 Aug 2003 04:55:34 -0000 1.1
+++ AbstractValidateActionForm.java 31 Aug 2003 22:42:45 -0000 1.2
@@ -62,33 +62,31 @@
package org.apache.struts.chain;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import org.apache.commons.beanutils.BeanUtils;
+import org.apache.commons.chain.Catalog;
+import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.apache.commons.chain.impl.ChainBase;
import org.apache.commons.chain.web.WebContext;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.chain.Constants;
import org.apache.struts.config.ActionConfig;
-import org.apache.struts.config.ForwardConfig;
-import org.apache.struts.config.ModuleConfig;
/**
* <p>Validate the properties of the form bean for this request. If there are
- * any validation errors, execute the child commands in our chain; otherwise,
+ * any validation errors, execute the specified command; otherwise,
* proceed normally.</p>
*
* @author Craig R. McClanahan
* @version $Revision$ $Date$
*/
-public abstract class AbstractValidateActionForm extends ChainBase {
+public abstract class AbstractValidateActionForm implements Command {
// ------------------------------------------------------ Instance Variables
@@ -97,7 +95,11 @@
private String actionConfigKey = Constants.ACTION_CONFIG_KEY;
private String actionFormKey = Constants.ACTION_FORM_KEY;
private String cancelKey = Constants.CANCEL_KEY;
- private String forwardConfigKey = Constants.FORWARD_CONFIG_KEY;
+ private String catalogKey = Constants.CATALOG_KEY;
+ private String failureCommand = null;
+
+ private static final Log log =
+ LogFactory.getLog(AbstractValidateActionForm.class);
// -------------------------------------------------------------- Properties
@@ -181,26 +183,48 @@
/**
* <p>Return the context attribute key under which the
- * <code>ForwardConfig</code> for the currently selected application
- * action is stored.</p>
+ * <code>Catalog</code> we perform lookups in is stored.</p>
*/
- public String getForwardConfigKey() {
+ public String getCatalogKey() {
- return (this.forwardConfigKey);
+ return (this.catalogKey);
}
/**
* <p>Set the context attribute key under which the
- * <code>ForwardConfig</code> for the currently selected application
- * action is stored.</p>
+ * <code>Catalog</code> we perform lookups in is stored.</p>
*
- * @param forwardConfigKey The new context attribute key
+ * @param catalogKey The new context attribute key
+ */
+ public void setCatalogKey(String catalogKey) {
+
+ this.catalogKey = catalogKey;
+
+ }
+
+
+ /**
+ * <p>Return the name of the command to be executed
+ * if a validation failure occurs.</p>
*/
- public void setForwardConfigKey(String forwardConfigKey) {
+ public String getFailureCommand() {
- this.forwardConfigKey = forwardConfigKey;
+ return (this.failureCommand);
+
+ }
+
+
+ /**
+ * <p>Set the name of the command to be executed
+ * if a validation failure occurs.</p>
+ *
+ * @param failureCommand The name of the chain to be executed
+ */
+ public void setFailureCommand(String failureCommand) {
+
+ this.failureCommand = failureCommand;
}
@@ -249,38 +273,27 @@
return (false);
}
- // Cache an ForwardConfig back to our input page
- ForwardConfig forwardConfig = null;
- String input = actionConfig.getInput();
- ModuleConfig moduleConfig = actionConfig.getModuleConfig();
- if (moduleConfig.getControllerConfig().getInputForward()) {
- forwardConfig = actionConfig.findForwardConfig(input);
- } else {
- forwardConfig = forward(context, moduleConfig, input);
+ // Execute the specified validation failure command
+ try {
+ Catalog catalog = (Catalog)
+ context.getAttributes().get(getCatalogKey());
+ Command command = catalog.getCommand(getFailureCommand());
+ if (log.isTraceEnabled()) {
+ log.trace("Calling failure command '" + getFailureCommand()
+ + "'");
+ }
+ command.execute(context);
+ } catch (Exception e) {
+ log.warn("Exception from failure command '" +
+ getFailureCommand() + "'", e);
+ throw new IllegalStateException("Failure chain threw exception");
}
- context.getAttributes().put(getForwardConfigKey(), forwardConfig);
-
- // Execute our nested chain and indicate that we are through
- super.execute(context);
return (true);
}
// ------------------------------------------------------- Protected Methods
-
-
- /**
- * <p>Create and return a <code>ForwardConfig</code> representing the
- * specified module-relative destination.</p>
- *
- * @param context The context for this request
- * @param moduleConfig The <code>ModuleConfig</code> for this request
- * @param uri The module-relative URI to be the destination
- */
- protected abstract ForwardConfig forward(Context context,
- ModuleConfig moduleConfig,
- String uri);
/**
1.1
jakarta-struts/contrib/struts-chain/src/java/org/apache/struts/chain/AbstractSelectInput.java
Index: AbstractSelectInput.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-struts/contrib/struts-chain/src/java/org/apache/struts/chain/AbstractSelectInput.java,v
1.1 2003/08/31 22:42:45 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/08/31 22:42:45 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Struts", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.struts.chain;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.chain.Constants;
import org.apache.struts.config.ActionConfig;
import org.apache.struts.config.ForwardConfig;
import org.apache.struts.config.ModuleConfig;
/**
* <p>Select and cache a <code>ForwardConfig</code> that returns us to the
* input page for the current action, if any.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/08/31 22:42:45 $
*/
public abstract class AbstractSelectInput implements Command {
// ------------------------------------------------------ Instance Variables
private String actionConfigKey = Constants.ACTION_CONFIG_KEY;
private String forwardConfigKey = Constants.FORWARD_CONFIG_KEY;
private static final Log log = LogFactory.getLog(AbstractSelectInput.class);
// -------------------------------------------------------------- Properties
/**
* <p>Return the context attribute key under which the
* <code>ActionConfig</code> for the currently selected application
* action is stored.</p>
*/
public String getActionConfigKey() {
return (this.actionConfigKey);
}
/**
* <p>Set the context attribute key under which the
* <code>ActionConfig</code> for the currently selected application
* action is stored.</p>
*
* @param actionConfigKey The new context attribute key
*/
public void setActionConfigKey(String actionConfigKey) {
this.actionConfigKey = actionConfigKey;
}
/**
* <p>Return the context attribute key under which the
* <code>ForwardConfig</code> for the currently selected application
* action is stored.</p>
*/
public String getForwardConfigKey() {
return (this.forwardConfigKey);
}
/**
* <p>Set the context attribute key under which the
* <code>ForwardConfig</code> for the currently selected application
* action is stored.</p>
*
* @param forwardConfigKey The new context attribute key
*/
public void setForwardConfigKey(String forwardConfigKey) {
this.forwardConfigKey = forwardConfigKey;
}
// ---------------------------------------------------------- Public Methods
/**
* <p>Select and cache a <code>ForwardConfig</code> for the input page
* for the current request.</p>
*
* @param context The <code>Context</code> for the current request
*
* @return <code>false</code> so that processing continues
*/
public boolean execute(Context context) throws Exception {
ActionConfig actionConfig = (ActionConfig)
context.getAttributes().get(getActionConfigKey());
ModuleConfig moduleConfig = actionConfig.getModuleConfig();
// Cache an ForwardConfig back to our input page
ForwardConfig forwardConfig = null;
String input = actionConfig.getInput();
if (moduleConfig.getControllerConfig().getInputForward()) {
if (log.isTraceEnabled()) {
log.trace("Finding ForwardConfig for '" + input + "'");
}
forwardConfig = actionConfig.findForwardConfig(input);
if (forwardConfig == null) {
forwardConfig = moduleConfig.findForwardConfig(input);
}
} else {
if (log.isTraceEnabled()) {
log.trace("Delegating to forward() for '" + input + "'");
}
forwardConfig = forward(context, moduleConfig, input);
}
if (log.isDebugEnabled()) {
log.debug("Forwarding back to " + forwardConfig);
}
context.getAttributes().put(getForwardConfigKey(), forwardConfig);
return (false);
}
// ------------------------------------------------------- Protected Methods
/**
* <p>Create and return a <code>ForwardConfig</code> representing the
* specified module-relative destination.</p>
*
* @param context The context for this request
* @param moduleConfig The <code>ModuleConfig</code> for this request
* @param uri The module-relative URI to be the destination
*/
protected abstract ForwardConfig forward(Context context,
ModuleConfig moduleConfig,
String uri);
}
1.2 +4 -25
jakarta-struts/contrib/struts-chain/src/java/org/apache/struts/chain/servlet/ValidateActionForm.java
Index: ValidateActionForm.java
===================================================================
RCS file:
/home/cvs/jakarta-struts/contrib/struts-chain/src/java/org/apache/struts/chain/servlet/ValidateActionForm.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ValidateActionForm.java 11 Aug 2003 04:55:34 -0000 1.1
+++ ValidateActionForm.java 31 Aug 2003 22:42:45 -0000 1.2
@@ -67,13 +67,10 @@
import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
-import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.chain.AbstractValidateActionForm;
import org.apache.struts.chain.Constants;
import org.apache.struts.config.ActionConfig;
-import org.apache.struts.config.ForwardConfig;
-import org.apache.struts.config.ModuleConfig;
/**
@@ -89,24 +86,6 @@
// ------------------------------------------------------- Protected Methods
-
-
- /**
- * <p>Create and return a <code>ForwardConfig</code> representing the
- * specified module-relative destination.</p>
- *
- * @param context The context for this request
- * @param moduleConfig The <code>ModuleConfig</code> for this request
- * @param uri The module-relative URI to be the destination
- */
- protected ForwardConfig forward(Context context,
- ModuleConfig moduleConfig,
- String uri) {
-
- return (new ActionForward(null, moduleConfig.getPrefix() + uri,
- false, true));
-
- }
/**
1.1
jakarta-struts/contrib/struts-chain/src/java/org/apache/struts/chain/servlet/SelectInput.java
Index: SelectInput.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-struts/contrib/struts-chain/src/java/org/apache/struts/chain/servlet/SelectInput.java,v
1.1 2003/08/31 22:42:45 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/08/31 22:42:45 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Struts", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.struts.chain.servlet;
import org.apache.commons.chain.Context;
import org.apache.commons.chain.web.servlet.ServletWebContext;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.chain.AbstractSelectInput;
import org.apache.struts.chain.Constants;
import org.apache.struts.config.ActionConfig;
import org.apache.struts.config.ForwardConfig;
import org.apache.struts.config.ModuleConfig;
/**
* <p>Validate the properties of the form bean for this request. If there are
* any validation errors, execute the child commands in our chain; otherwise,
* proceed normally.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/08/31 22:42:45 $
*/
public class SelectInput extends AbstractSelectInput {
// ------------------------------------------------------- Protected Methods
/**
* <p>Create and return a <code>ForwardConfig</code> representing the
* specified module-relative destination.</p>
*
* @param context The context for this request
* @param moduleConfig The <code>ModuleConfig</code> for this request
* @param uri The module-relative URI to be the destination
*/
protected ForwardConfig forward(Context context,
ModuleConfig moduleConfig,
String uri) {
return (new ActionForward(null, moduleConfig.getPrefix() + uri,
false, true));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]