Author: germuska Date: Sun Jan 2 18:28:56 2005 New Revision: 123922 URL: http://svn.apache.org/viewcvs?view=rev&rev=123922 Log: Add support for executing chain commands associated with ActionMappings, instead of or in addition to normal action/forward/include processing.
Also correct reference to TilesPreProcessor in chain-config.xml reflecting the factoring of tiles out of core. Added: struts/core/trunk/src/share/org/apache/struts/chain/commands/ExecuteCommand.java Modified: struts/core/trunk/conf/share/chain-config.xml struts/core/trunk/conf/share/struts-config_1_3.dtd struts/core/trunk/src/share/org/apache/struts/config/ActionConfig.java struts/core/trunk/src/share/org/apache/struts/config/ActionConfigMatcher.java Modified: struts/core/trunk/conf/share/chain-config.xml Url: http://svn.apache.org/viewcvs/struts/core/trunk/conf/share/chain-config.xml?view=diff&rev=123922&p1=struts/core/trunk/conf/share/chain-config.xml&r1=123921&p2=struts/core/trunk/conf/share/chain-config.xml&r2=123922 ============================================================================== --- struts/core/trunk/conf/share/chain-config.xml (original) +++ struts/core/trunk/conf/share/chain-config.xml Sun Jan 2 18:28:56 2005 @@ -182,6 +182,12 @@ className="org.apache.struts.chain.commands.servlet.SelectInput"/> + <!-- Lookup and execute a chain command if the current ActionConfig is + so-configured. --> + <command + className="org.apache.struts.chain.commands.ExecuteCommand"/> + + <!-- Select the appropriate ForwardConfig for action mappings that only have an ActionForward --> <command @@ -209,15 +215,8 @@ <!-- - If you want to use Tiles, uncomment this command, and modify your - Struts Configuration to use this PlugIn: - "org.apache.struts.chain.legacy.TilesPlugin" - - This PlugIn is required because the basic TilesPlugIn tests at initialization - time to make sure that you are specific using the TilesRequestProcessor - (or a subclass), and the struts-chain ComposableRequestProcessor fails that test. - In every other respect, the struts-chain TilesPlugin is identical to the normal - TilesPlugIn, and is configured in the same way. + If you want to use Tiles, uncomment this command and make sure you have + the struts-tiles JAR included in your web application. <command className="org.apache.struts.chain.commands.servlet.TilesPreProcessor"/> Modified: struts/core/trunk/conf/share/struts-config_1_3.dtd Url: http://svn.apache.org/viewcvs/struts/core/trunk/conf/share/struts-config_1_3.dtd?view=diff&rev=123922&p1=struts/core/trunk/conf/share/struts-config_1_3.dtd&r1=123921&p2=struts/core/trunk/conf/share/struts-config_1_3.dtd&r2=123922 ============================================================================== --- struts/core/trunk/conf/share/struts-config_1_3.dtd (original) +++ struts/core/trunk/conf/share/struts-config_1_3.dtd Sun Jan 2 18:28:56 2005 @@ -361,6 +361,10 @@ the bean's specified "name". Optional if "name" is specified, else not valid. + catalog The name of a commons-chain catalog in which to look up + a command to be executed as part of servicing this request. + Only meaningful if "command" is also specified. + className The fully qualified Java class name of the ActionMapping subclass to use for this action mapping object. Defaults to the type specified by the enclosing <action-mappings> @@ -368,6 +372,9 @@ not specified. ["org.apache.struts.action.ActionMapping"] + command The name of a commons-chain command which should be looked up + and executed as part of servicing this request. + forward Module-relative path of the servlet or other resource that will process this request, instead of the Action class specified by "type". The path WILL NOT be processed @@ -443,7 +450,9 @@ <!ELEMENT action (icon?, display-name?, description?, set-property*, exception*, forward*)> <!ATTLIST action id ID #IMPLIED> <!ATTLIST action attribute %BeanName; #IMPLIED> +<!ATTLIST action catalog CDATA #IMPLIED> <!ATTLIST action className %ClassName; #IMPLIED> +<!ATTLIST action command CDATA #IMPLIED> <!ATTLIST action forward %RequestPath; #IMPLIED> <!ATTLIST action include %RequestPath; #IMPLIED> <!ATTLIST action input %RequestPath; #IMPLIED> Added: struts/core/trunk/src/share/org/apache/struts/chain/commands/ExecuteCommand.java Url: http://svn.apache.org/viewcvs/struts/core/trunk/src/share/org/apache/struts/chain/commands/ExecuteCommand.java?view=auto&rev=123922 ============================================================================== --- (empty file) +++ struts/core/trunk/src/share/org/apache/struts/chain/commands/ExecuteCommand.java Sun Jan 2 18:28:56 2005 @@ -0,0 +1,176 @@ +package org.apache.struts.chain.commands; + +import org.apache.struts.chain.Constants; +import org.apache.struts.config.ActionConfig; + +import org.apache.commons.chain.Catalog; +import org.apache.commons.chain.CatalogFactory; +import org.apache.commons.chain.Command; +import org.apache.commons.chain.Context; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * <p>Invoke the appropriate <code>Command</code> for this request. If the context's + * <code>ActionConfig</code> has no <code>command</code> property defined, no action + * will be taken. If the specified command cannot be found, a warning will be logged, + * but processing will continue. Depending on how the chain is configured, this can + * be used in place of an <code>Action</code> or as a method of performing pre-processing. + * </p> + * <p>If used instead of an action, the command which is looked up should put an ActionForward + * into the context, unless it has already dealt with the response.</p> + * @version $Id$ + */ +public class ExecuteCommand implements Command { + + + // ------------------------------------------------------ Instance Variables + + + private String actionConfigKey = Constants.ACTION_CONFIG_KEY; + private String validKey = Constants.VALID_KEY; + + private static final Log log = + LogFactory.getLog(ExecuteCommand.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 + * validity flag for this request is stored.</p> + */ + public String getValidKey() { + + return (this.validKey); + + } + + + /** + * <p>Set the context attribute key under which the + * validity flag for this request is stored.</p> + * + * @param validKey The new context attribute key + */ + public void setValidKey(String validKey) { + + this.validKey = validKey; + + } + + + // ---------------------------------------------------------- Public Methods + + + /** + * <p>If the <code>context</code> is "valid", lookup a command and execute it.</p> + * + * @param context The <code>Context</code> for the current request + * + * @return the result of the lookup command's <code>execute</code> method, if executed, + * or <code>false</code> if it was not executed. + */ + public boolean execute(Context context) throws Exception { + + if (shouldProcess(context)) { + + Command command = getCommand(context); + + if (command != null) { + return (command.execute(context)); + } + + } + + return (false) ; + + } + + /** + * Evaluate the current context to see if a command should even be + * executed. + * @param context + * @return + */ + protected boolean shouldProcess(Context context) { + // Skip processing if the current request is not valid + Boolean valid = (Boolean) context.get(getValidKey()); + return ((valid != null) && valid.booleanValue()); + + } + + /** + * Find the <code>ActionConfig</code> in the current context and, if it is + * properly configured, lookup the appropriate <code>commons-chain</code> command. + * @param context + * @return a <code>Command</code> to execute, or null if none is specified + * or if the specified command cannot be found. + */ + protected Command getCommand(Context context) { + + ActionConfig actionConfig = (ActionConfig) + context.get(getActionConfigKey()); + + String commandName = actionConfig.getCommand(); + + if (commandName == null) { + return null; + } + + String catalogName = actionConfig.getCatalog(); + + Command command = null; + Catalog catalog = null; + + if (catalogName != null) { + catalog = CatalogFactory.getInstance().getCatalog(catalogName); + if (catalog == null) { + log.warn("No catalog found under " + catalogName); + return null; + } + + } else { + catalogName = "the default catalog"; + catalog = CatalogFactory.getInstance().getCatalog(); + if (catalog == null) { + log.warn("No default catalog found."); + return null; + } + } + + log.debug("looking up command " + commandName + " in " + catalogName); + return catalog.getCommand(commandName); + + } + + +} \ No newline at end of file Modified: struts/core/trunk/src/share/org/apache/struts/config/ActionConfig.java Url: http://svn.apache.org/viewcvs/struts/core/trunk/src/share/org/apache/struts/config/ActionConfig.java?view=diff&rev=123922&p1=struts/core/trunk/src/share/org/apache/struts/config/ActionConfig.java&r1=123921&p2=struts/core/trunk/src/share/org/apache/struts/config/ActionConfig.java&r2=123922 ============================================================================== --- struts/core/trunk/src/share/org/apache/struts/config/ActionConfig.java (original) +++ struts/core/trunk/src/share/org/apache/struts/config/ActionConfig.java Sun Jan 2 18:28:56 2005 @@ -533,6 +533,78 @@ this.validate = validate; } + /** + * The name of a <code>commons-chain</code> command which should + * be executed as part of the processing of this action. + * @since Struts 1.3.0 + */ + protected String command = null; + + /** + * The name of a <code>commons-chain</code> catalog in which <code>command</code> + * should be sought. If a <code>command</code> is defined and this property is undefined, + * the "default" catalog will be used. + * This is likely to be infrequently + * used after a future release of <code>commons-chain</code> supports + * a one-string expression of a catalog/chain combination. + * @since Struts 1.3.0 + */ + protected String catalog = null; + + /** + * Get the name of a <code>commons-chain</code> command which should + * be executed as part of the processing of this action. + * @return name of a <code>commons-chain</code> command which should + * be executed as part of the processing of this action. + * @since Struts 1.3.0 + */ + public String getCommand() { + return (this.command); + } + + /** + * Get the name of a <code>commons-chain</code> catalog in which + * a specified command should be sought. This is likely to be infrequently + * used after a future release of <code>commons-chain</code> supports + * a one-string expression of a catalog/chain combination. + * @return name of a <code>commons-chain</code> catalog in which + * a specified command should be sought. + * @since Struts 1.3.0 + */ + public String getCatalog() { + return (this.catalog); + } + + /** + * Set the name of a <code>commons-chain</code> command which should + * be executed as part of the processing of this action. + * @param command name of a <code>commons-chain</code> command which should + * be executed as part of the processing of this action. + * @since Struts 1.3.0 + */ + public void setCommand(String command) { + if (configured) { + throw new IllegalStateException("Configuration is frozen"); + } + this.command = command; + } + + /** + * Set the name of a <code>commons-chain</code> catalog in which + * a specified command should be sought. This is likely to be infrequently + * used after a future release of <code>commons-chain</code> supports + * a one-string expression of a catalog/chain combination. + * @param catalog name of a <code>commons-chain</code> catalog in which + * a specified command should be sought. + * @since Struts 1.3.0 + */ + public void setCatalog(String catalog) { + if (configured) { + throw new IllegalStateException("Configuration is frozen"); + } + this.catalog = catalog; + } + // --------------------------------------------------------- Public Methods Modified: struts/core/trunk/src/share/org/apache/struts/config/ActionConfigMatcher.java Url: http://svn.apache.org/viewcvs/struts/core/trunk/src/share/org/apache/struts/config/ActionConfigMatcher.java?view=diff&rev=123922&p1=struts/core/trunk/src/share/org/apache/struts/config/ActionConfigMatcher.java&r1=123921&p2=struts/core/trunk/src/share/org/apache/struts/config/ActionConfigMatcher.java&r2=123922 ============================================================================== --- struts/core/trunk/src/share/org/apache/struts/config/ActionConfigMatcher.java (original) +++ struts/core/trunk/src/share/org/apache/struts/config/ActionConfigMatcher.java Sun Jan 2 18:28:56 2005 @@ -154,6 +154,8 @@ config.setForward(convertParam(orig.getForward(), vars)); config.setInclude(convertParam(orig.getInclude(), vars)); config.setInput(convertParam(orig.getInput(), vars)); + config.setCatalog(convertParam(orig.getCatalog(), vars)); + config.setCommand(convertParam(orig.getCommand(), vars)); ForwardConfig[] fConfigs = orig.findForwardConfigs(); ForwardConfig cfg; --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]