You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by mr...@apache.org on 2005/08/14 02:25:30 UTC

svn commit: r232550 - in /struts/sandbox/trunk/ti/src/java: ./ org/apache/ti/interceptor/ org/apache/ti/processor/ org/apache/ti/processor/chain/

Author: mrdon
Date: Sat Aug 13 17:25:26 2005
New Revision: 232550

URL: http://svn.apache.org/viewcvs?rev=232550&view=rev
Log:
 * Adding method to support calling of event-type methods. 
   For example, a validate event for the action method
   'someAction' would be called 'someAction_validate()'
 * Adding Struts 1.x style workflow where if validation 
   fails, the "input" result will be returned and the action
   will not be executed.

Added:
    struts/sandbox/trunk/ti/src/java/org/apache/ti/interceptor/ControllerWorkflowInterceptor.java
Modified:
    struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/ControllerActionInvocation.java
    struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/InvokeAction.java
    struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/ChainInvokeAction.java
    struts/sandbox/trunk/ti/src/java/ti-default.xml

Added: struts/sandbox/trunk/ti/src/java/org/apache/ti/interceptor/ControllerWorkflowInterceptor.java
URL: http://svn.apache.org/viewcvs/struts/sandbox/trunk/ti/src/java/org/apache/ti/interceptor/ControllerWorkflowInterceptor.java?rev=232550&view=auto
==============================================================================
--- struts/sandbox/trunk/ti/src/java/org/apache/ti/interceptor/ControllerWorkflowInterceptor.java (added)
+++ struts/sandbox/trunk/ti/src/java/org/apache/ti/interceptor/ControllerWorkflowInterceptor.java Sat Aug 13 17:25:26 2005
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package org.apache.ti.interceptor;
+
+import com.opensymphony.xwork.Action;
+import com.opensymphony.xwork.ActionInvocation;
+import com.opensymphony.xwork.Validateable;
+import com.opensymphony.xwork.*;
+import com.opensymphony.xwork.validator.*;
+import com.opensymphony.xwork.interceptor.Interceptor;
+
+import org.apache.ti.processor.*;
+import org.apache.commons.logging.*;
+
+
+/**
+ * An interceptor that does some basic validation workflow before allowing the interceptor chain to continue.
+ * The order of execution in the workflow is:
+ * <p/>
+ * <ol>
+ * <li>If the action being executed implements {@link Validateable}, the action's
+ * {@link Validateable#validate() validate} method is called.</li>
+ * <li>Next, if the action implements {@link ValidationAware}, the action's
+ * {@link ValidationAware#hasErrors() hasErrors} method is called. If this
+ * method returns true, this interceptor stops the chain from continuing and
+ * immediately returns {@link Action#INPUT}</li>
+ * </ol>
+ * <p/>
+ * <i>Note: if the action doesn't implement either interface, this interceptor effectively does nothing.</i>
+ *
+ * @author Jason Carreira
+ */
+public class ControllerWorkflowInterceptor implements Interceptor {
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    private static final Log log = LogFactory.getLog(ControllerWorkflowInterceptor.class);
+    
+    public void destroy() {
+    }
+
+    public void init() {
+    }
+
+    public String intercept(ActionInvocation invocation) throws Exception {
+        ControllerActionInvocation inv = (ControllerActionInvocation)invocation;
+        Object action = invocation.getAction();
+        
+        ValidatorContext val = ControllerContext.getContext().getValidatorContext();
+        inv.invokeActionEvent("validate", 
+            new Class[]{ValidatorContext.class},
+            new Object[] {val},
+            true);
+        
+        
+        if (action instanceof Validateable) {
+            Validateable validateable = (Validateable) action;
+            validateable.validate();
+        }
+
+        if (val.hasErrors()) {
+            if (inv.getProxy().getConfig().getResults().containsKey(Action.INPUT)) {
+                return Action.INPUT;
+            } else {
+                log.debug("Input result not found, action will be invoked");
+            }
+        }
+
+        return invocation.invoke();
+    }
+}

Modified: struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/ControllerActionInvocation.java
URL: http://svn.apache.org/viewcvs/struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/ControllerActionInvocation.java?rev=232550&r1=232549&r2=232550&view=diff
==============================================================================
--- struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/ControllerActionInvocation.java (original)
+++ struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/ControllerActionInvocation.java Sat Aug 13 17:25:26 2005
@@ -50,6 +50,62 @@
         this.invokeAction = inv;
     }
 
+    public Object invokeActionEvent(String eventName, boolean optional) throws Exception {
+        return invokeActionEvent(eventName, null, null, optional);
+    }
+    
+    public Object invokeActionEvent(String eventName, Class[] otherArgs, 
+            Object[] otherParams, boolean optional) throws Exception {
+    
+        // TODO: this should be optimized
+        Object result = null;
+        Method method = null;
+        Class[] args = otherArgs;
+        Object[] params = otherParams;
+        if (form != null) {
+            if (otherArgs == null) {
+                args = new Class[] {form.getClass()};
+                params = new Object[] {form};
+            } else {
+                args = new Class[otherArgs.length + 1];
+                args[0] = form.getClass();
+                System.arraycopy(otherArgs, 0, args, 1, otherArgs.length);
+                
+                params = new Object[otherParams.length + 1];
+                params[0] = form;
+                System.arraycopy(otherParams, 0, params, 1, otherParams.length);
+            }
+        }
+        
+        String methodName = getActionMethod().getName();
+        methodName += "_" + eventName;
+        Class cls = getAction().getClass();
+        try {
+            method = cls.getMethod(methodName, args);
+        } catch (NoSuchMethodException ex) {
+            log.debug("Unable to locate action event method "+methodName);
+            if (!optional) {
+                throw ex;
+            } 
+        }
+        
+        if (method != null) {
+            try {
+                result = method.invoke(action, params);
+            } catch (InvocationTargetException e) {
+                // We try to return the source exception.
+                Throwable t = e.getTargetException();
+    
+                if (t instanceof Exception) {
+                    throw (Exception) t;
+                } else {
+                    throw e;
+                }
+            }
+        }
+        return result;
+    }
+    
     public Method getActionMethod() {
         // TODO: this should be optimized 
         if (actionMethod == null) {
@@ -80,6 +136,7 @@
         return actionMethod;
     }
     
+    
     public Object getForm() {
         return form;
     }
@@ -94,7 +151,7 @@
      */
     protected String invokeAction(Object action, ActionConfig actionConfig) throws Exception {
         
-        return invokeAction.invokeAction(action, actionConfig);
+        return invokeAction.invoke(action, actionConfig);
     }
     
     public String invokeXWorkAction(Object action, ActionConfig actionConfig)

Modified: struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/InvokeAction.java
URL: http://svn.apache.org/viewcvs/struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/InvokeAction.java?rev=232550&r1=232549&r2=232550&view=diff
==============================================================================
--- struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/InvokeAction.java (original)
+++ struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/InvokeAction.java Sat Aug 13 17:25:26 2005
@@ -31,6 +31,6 @@
      *  Invokes action.  If the action method contains one parameter, this method
      *  handles its execution.  Otherwise, it is delegated to the super class.
      */
-    public String invokeAction(Object action, ActionConfig actionConfig) throws Exception;
+    public String invoke(Object action, ActionConfig actionConfig) throws Exception;
         
 }

Modified: struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/ChainInvokeAction.java
URL: http://svn.apache.org/viewcvs/struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/ChainInvokeAction.java?rev=232550&r1=232549&r2=232550&view=diff
==============================================================================
--- struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/ChainInvokeAction.java (original)
+++ struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/ChainInvokeAction.java Sat Aug 13 17:25:26 2005
@@ -50,7 +50,7 @@
      *  Invokes action.  If the action method contains one parameter, this method
      *  handles its execution.  Otherwise, it is delegated to the super class.
      */
-    public String invokeAction(Object action, ActionConfig actionConfig) throws Exception {
+    public String invoke(Object action, ActionConfig actionConfig) throws Exception {
         
         CatalogFactory factory = CatalogFactory.getInstance();
         Catalog cat = factory.getCatalog(catalogName);

Modified: struts/sandbox/trunk/ti/src/java/ti-default.xml
URL: http://svn.apache.org/viewcvs/struts/sandbox/trunk/ti/src/java/ti-default.xml?rev=232550&r1=232549&r2=232550&view=diff
==============================================================================
--- struts/sandbox/trunk/ti/src/java/ti-default.xml (original)
+++ struts/sandbox/trunk/ti/src/java/ti-default.xml Sat Aug 13 17:25:26 2005
@@ -28,6 +28,8 @@
             <interceptor name="validation" class="org.apache.ti.interceptor.ControllerValidationInterceptor" />
             <interceptor name="controllerValidation" class="com.opensymphony.xwork.validator.ValidationInterceptor"/>
             <interceptor name="workflow" class="com.opensymphony.xwork.interceptor.DefaultWorkflowInterceptor"/>
+            <interceptor name="controllerWorkflow" class="com.opensymphony.xwork.interceptor.ControllerWorkflowInterceptor"/>
+            
             <interceptor name="servlet-config" class="com.opensymphony.webwork.interceptor.ServletConfigInterceptor"/>
             <interceptor name="prepare" class="com.opensymphony.xwork.interceptor.PrepareInterceptor"/>
             <interceptor name="conversionError" class="com.opensymphony.webwork.interceptor.WebWorkConversionErrorInterceptor"/>
@@ -47,7 +49,7 @@
             <interceptor-stack name="validationWorkflowStack">
                 <interceptor-ref name="defaultStack"/>
                 <interceptor-ref name="controllerValidation"/>
-                <interceptor-ref name="workflow"/>
+                <interceptor-ref name="controllerWorkflow"/>
             </interceptor-stack>
 
             <!-- Sample file upload stack -->



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org