Commits

jcarreira  committed 452cd56

XW-126 & XW-124/XW-127:
Made Result not cached between result executions
Made CompoundRootAccessor not throw NPE for null arg to method

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@117e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits 22fb0c3

Comments (0)

Files changed (4)

File src/java/com/opensymphony/xwork/DefaultActionInvocation.java

     }
 
     /**
-* If the DefaultActionInvocation has been executed before and the Result is an instance of ActionChainResult, this method
-* will walk down the chain of ActionChainResults until it finds a non-chain result, which will be returned. If the
-* DefaultActionInvocation's result has not been executed before, the Result instance will be created and populated with
-* the result params.
-* @return a Result instance
-* @throws Exception
-*/
+     * If the DefaultActionInvocation has been executed before and the Result is an instance of ActionChainResult, this method
+     * will walk down the chain of ActionChainResults until it finds a non-chain result, which will be returned. If the
+     * DefaultActionInvocation's result has not been executed before, the Result instance will be created and populated with
+     * the result params.
+     * @return a Result instance
+     * @throws Exception
+     */
     public Result getResult() throws Exception {
-        if (result != null) {
-            Result returnResult = result;
+        Result returnResult = result;
 
-            // If we've chained to other Actions, we need to find the last result
-            while (returnResult instanceof ActionChainResult) {
-                ActionProxy aProxy = ((ActionChainResult) returnResult).getProxy();
+        // If we've chained to other Actions, we need to find the last result
+        while (returnResult instanceof ActionChainResult) {
+            ActionProxy aProxy = ((ActionChainResult) returnResult).getProxy();
 
-                if (aProxy != null) {
-                    Result proxyResult = aProxy.getInvocation().getResult();
+            if (aProxy != null) {
+                Result proxyResult = aProxy.getInvocation().getResult();
 
-                    if ((proxyResult != null) && (aProxy.getExecuteResult())) {
-                        returnResult = proxyResult;
-                    } else {
-                        break;
-                    }
+                if ((proxyResult != null) && (aProxy.getExecuteResult())) {
+                    returnResult = proxyResult;
                 } else {
                     break;
                 }
+            } else {
+                break;
             }
+        }
 
-            return returnResult;
-        } else {
-            Map results = proxy.getConfig().getResults();
-            ResultConfig resultConfig = (ResultConfig) results.get(resultCode);
+        return returnResult;
+    }
 
-            if (resultConfig != null) {
-                Class resultClass = resultConfig.getClazz();
+    public Result createResult() throws Exception {
+        Map results = proxy.getConfig().getResults();
+        ResultConfig resultConfig = (ResultConfig) results.get(resultCode);
+        Result newResult = null;
 
-                if (resultClass != null) {
-                    try {
-                        result = (Result) resultClass.newInstance();
-                    } catch (Exception e) {
-                        LOG.error("There was an exception while instantiating the result of type " + resultClass, e);
-                        throw e;
-                    }
+        if (resultConfig != null) {
+            Class resultClass = resultConfig.getClazz();
 
-                    OgnlUtil.setProperties(resultConfig.getParams(), result, ActionContext.getContext().getContextMap());
+            if (resultClass != null) {
+                try {
+                    newResult = (Result) resultClass.newInstance();
+                } catch (Exception e) {
+                    LOG.error("There was an exception while instantiating the result of type " + resultClass, e);
+                    throw e;
                 }
-            }
 
-            return result;
+                OgnlUtil.setProperties(resultConfig.getParams(), newResult, ActionContext.getContext().getContextMap());
+            }
         }
+        return newResult;
     }
 
     public String getResultCode() {
     }
 
     /**
- * Register a com.opensymphony.xwork.interceptor.PreResultListener to be notified after the Action is executed and before the
- * Result is executed. The ActionInvocation implementation must guarantee that listeners will be called in the order
- * in which they are registered. Listener registration and execution does not need to be thread-safe.
- * @param listener
- */
+     * Register a com.opensymphony.xwork.interceptor.PreResultListener to be notified after the Action is executed and before the
+     * Result is executed. The ActionInvocation implementation must guarantee that listeners will be called in the order
+     * in which they are registered. Listener registration and execution does not need to be thread-safe.
+     * @param listener
+     */
     public void addPreResultListener(PreResultListener listener) {
         if (preResultListeners == null) {
             preResultListeners = new ArrayList(1);
         if (!executed) {
             if (preResultListeners != null) {
                 for (Iterator iterator = preResultListeners.iterator();
-                        iterator.hasNext();) {
+                     iterator.hasNext();) {
                     PreResultListener listener = (PreResultListener) iterator.next();
                     listener.beforeResult(this, resultCode);
                 }
     }
 
     /**
-* Uses getResult to get the final Result and executes it
-*/
+     * Uses getResult to get the final Result and executes it
+     */
     private void executeResult() throws Exception {
-        Result aResult = getResult();
+        result = createResult();
 
-        if (aResult != null) {
-            aResult.execute(this);
-        } else if (!resultCode.equals(Action.NONE)){
+        if (result != null) {
+            result.execute(this);
+        } else if (!resultCode.equals(Action.NONE)) {
             LOG.warn("No result defined for action " + getAction().getClass().getName() + " and result " + getResultCode());
         }
     }

File src/java/com/opensymphony/xwork/util/CompoundRootAccessor.java

         Class[] classes = new Class[args.length];
         for (int i = 0; i < args.length; i++) {
             Object arg = args[i];
-            classes[i] = arg.getClass();
+            classes[i] = (arg != null) ? arg.getClass() : Object.class;
         }
 
         return classes;

File src/test/com/opensymphony/xwork/util/Dog.java

     public int multiplyAge(int by) {
         return age * by;
     }
+
+    /**
+     * a method which is safe to call with a null argument
+     * @param arg the Boolean to return
+     * @return arg, if it is not null, or Boolean.TRUE if arg is null
+     */
+    public Boolean nullSafeMethod(Boolean arg) {
+        return (arg == null) ? Boolean.TRUE : arg;
+    }
 }

File src/test/com/opensymphony/xwork/util/OgnlValueStackTest.java

 
         assertEquals("Jack", vs.findValue("name"));
         assertEquals("Rover", vs.findValue("[1].name"));
+        //hates will be null
+        assertEquals(Boolean.TRUE,vs.findValue("nullSafeMethod(hates)"));
     }
 
     public void testSerializable() throws IOException, ClassNotFoundException {