Anonymous avatar Anonymous committed d6ffcc6

partial collection support

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

Comments (0)

Files changed (7)

src/java/com/opensymphony/xwork/interceptor/ParametersInterceptor.java

             }
 
             try {
-                InstantiatingNullHandler.setState(true);
-                XWorkMethodAccessor.setState(true);
+                invocation.getInvocationContext().put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.TRUE);
+                invocation.getInvocationContext().put(XWorkMethodAccessor.DENY_METHOD_EXECUTION, Boolean.TRUE);
                 invocation.getInvocationContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
 
                 if (parameters != null) {
                     }
                 }
             } finally {
+                invocation.getInvocationContext().put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.FALSE);
+                invocation.getInvocationContext().put(XWorkMethodAccessor.DENY_METHOD_EXECUTION, Boolean.FALSE);
                 invocation.getInvocationContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.FALSE);
-                InstantiatingNullHandler.setState(false);
-                XWorkMethodAccessor.setState(false);
             }
         }
     }

src/java/com/opensymphony/xwork/util/InstantiatingNullHandler.java

 import ognl.NullHandler;
 
 import java.lang.reflect.Method;
-
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
 
 
 /**
  * @version $Revision$
  */
 public class InstantiatingNullHandler implements NullHandler {
-    //~ Static fields/initializers /////////////////////////////////////////////
-
-    private static ThreadLocal state = new ThreadLocal();
-
     //~ Instance fields ////////////////////////////////////////////////////////
 
     private Map clazzMap = new HashMap();
+    public static final String CREATE_NULL_OBJECTS = "xwork.NullHandler.createNullObjects";
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
-    /**
-     * this is very ugly!  however, it gets the job done.  if the state is set to on, then the InstantiatingNullHandler
-     * will create a new object if the requested property does not already exist.  the intended paradigm is
-     *
-     * <pre>
-     * try {
-     *    InstantiatingNullHandler.setState(true);
-     *    // call Ognl setters
-     * } finally {
-     *    InstantiatingNullHandler.setState(false);
-     * }
-     * </pre>
-     * @param on indicates whether or not new objects should be created
-     */
-    public static void setState(boolean on) {
-        if (on) {
-            state.set(Boolean.TRUE);
-        } else {
-            state.set(null);
-        }
-    }
-
     public Object nullMethodResult(Map context, Object target, String methodName, Object[] args) {
         return null;
     }
      * @return
      */
     public Object nullPropertyValue(Map context, Object target, Object property) {
-        if (state.get() == null) {
+        Boolean create = (Boolean) context.get(CREATE_NULL_OBJECTS);
+        boolean c = (create == null ? false : create.booleanValue());
+        if (!c) {
             return null;
         }
 
     }
 
     private Object createObject(Class clazz, Map context) throws InstantiationException, IllegalAccessException {
+        if (clazz == Collection.class || clazz == List.class) {
+            return createNewList(context);
+        } else if (clazz == Set.class) {
+
+        } else if (clazz == Map.class) {
+
+        }
+
         return clazz.newInstance();
     }
 
+    private XWorkList createNewList(Map context) {
+        Class clazz = null;
+        return new XWorkList(clazz);
+    }
+
     /**
      * Attempt to find the setter associated with the provided instance and propertyName.  If we do find it, place that
      * Method into the methodMap keyed by property name

src/java/com/opensymphony/xwork/util/XWorkConverter.java

             for (Iterator iterator = mapping.entrySet().iterator();
                     iterator.hasNext();) {
                 Map.Entry entry = (Map.Entry) iterator.next();
-                entry.setValue(createTypeConverter((String) entry.getValue()));
+                String propName = (String) entry.getKey();
+                String className = (String) entry.getValue();
+                if (!propName.startsWith("Collection_")) {
+                    entry.setValue(createTypeConverter(className));
+                }
             }
 
             mappings.put(clazz, mapping);

src/java/com/opensymphony/xwork/util/XWorkList.java

+package com.opensymphony.xwork.util;
+
+import java.util.AbstractList;
+import java.util.ArrayList;
+
+/**
+ * User: plightbo
+ * Date: Jan 13, 2004
+ * Time: 7:02:33 PM
+ */
+public class XWorkList extends ArrayList {
+    private Class clazz;
+
+    public XWorkList(Class clazz) {
+        this.clazz = clazz;
+    }
+
+    public synchronized Object get(int index) {
+        while (index >= this.size()) {
+            try {
+                this.add(clazz.newInstance());
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }
+
+        return super.get(index);
+    }
+
+}

src/java/com/opensymphony/xwork/util/XWorkMethodAccessor.java

  * Time: 8:34:20 PM
  */
 public class XWorkMethodAccessor extends ObjectMethodAccessor {
-    private static ThreadLocal state = new ThreadLocal();
+    public static final String DENY_METHOD_EXECUTION = "xwork.MethodAccessor.denyMethodExecution";
 
-    public static void setState(boolean on) {
-        if (on) {
-            state.set(Boolean.TRUE);
-        } else {
-            state.set(null);
-        }
-    }
-
-    public Object callMethod(Map map, Object object, String string, Object[] objects) throws MethodFailedException {
-        if (state.get() == null) {
-            return super.callMethod(map, object, string, objects);
+    public Object callMethod(Map context, Object object, String string, Object[] objects) throws MethodFailedException {
+        Boolean create = (Boolean) context.get(DENY_METHOD_EXECUTION);
+        boolean c = (create == null ? false : create.booleanValue());
+        if (!c) {
+            return super.callMethod(context, object, string, objects);
         } else {
             return null;
         }
     }
 
-    public Object callStaticMethod(Map map, Class aClass, String string, Object[] objects) throws MethodFailedException {
-        if (state.get() == null) {
-            return super.callStaticMethod(map, aClass, string, objects);
+    public Object callStaticMethod(Map context, Class aClass, String string, Object[] objects) throws MethodFailedException {
+        Boolean create = (Boolean) context.get(DENY_METHOD_EXECUTION);
+        boolean c = (create == null ? false : create.booleanValue());
+        if (!c) {
+            return super.callStaticMethod(context, aClass, string, objects);
         } else {
             return null;
         }

src/test/com/opensymphony/xwork/util/Foo-conversion.properties

-bar=com.opensymphony.xwork.util.FooBarConverter
+bar=com.opensymphony.xwork.util.FooBarConverter
+Collection_bars=com.opensymphony.xwork.util.Bar

src/test/com/opensymphony/xwork/util/Foo.java

 package com.opensymphony.xwork.util;
 
 import java.util.Date;
+import java.util.List;
 
 
 /**
     boolean useful;
     int number;
     long aLong;
+    List bars;
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
     public boolean isUseful() {
         return useful;
     }
+
+    public List getBars() {
+        return bars;
+    }
+
+    public void setBars(List bars) {
+        this.bars = bars;
+    }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.