You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by xu...@apache.org on 2011/05/03 08:19:05 UTC

svn commit: r1098934 - in /openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic: EnterpriseBeanBuilder.java MethodInfoUtil.java MethodScheduleBuilder.java

Author: xuhaihong
Date: Tue May  3 06:19:04 2011
New Revision: 1098934

URL: http://svn.apache.org/viewvc?rev=1098934&view=rev
Log:
OPENEJB-1515 NPE in MethodScheduleBuilder.build(BeanContext, EnterpriseBeanInfo) (Patch from Shawn Jiang)

Modified:
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EnterpriseBeanBuilder.java
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/MethodInfoUtil.java
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/MethodScheduleBuilder.java

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EnterpriseBeanBuilder.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EnterpriseBeanBuilder.java?rev=1098934&r1=1098933&r2=1098934&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EnterpriseBeanBuilder.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EnterpriseBeanBuilder.java Tue May  3 06:19:04 2011
@@ -173,7 +173,7 @@ class EnterpriseBeanBuilder {
             StatefulBeanInfo statefulBeanInfo = (StatefulBeanInfo) bean;
 
             for (InitMethodInfo init : statefulBeanInfo.initMethods) {
-                Method beanMethod = toMethod(ejbClass, init.beanMethod);
+                Method beanMethod = MethodInfoUtil.toMethod(ejbClass, init.beanMethod);
                 List<Method> methods = new ArrayList<Method>();
 
                 if (home != null) methods.addAll(Arrays.asList(home.getMethods()));
@@ -191,7 +191,7 @@ class EnterpriseBeanBuilder {
             }
 
             for (RemoveMethodInfo removeMethod : statefulBeanInfo.removeMethods) {
-                Method method = toMethod(ejbClass, removeMethod.beanMethod);
+                Method method = MethodInfoUtil.toMethod(ejbClass, removeMethod.beanMethod);
                 deployment.getRemoveMethods().add(method);
                 deployment.setRetainIfExeption(method, removeMethod.retainIfException);
             }
@@ -276,7 +276,7 @@ class EnterpriseBeanBuilder {
         //we could directly check the matching bean method.
         if (ejbType == BeanType.STATELESS || ejbType == BeanType.SINGLETON || ejbType == BeanType.STATEFUL) {
             for (NamedMethodInfo methodInfo : bean.asynchronous) {
-                Method method = toMethod(ejbClass, methodInfo);
+                Method method = MethodInfoUtil.toMethod(ejbClass, methodInfo);
                 deployment.getMethodContext(deployment.getMatchingBeanMethod(method)).setAsynchronous(true);
             }
             for (String className : bean.asynchronousClasses) {
@@ -349,9 +349,9 @@ class EnterpriseBeanBuilder {
         try {
             if (TimedObject.class.isAssignableFrom(ejbClass)) {
                 timeout = ejbClass.getMethod("ejbTimeout", Timer.class);
-            } else if (info.methodParams != null) {
+            } else {
                 try {
-                    timeout = toMethod(ejbClass, info);
+                    timeout = MethodInfoUtil.toMethod(ejbClass, info);
                 } catch (IllegalStateException e) {
                     //Spec 18.2.5.3 [102] For the compatibility of timeout method signature, if method-params is  not set, it is also required to search the method signaure below :
                     //void <METHOD> (Timer timer)
@@ -360,13 +360,13 @@ class EnterpriseBeanBuilder {
                     // not get 'Callback method does not exist' runtime exceptions and instead
                     // get a validation failure.  Then we can explicitly add the (Timer) param
                     // if the fallback method does exist.
-                    if (info.methodParams.size() == 0) {
+                    if (info.methodParams == null) {
                         NamedMethodInfo candidateInfo = new NamedMethodInfo();
                         candidateInfo.className = info.className;
                         candidateInfo.id = info.id;
                         candidateInfo.methodName = info.methodName;
                         candidateInfo.methodParams = Arrays.asList(Timer.class.getName());
-                        timeout = toMethod(ejbClass, candidateInfo);
+                        timeout = MethodInfoUtil.toMethod(ejbClass, candidateInfo);
                     }
                 }
             }
@@ -377,36 +377,6 @@ class EnterpriseBeanBuilder {
         return timeout;
     }
 
-    private Method toMethod(Class clazz, NamedMethodInfo info) {
-        List<Class> parameterTypes = new ArrayList<Class>();
-
-        if (info.methodParams != null){
-            for (String paramType : info.methodParams) {
-                try {
-                    parameterTypes.add(Classes.forName(paramType, clazz.getClassLoader()));
-                } catch (ClassNotFoundException cnfe) {
-                    throw new IllegalStateException("Parameter class could not be loaded for type " + paramType, cnfe);
-                }
-            }
-        }
-
-        Class[] parameters = parameterTypes.toArray(new Class[parameterTypes.size()]);
-
-        IllegalStateException noSuchMethod = null;
-        while (clazz != null) {
-            try {
-                return clazz.getDeclaredMethod(info.methodName, parameters);
-            } catch (NoSuchMethodException e) {
-                if (noSuchMethod == null) {
-                    noSuchMethod = new IllegalStateException("Callback method does not exist: " + clazz.getName() + "." + info.methodName, e);
-                }
-                clazz = clazz.getSuperclass();
-            }
-        }
-
-        throw noSuchMethod;
-    }
-
 
     private Class loadClass(String className, String messageCode) throws OpenEJBException {
         Class clazz = load(className, messageCode);

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/MethodInfoUtil.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/MethodInfoUtil.java?rev=1098934&r1=1098933&r2=1098934&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/MethodInfoUtil.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/MethodInfoUtil.java Tue May  3 06:19:04 2011
@@ -17,13 +17,17 @@
 package org.apache.openejb.assembler.classic;
 
 import org.apache.openejb.BeanContext;
+import org.apache.openejb.util.Classes;
 import org.apache.openejb.util.Join;
+import org.apache.openejb.util.SetAccessible;
 
 import javax.ejb.EJBLocalObject;
 import javax.ejb.EJBObject;
 import javax.ejb.EJBHome;
 import javax.ejb.EJBLocalHome;
+
 import static java.util.Arrays.asList;
+
 import java.util.Comparator;
 import java.util.List;
 import java.util.ArrayList;
@@ -37,6 +41,51 @@ import java.lang.reflect.Method;
  * @version $Rev$ $Date$
  */
 public class MethodInfoUtil {
+    
+    
+    /**
+     * Finds the nearest java.lang.reflect.Method with the given NamedMethodInfo
+     * Callbacks can be private so class.getMethod() cannot be used.  Searching
+     * starts by looking in the specified class, if the method is not found searching continues with
+     * the immediate parent and continues recurssively until the method is found or java.lang.Object
+     * is reached.  If the method is not found a IllegalStateException is thrown.
+     *
+     * @param clazz
+     * @param methodName
+     * @param parameterTypes
+     * @return
+     * @throws IllegalStateException if the method is not found in this class or any of its parent classes
+     */
+    public static Method toMethod(Class clazz, NamedMethodInfo info) {
+        List<Class> parameterTypes = new ArrayList<Class>();
+
+        if (info.methodParams != null){
+            for (String paramType : info.methodParams) {
+                try {
+                    parameterTypes.add(Classes.forName(paramType, clazz.getClassLoader()));
+                } catch (ClassNotFoundException cnfe) {
+                    throw new IllegalStateException("Parameter class could not be loaded for type " + paramType, cnfe);
+                }
+            }
+        }
+
+        Class[] parameters = parameterTypes.toArray(new Class[parameterTypes.size()]);
+
+        IllegalStateException noSuchMethod = null;
+        while (clazz != null) {
+            try {
+                Method method = clazz.getDeclaredMethod(info.methodName, parameters);
+                return SetAccessible.on(method);
+            } catch (NoSuchMethodException e) {
+                if (noSuchMethod == null) {
+                    noSuchMethod = new IllegalStateException("Callback method does not exist: " + clazz.getName() + "." + info.methodName, e);
+                }
+                clazz = clazz.getSuperclass();
+            }
+        }
+
+        throw noSuchMethod;
+    }    
 
     public static List<Method> matchingMethods(Method signature, Class clazz) {
         List<Method> list = new ArrayList<Method>();

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/MethodScheduleBuilder.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/MethodScheduleBuilder.java?rev=1098934&r1=1098933&r2=1098934&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/MethodScheduleBuilder.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/MethodScheduleBuilder.java Tue May  3 06:19:04 2011
@@ -17,14 +17,12 @@
 package org.apache.openejb.assembler.classic;
 
 import org.apache.openejb.BeanContext;
+import org.apache.openejb.MethodContext;
 import org.apache.openejb.util.Logger;
 import org.apache.openejb.util.LogCategory;
-import org.apache.openejb.util.Classes;
-import org.apache.openejb.util.SetAccessible;
 import org.apache.openejb.core.timer.ScheduleData;
 import javax.ejb.ScheduleExpression;
 import javax.ejb.TimerConfig;
-import java.util.List;
 import java.lang.reflect.Method;
 
 public class MethodScheduleBuilder {
@@ -32,84 +30,72 @@ public class MethodScheduleBuilder {
     public static final Logger logger = Logger.getInstance(LogCategory.OPENEJB_STARTUP, MethodScheduleBuilder.class.getPackage().getName());
 
     public void build(BeanContext beanContext, EnterpriseBeanInfo beanInfo) {
+        
         Class<?> clazz = beanContext.getBeanClass();
-
+        
         for (MethodScheduleInfo info : beanInfo.methodScheduleInfos) {
-            Method method;
-            try {
-                method = getMethod(clazz, info.method.methodName, toClasses(info.method.methodParams, clazz.getClassLoader()));
-            } catch (NoSuchMethodException e) {
-                // method doesn't exist
-                logger.warning("Schedule method does not exist: "+info.method.methodName, e);
-                continue;
-            } catch (ClassNotFoundException e) {
-                logger.warning("Schedule method param cannot be loaded.", e);
-                continue;
-            }
-
-            if (info.method.className == null || method.getDeclaringClass().getName().equals(info.method.className)){
+            
+            Method timeoutMethodOfSchedule = null;
+            
+            if (info.method.methodParams == null) {
+                
+                logger.info("Schedule timeout method with 'null' method parameters is invalid: " + info.method.methodName);
+
+            } else {
+
+                try {
+                    timeoutMethodOfSchedule = MethodInfoUtil.toMethod(clazz, info.method);
+                } catch (IllegalStateException e) {
+                    // method doesn't exist
+                    logger.warning("Schedule method does not exist: " + info.method.methodName, e);
+                    continue;
+                }
 
-                for (ScheduleInfo scheduleInfo : info.schedules) {
+            }
+            
+            MethodContext methodContext = null;
+            
+            if (timeoutMethodOfSchedule == null && beanContext.getEjbTimeout() != null) {
+                methodContext = beanContext.getMethodContext(beanContext.getEjbTimeout());
+            } else if (info.method.className == null
+                    || timeoutMethodOfSchedule.getDeclaringClass().getName().equals(info.method.className)) {
 
-                    ScheduleExpression expr = new ScheduleExpression();
-                    expr.second(scheduleInfo.second == null ? "0" : scheduleInfo.second);
-                    expr.minute(scheduleInfo.minute == null ? "0" : scheduleInfo.minute);
-                    expr.hour(scheduleInfo.hour == null ? "0" : scheduleInfo.hour);
-                    expr.dayOfWeek(scheduleInfo.dayOfWeek == null ? "*" : scheduleInfo.dayOfWeek);
-                    expr.dayOfMonth(scheduleInfo.dayOfMonth == null ? "*" : scheduleInfo.dayOfMonth);
-                    expr.month(scheduleInfo.month == null ? "*" : scheduleInfo.month);
-                    expr.year(scheduleInfo.year == null ? "*" : scheduleInfo.year);
-                    expr.timezone(scheduleInfo.timezone);
-                    expr.start(scheduleInfo.start);
-                    expr.end(scheduleInfo.end);
-
-                    TimerConfig config = new TimerConfig();
-                    config.setInfo(scheduleInfo.info);
-                    config.setPersistent(scheduleInfo.persistent);
+                methodContext = beanContext.getMethodContext(timeoutMethodOfSchedule);
 
-                    beanContext.getMethodContext(method).getSchedules().add(new ScheduleData(config, expr));
-                }
             }
+            
+            this.addSchedulesToMethod(methodContext, info);
         }
     }
-
-    private Class<?>[] toClasses(List<String> params, ClassLoader classLoader) throws ClassNotFoundException {
-        Class<?>[] paramsArray = new Class[params.size()];
-        for (int j = 0; j < paramsArray.length; j++) {
-            String methodParam = params.get(j);
-            paramsArray[j] = Classes.forName(methodParam, classLoader);
-
+    
+    private void addSchedulesToMethod(MethodContext methodContext, MethodScheduleInfo info){
+        
+        if (methodContext == null) {
+            return;
         }
-        return paramsArray;
-    }
+        
+        for (ScheduleInfo scheduleInfo : info.schedules) {
 
+            ScheduleExpression expr = new ScheduleExpression();
+            expr.second(scheduleInfo.second == null ? "0" : scheduleInfo.second);
+            expr.minute(scheduleInfo.minute == null ? "0" : scheduleInfo.minute);
+            expr.hour(scheduleInfo.hour == null ? "0" : scheduleInfo.hour);
+            expr.dayOfWeek(scheduleInfo.dayOfWeek == null ? "*" : scheduleInfo.dayOfWeek);
+            expr.dayOfMonth(scheduleInfo.dayOfMonth == null ? "*" : scheduleInfo.dayOfMonth);
+            expr.month(scheduleInfo.month == null ? "*" : scheduleInfo.month);
+            expr.year(scheduleInfo.year == null ? "*" : scheduleInfo.year);
+            expr.timezone(scheduleInfo.timezone);
+            expr.start(scheduleInfo.start);
+            expr.end(scheduleInfo.end);
+
+            TimerConfig config = new TimerConfig();
+            config.setInfo(scheduleInfo.info);
+            config.setPersistent(scheduleInfo.persistent);
 
-
-    /**
-     * Finds the nearest java.lang.reflect.Method with the given
-     * name and parameters.  Callbacks can be private so class.getMethod() cannot be used.  Searching
-     * starts by looking in the specified class, if the method is not found searching continues with
-     * the immediate parent and continues recurssively until the method is found or java.lang.Object
-     * is reached.  If the method is not found a NoSuchMethodException is thrown.
-     *
-     * @param clazz
-     * @param methodName
-     * @param parameterTypes
-     * @return
-     * @throws NoSuchMethodException if the method is not found in this class or any of its parent classes
-     */
-    private Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException {
-        NoSuchMethodException original = null;
-        while (clazz != null){
-            try {
-                Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
-                return SetAccessible.on(method);
-            } catch (NoSuchMethodException e) {
-                if (original == null) original = e;
-            }
-            clazz = clazz.getSuperclass();
+            methodContext.getSchedules().add(new ScheduleData(config, expr));
         }
-        throw original;
+        
     }
 
+
 }