Author: germuska
Date: Tue Feb 22 20:19:49 2005
New Revision: 154949

URL: http://svn.apache.org/viewcvs?view=rev&rev=154949
Log:
Add support for using LookupCommand in a way which does not pass through the
result from the looked up command.  This will allow chains used by Lookup to
"abort" (by having some command return 'false') without aborting the chain from
which the lookup was performed.


Modified:
    
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/generic/LookupCommand.java
    
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/generic/LookupCommandTestCase.java

Modified: 
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/generic/LookupCommand.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/generic/LookupCommand.java?view=diff&r1=154948&r2=154949
==============================================================================
--- 
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/generic/LookupCommand.java
 (original)
+++ 
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/generic/LookupCommand.java
 Tue Feb 22 20:19:49 2005
@@ -39,7 +39,7 @@
  * <code>IllegalArgumentException</code>.</p>
  *
  * @author Craig R. McClanahan
- * @version $Revision: 1.11 $ $Date: 2005/01/08 18:02:28 $
+ * @version $Revision: 1.11 $ $Date$
  */
 
 public class LookupCommand implements Filter {
@@ -154,25 +154,71 @@
 
     }
 
+    private boolean ignoreExecuteResult = false;
 
+    /**
+     * <p>Return <code>true</code> if this command should ignore
+     * the return value from executing the looked-up command.  
+     * Defaults to <code>false</code>, which means that the return result 
+     * of executing this lookup will be whatever is returned from that
+     * command.</p>
+     */
+    public boolean isIgnoreExecuteResult() {
+        return ignoreExecuteResult;
+    }
 
+    /**
+     * <p>Set the rules for whether or not this class will ignore or
+     * pass through the value returned from executing the looked up 
+     * command.</p>
+     * <p>If you are looking up a chain which may be "aborted" and
+     * you do not want this class to stop chain processing, then this 
+     * value should be set to <code>true</code></p>
+     * @param ignoreExecuteResult
+     */
+    public void setIgnoreExecuteResult(boolean ignoreReturn) {
+        this.ignoreExecuteResult = ignoreReturn;
+    }
+
+    private boolean ignorePostprocessResult = false;
+    
+    public boolean isIgnorePostprocessResult() {
+        return ignorePostprocessResult;
+    }
+    public void setIgnorePostprocessResult(boolean ignorePostprocessResult) {
+        this.ignorePostprocessResult = ignorePostprocessResult;
+    }
     // ---------------------------------------------------------- Filter 
Methods
 
 
     /**
-     * <p>Look up the specified command, and (if found) execute it.</p>
+     * <p>Look up the specified command, and (if found) execute it.
+     * Unless <code>ignoreExecuteResult</code> is set to <code>true</code>, 
+     * return the result of executing the found command.  If no command
+     * is found, return <code>false</code>, unless the <code>optional</code>
+     * property is <code>false</code>, in which case an 
<code>IllegalArgumentException</code>
+     * will be thrown.  
+     * </p>
      *
      * @param context The context for this request
      *
      * @exception IllegalArgumentException if no such [EMAIL PROTECTED] 
Command}
      *  can be found and the <code>optional</code> property is set
      *  to <code>false</code>
+     * @return the result of executing the looked-up command, or 
+     * <code>false</code> if no command is found or if the command
+     * is found but the <code>ignoreExecuteResult</code> property of this
+     * instance is <code>true</code>
      */
     public boolean execute(Context context) throws Exception {
 
         Command command = getCommand(context);
         if (command != null) {
-            return (command.execute(context));
+            boolean result = (command.execute(context));
+            if (isIgnoreExecuteResult()) {
+                return false;
+            }
+            return result;
         } else {
             return (false);
         }
@@ -188,13 +234,20 @@
      * @param exception Any <code>Exception</code> thrown by command execution
      *
      * @exception Exception if thrown by the <code>postprocess()</code> method
+     * @return the result of executing the <code>postprocess</code> method
+     * of the looked-up command, unless <code>ignorePostprocessResult</code> 
is 
+     * <code>true</code>.  If no command is found, return <code>false</code>,
+     * unless the <code>optional</code> property is <code>false</code>, in 
which 
+     * case <code>IllegalArgumentException</code> will be thrown.
      */
     public boolean postprocess(Context context, Exception exception) {
 
         Command command = getCommand(context);
         if (command != null) {
             if (command instanceof Filter) {
-                return (((Filter) command).postprocess(context, exception));
+                boolean result = (((Filter) command).postprocess(context, 
exception));
+                if (isIgnorePostprocessResult())  return false;
+                return result;
             }
         }
         return (false);

Modified: 
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/generic/LookupCommandTestCase.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/generic/LookupCommandTestCase.java?view=diff&r1=154948&r2=154949
==============================================================================
--- 
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/generic/LookupCommandTestCase.java
 (original)
+++ 
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/generic/LookupCommandTestCase.java
 Tue Feb 22 20:19:49 2005
@@ -180,6 +180,23 @@
         checkExecuteLog("2b1/2b2/2b3");
     }
 
+    // Test ability to lookup and execute single non-delegating command, 
ignoring its result
+    public void testExecuteMethodLookup_3a() {
+
+        // use default catalog
+        catalog.addCommand("foo", new NonDelegatingCommand("3a"));
+        command.setIgnoreExecuteResult(true);
+        command.setName("foo");
+
+        try {
+            assertFalse("Command should return false",
+                       command.execute(context));
+        } catch (Exception e) {
+            fail("Threw exception: " + e);
+        }
+        checkExecuteLog("3a");
+    }
+
 
     // -------------------------------------------------------- Support Methods
 



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to