webwork/src/main/webwork/util InjectionUtils.java,NONE,1.1 BeanUtil.java,1.39,1.40 ValueStack.java,1.59,1.60

[email protected] Fri, 04 Jun 2004 00:57:41 -0700
Newsgroups gmane.comp.java.open-symphony.cvs
Message-ID <[email protected]>
Update of /cvsroot/opensymphony/webwork/src/main/webwork/util
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5907/src/main/webwork/util

Modified Files:
	BeanUtil.java ValueStack.java 
Added Files:
	InjectionUtils.java 
Log Message:
Allow basic 'AOP' style injection in webwork for method calls made via
reflection.

The basic usage will be to allow profiling to the code, although other
usages I'm sure are possible.

Fixes WW-550.

Added a new class 'InjectionUtils', and a new webwork property
'webwork.injection.class', which you can implement yourself.

Changed all usages of Method.invoke() to call InjectionUtils.invoke()
instead.



--- NEW FILE: InjectionUtils.java ---
package webwork.util;

import org.apache.commons.logging.LogFactory;
import webwork.config.Configuration;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * This class allows for basic 'AOP' style around processing of method invocations.  The most common
 * usage would be to profile or log method calls.
 * <p>
 * To use your own class, you need to implement {@link InjectionImpl}, and set the webwork property
 * <code>webwork.injection.class</code> to the class that you wish to use.
 *
 * @author <a href="mailto:[email protected]">Scott Farquhar</a>
 */
public class InjectionUtils
{
    private static InjectionImpl impl;
    public static final String CLASS_NAME = "webwork.injection.class";

    static {
        impl = new DefaultInjectionImpl();

        String classname = (String) Configuration.get(CLASS_NAME);
        if (classname != null && classname.length() > 0) {
            try
            {
                impl = (InjectionImpl) ClassLoaderUtils.loadClass(classname, InjectionUtils.class).newInstance();
            }
            catch (Exception e)
            {
                LogFactory.getLog(InjectionUtils.class).error("Could not load class " + classname + " or could not cast it to InjectionUtils.  Using default", e);
            }
        }
    }

    public static Object invoke(Method target, Object value, Object[] args) throws IllegalAccessException, InvocationTargetException {
        return impl.invoke(target, value, args);
    }

    public static interface InjectionImpl {
        public Object invoke(Method target, Object value, Object[] args) throws IllegalAccessException, InvocationTargetException;
    }

    public static class DefaultInjectionImpl implements InjectionImpl {
        public Object invoke(Method target, Object value, Object[] args) throws IllegalAccessException, InvocationTargetException {
            return target.invoke(value, args);
        }
    }

}

Index: BeanUtil.java
===================================================================
RCS file: /cvsroot/opensymphony/webwork/src/main/webwork/util/BeanUtil.java,v
retrieving revision 1.39
retrieving revision 1.40
diff -u -d -r1.39 -r1.40
--- BeanUtil.java	18 Nov 2003 00:55:37 -0000	1.39
+++ BeanUtil.java	4 Jun 2004 07:57:24 -0000	1.40
@@ -24,7 +24,7 @@
 /**
  *   JavaBean utility methods
  *
- *   @author Rickard Öberg ([email protected])
+ *   @author Rickard Öberg ([email protected])
  *   @author Matt Baldree ([email protected])
  *   @version $Revision$
  */
@@ -134,9 +134,9 @@
                Method writeMethod = pdTo.getWriteMethod();
                if (writeMethod != null && readMethod != null)
                {
-                  writeParameters[0] = pdFrom.getReadMethod().invoke(from, readParameters);
+                  writeParameters[0] = InjectionUtils.invoke(pdFrom.getReadMethod(), from, readParameters);
                   if (!(!includeNull && writeParameters[0] == null))
-                     pdTo.getWriteMethod().invoke(to, writeParameters);
+                     InjectionUtils.invoke(pdTo.getWriteMethod(), to, writeParameters);
                }
             }
          }
@@ -317,7 +317,7 @@
                   {
                      throw new IllegalArgumentException("Read Method " + descriptor.getName() + " for " + curObject.getClass().getName() + " was not found");
                   }
-                  curObject = m.invoke(curObject, new Object[0]);
+                  curObject = InjectionUtils.invoke(m, curObject, new Object[0]);
                }
             } catch (IllegalArgumentException iae)
             {
@@ -357,7 +357,7 @@
                      {
                         throw new IllegalArgumentException("Indexed Read Method " + descriptor.getName() + " for " + curObject.getClass().getName() + " was not found");
                      }
-                     curObject = m.invoke(curObject, new Object[]{index});
+                     curObject = InjectionUtils.invoke(m, curObject, new Object[]{index});
                   } else
                   {
                      throw new IllegalArgumentException("Attempting to access non-indexed field \"" + curSegment.getId() + "\" as an indexed field");
@@ -680,7 +680,7 @@
       try
       {
          Object[] writeParameter = new Object[]{index, val};
-         m.invoke(obj, writeParameter);
+         InjectionUtils.invoke(m, obj, writeParameter);
          return true;
       } catch (Exception e)
       {
@@ -730,7 +730,7 @@
       try
       {
          Object[] writeParameter = new Object[]{val};
-         m.invoke(obj, writeParameter);
+         InjectionUtils.invoke(m, obj, writeParameter);
          return true;
       } catch (Exception e)
       {
@@ -765,11 +765,11 @@
          if (descriptor.getPropertyType().equals(String.class) ||
             descriptor.getPropertyType().equals(Object.class))
          {
-            m.invoke(obj, new Object[]{values[0]});
+            InjectionUtils.invoke(m, obj, new Object[]{values[0]});
             return true;
          } else if (descriptor.getPropertyType().equals(String[].class))
          {
-            m.invoke(obj, new Object[]{values});
+            InjectionUtils.invoke(m, obj, new Object[]{values});
             return true;
          }
       } catch (Exception e)
@@ -820,11 +820,11 @@
                   {
                      Array.set(a, i, getAsValue(pe, values[i]));
                   }
-                  m.invoke(obj, new Object[]{a});
+                  InjectionUtils.invoke(m, obj, new Object[]{a});
                } else
                {
                   Object realValue = getAsValue(pe, values[0]);
-                  m.invoke(obj, new Object[]{realValue});
+                  InjectionUtils.invoke(m, obj, new Object[]{realValue});
                }
             } catch (NumberFormatException nfe)
             {   //need this for Sun's Editor's
@@ -877,7 +877,7 @@
             throw new IllegalArgumentException("No property editor registered for this type"); // No editor for this parameter type
          }
          Object realValue = getAsValue(pe, values[0]);
-         m.invoke(obj, new Object[]{index, realValue});
+         InjectionUtils.invoke(m, obj, new Object[]{index, realValue});
       } catch (IllegalArgumentException iae)
       {
          throw iae;
@@ -912,11 +912,11 @@
          Method m = descriptor.getIndexedWriteMethod();
          if ((descriptor.getIndexedPropertyType().equals(String.class)) || (descriptor.getIndexedPropertyType().equals(Object.class)))
          {
-            m.invoke(obj, new Object[]{index, values[0]});
+            InjectionUtils.invoke(m, obj, new Object[]{index, values[0]});
             return true;
          } else if (descriptor.getIndexedPropertyType().equals(String[].class))
          {
-            m.invoke(obj, new Object[]{index, values});
+            InjectionUtils.invoke(m, obj, new Object[]{index, values});
             return true;
          }
       } catch (Exception e)

Index: ValueStack.java
===================================================================
RCS file: /cvsroot/opensymphony/webwork/src/main/webwork/util/ValueStack.java,v
retrieving revision 1.59
retrieving revision 1.60
diff -u -d -r1.59 -r1.60
--- ValueStack.java	31 May 2004 23:14:19 -0000	1.59
+++ ValueStack.java	4 Jun 2004 07:57:25 -0000	1.60
@@ -25,8 +25,8 @@
  * values available by using the push and pop methods. They can then
  * be accessed by using the find* methods.
  *
- *	@author Rickard Öberg ([email protected])
- * @author Maurice C. Parker ([email protected])
+ *	@author Rickard Öberg ([email protected])
+ *  @author Maurice C. Parker ([email protected])
  *	@version $Revision$
  */
 public class ValueStack{
@@ -346,7 +346,7 @@
                            //log.debug( "PROPERTY: method not found: '" + segment.getId() + "' current value: '" + value + "' (" + value.getClass() + ")");
                            break objectWalk;
                         } else {
-                           value = methods[0].getMethod().invoke(value, null);
+                           value = InjectionUtils.invoke(methods[0].getMethod(),value, null);
                            gotResult = true;
                            //log.debug( "PROPERTY: found property value: " + value  + " (" + value.getClass() + ")");
                         }
@@ -426,7 +426,7 @@
                               }
                            }
                         }
-                        value = target.getMethod().invoke(value, params);
+                        value = InjectionUtils.invoke(target.getMethod(), value, params);
                         gotResult = true;
                      } catch (IllegalArgumentException e) {
                         log.error("Illegal parameters invoking " + value.getClass() + "." + target.getMethod().getName() + "(" +Arrays.asList(getParameterClasses(params)) + ")");



-------------------------------------------------------
This SF.Net email is sponsored by the new InstallShield X.
From Windows to Linux, servers to mobile, InstallShield X is the one
installation-authoring solution that does it all. Learn more and
evaluate today! http://www.installshield.com/Dev2Dev/0504