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/08 08:39:13 UTC

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

Author: mrdon
Date: Sun Aug  7 23:39:10 2005
New Revision: 230763

URL: http://svn.apache.org/viewcvs?rev=230763&view=rev
Log:
 * Added ability to define form by declaring a single action method
   parameter.  For example, login(LoginBean form).
 * Added new form bean creation chain that uses a chain of commands,
   each examining the form class to see if they can instantiate it.
   This will allow Ti to mimic Struts classic by creating ActionForms
   and DynaActionForms.
 * Switching to correct version of eclipse compiler  

Added:
    struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/CreateFormChain.java
    struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/CreateJavaBeanForm.java
Modified:
    struts/sandbox/trunk/ti/project.xml
    struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/ControllerActionInvocation.java
    struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/chain-config-servlet.xml

Modified: struts/sandbox/trunk/ti/project.xml
URL: http://svn.apache.org/viewcvs/struts/sandbox/trunk/ti/project.xml?rev=230763&r1=230762&r2=230763&view=diff
==============================================================================
--- struts/sandbox/trunk/ti/project.xml (original)
+++ struts/sandbox/trunk/ti/project.xml Sun Aug  7 23:39:10 2005
@@ -276,7 +276,7 @@
     <dependency>
       <groupId>eclipse</groupId>
       <artifactId>jdtcore</artifactId>
-      <version>3.0.1</version>
+      <version>3.1.0</version>
       <properties>
         <war.bundle>true</war.bundle>
       </properties>

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=230763&r1=230762&r2=230763&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 Sun Aug  7 23:39:10 2005
@@ -31,7 +31,8 @@
 
     protected BeanFactory beanFactory;
     protected Method actionMethod;
-
+    protected Object form;
+    
     protected ControllerActionInvocation(ActionProxy proxy) throws Exception {
         this(proxy, null);
     }
@@ -45,16 +46,66 @@
     }
 
     public Method getActionMethod() {
+        // TODO: this should be optimized 
         if (actionMethod == null) {
             if (getAction() != null) {
                 try {
                     actionMethod = proxy.getConfig().getMethod(getAction().getClass());
                 } catch (NoSuchMethodException ex) {
+                    Class cls = getAction().getClass();
+                    String methodName = proxy.getConfig().getMethodName();
+
+                    Method[] methods = cls.getMethods();
+                    Class[] args;
+                    for (int x=0; x<methods.length; x++) {
+                        if (methods[x].getName().equals(methodName) &&
+                            methods[x].getParameterTypes().length == 1) {
+                            actionMethod = methods[x];
+                            break;
+                        }
+                    }
+                }
+                
+                if (actionMethod == null) {
                     throw new IllegalStateException("Cannot location method '"+proxy.getConfig().getMethodName()
                         + "' in action '"+getAction().getClass()+"'");
                 }
             }    
         }    
         return actionMethod;
+    }
+    
+    public Object getForm() {
+        return form;
+    }
+    
+    public void setForm(Object o) {
+        this.form = o;
+    }
+
+    /**
+     *  Invokes action.  If the action method contains one parameter, this method
+     *  handles its execution.  Otherwise, it is delegated to the super class.
+     */
+    protected String invokeAction(Object action, ActionConfig actionConfig) throws Exception {
+        
+        Method method = getActionMethod();
+                
+        if (method.getParameterTypes().length == 1) {
+            try {
+                return (String) method.invoke(action, new Object[] {form});
+            } catch (InvocationTargetException e) {
+                // We try to return the source exception.
+                Throwable t = e.getTargetException();
+    
+                if (t instanceof Exception) {
+                    throw (Exception) t;
+                } else {
+                    throw e;
+                }
+            }
+        } else {
+            return super.invokeAction(action, actionConfig);
+        }
     }
 }

Added: struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/CreateFormChain.java
URL: http://svn.apache.org/viewcvs/struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/CreateFormChain.java?rev=230763&view=auto
==============================================================================
--- struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/CreateFormChain.java (added)
+++ struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/CreateFormChain.java Sun Aug  7 23:39:10 2005
@@ -0,0 +1,68 @@
+/*
+ * $Id: CreateFormChain.java 230569 2005-08-06 19:41:10Z mrdon $
+ *
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ti.processor.chain;
+
+import org.apache.ti.processor.*;
+import java.lang.reflect.Method;
+import org.apache.commons.chain.Context;
+import org.apache.commons.chain.web.WebContext;
+import org.apache.commons.chain.impl.ChainBase;
+
+import com.opensymphony.xwork.ActionContext;
+import com.opensymphony.xwork.ActionProxy;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ *  Initializes XWork by replacing default factories.
+ */
+public class CreateFormChain extends ChainBase {
+
+    private static final Log log = LogFactory.getLog(CreateFormChain.class);
+
+    public static final String FORM_CLASS = "formClass";
+    public static final String FORM_OBJECT = "formObject";
+    
+    public boolean execute(Context origctx) throws Exception {
+        //WebContext ctx = (WebContext)origctx;
+        
+        log.info("Processing create form chain");
+
+        ActionContext ctx = ActionContext.getContext();
+        ControllerActionInvocation inv = (ControllerActionInvocation)ctx.getActionInvocation();
+        
+        Method method = inv.getActionMethod();
+        Class[] params = method.getParameterTypes();
+        if (params.length == 1) {
+            origctx.put(FORM_CLASS, params[0]);
+
+            boolean retCode = super.execute(origctx);
+            if (retCode) {
+                Object o = origctx.get(FORM_OBJECT);
+                log.info("Created form: "+o);
+                inv.setForm(o);
+                ctx.getValueStack().push(o);
+            } else {
+                throw new IllegalStateException("Form "+params[0]+" unable to "
+                    + "be created.");
+            }
+        }
+        return false;
+    }
+}

Added: struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/CreateJavaBeanForm.java
URL: http://svn.apache.org/viewcvs/struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/CreateJavaBeanForm.java?rev=230763&view=auto
==============================================================================
--- struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/CreateJavaBeanForm.java (added)
+++ struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/CreateJavaBeanForm.java Sun Aug  7 23:39:10 2005
@@ -0,0 +1,49 @@
+/*
+ * $Id: CreateJavaBeanForm.java 230535 2005-08-06 07:56:40Z mrdon $
+ *
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ti.processor.chain;
+
+import org.apache.commons.chain.Command;
+import org.apache.commons.chain.Context;
+
+import com.opensymphony.xwork.ActionContext;
+import com.opensymphony.xwork.ActionProxy;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ *  Tries to instantiate the form class's no-arg constructor.
+ */
+public class CreateJavaBeanForm implements Command {
+
+    private static final Log log = LogFactory.getLog(CreateJavaBeanForm.class);
+
+    public boolean execute(Context origctx) throws Exception {
+        log.debug("Creating JavaBean form");
+        
+        Class cls = (Class) origctx.get(CreateFormChain.FORM_CLASS);
+        try {
+            Object o = cls.newInstance();
+            origctx.put(CreateFormChain.FORM_OBJECT, o);
+            return true;
+        } catch (Exception ex) {
+            log.warn("Unable to create object from class "+cls, ex);
+        }
+        return false;
+    }
+}

Modified: struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/chain-config-servlet.xml
URL: http://svn.apache.org/viewcvs/struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/chain-config-servlet.xml?rev=230763&r1=230762&r2=230763&view=diff
==============================================================================
--- struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/chain-config-servlet.xml (original)
+++ struts/sandbox/trunk/ti/src/java/org/apache/ti/processor/chain/chain-config-servlet.xml Sun Aug  7 23:39:10 2005
@@ -58,9 +58,13 @@
     <chain     name="process-action" className="org.apache.ti.processor.chain.ProcessActionChain">
       
       <command name="initControllerContext"  />
+      <lookup catalogName="struts-ti" name="createForm" optional="true" />
       <command name="selectLocale" className="org.apache.ti.processor.chain.servlet.SelectLocale" />
       <command name="executeAction" className="org.apache.ti.processor.chain.ExecuteAction" />
     </chain>
     
+    <chain name="createForm" className="org.apache.ti.processor.chain.CreateFormChain">
+      <command name="createJavaBeanForm" className="org.apache.ti.processor.chain.CreateJavaBeanForm" />
+    </chain>
 
 </catalog>



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