You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by km...@apache.org on 2010/07/30 01:17:05 UTC

svn commit: r980605 - in /openejb/trunk/openejb3/container/openejb-core/src: main/java/org/apache/openejb/config/ main/java/org/apache/openejb/config/rules/ main/java/org/apache/openejb/util/ test/java/org/apache/openejb/config/rules/

Author: kmalhi
Date: Thu Jul 29 23:17:05 2010
New Revision: 980605

URL: http://svn.apache.org/viewvc?rev=980605&view=rev
Log:
Excellent patch from Ivan Xu for OPENEJB-1314. Thanks Ivan. 

Modified:
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckCallbacks.java
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Join.java
    openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/CheckInvalidTimeoutTest.java

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java?rev=980605&r1=980604&r2=980605&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java Thu Jul 29 23:17:05 2010
@@ -2120,14 +2120,10 @@ public class AnnotationDeployer implemen
                 TimerConsumer timerConsumer = (TimerConsumer) bean;
                 if (timerConsumer.getTimeoutMethod() == null) {
                     List<Method> timeoutMethods = classFinder.findAnnotatedMethods(javax.ejb.Timeout.class);
-                   String componentName = null;
-                    if(timeoutMethods.size() >1){
-                        componentName = timerConsumer.getTimerConsumerName();
-                        fail(componentName,"timeout.tooManyMethods",timeoutMethods.size(),Join.join(",", timeoutMethods));
-                    }else if(timeoutMethods.size() == 1){
+                  //Validation Logic is moved to CheckCallback class.
+                    if(timeoutMethods.size() == 1){
                         timerConsumer.setTimeoutMethod(new NamedMethod(timeoutMethods.get(0)));
-                    }
-                    
+                    }                    
                 }
             }
 

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckCallbacks.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckCallbacks.java?rev=980605&r1=980604&r2=980605&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckCallbacks.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckCallbacks.java Thu Jul 29 23:17:05 2010
@@ -48,6 +48,7 @@ import org.apache.openejb.jee.Session;
 import org.apache.openejb.jee.SessionBean;
 import org.apache.openejb.jee.SessionType;
 import org.apache.openejb.jee.TimerConsumer;
+import org.apache.openejb.util.Join;
 import org.apache.xbean.finder.ClassFinder;
 
 /**
@@ -76,6 +77,8 @@ public class CheckCallbacks extends Vali
                 checkCallback(ejbClass, "PreDestroy", callback, bean);
             }
 
+            ClassFinder finder = new ClassFinder(ejbClass);
+            
             if (bean instanceof Session ) {
                 SessionBean session = (SessionBean) bean;
 
@@ -141,7 +144,6 @@ public class CheckCallbacks extends Vali
                     }
                 }
             } else {
-                ClassFinder finder = new ClassFinder(ejbClass);
 
                 for (Method method : finder.findAnnotatedMethods(PrePassivate.class)) {
                     ignoredAnnotation("PrePassivate", bean, bean.getEjbClass(), method.getName(), bean.getClass().getSimpleName());
@@ -176,6 +178,10 @@ public class CheckCallbacks extends Vali
                 TimerConsumer timerConsumer = (TimerConsumer) bean;
                 checkTimeOut(ejbClass, timerConsumer.getTimeoutMethod(), bean);
 
+                List<Method> timeoutMethods = finder.findAnnotatedMethods(Timeout.class);
+                if (timeoutMethods.size() > 1) {
+                    fail(timerConsumer.getTimerConsumerName(), "timeout.tooManyMethods", timeoutMethods.size(), Join.join(",", timeoutMethods));
+                }
                 for (AroundTimeout aroundTimeout : bean.getAroundTimeout()) {
                     checkAroundTimeout(ejbClass, aroundTimeout, bean.getEjbName());
                 }
@@ -376,43 +382,38 @@ public class CheckCallbacks extends Vali
         }
     }
 
-    private void checkTimeOut(Class ejbClass, NamedMethod timeout, EnterpriseBean bean) {
+    private void checkTimeOut(Class<?> ejbClass, NamedMethod timeout, EnterpriseBean bean) {
         if (timeout == null) return;
         try {
-            Method method = getMethod(ejbClass, timeout.getMethodName(), javax.ejb.Timer.class);
-            Annotation[] declaredAnnotations = method.getDeclaredAnnotations();
-            boolean isAnnotated = false;
-            for (Annotation annotation : declaredAnnotations) {
-                if(annotation instanceof Timeout){
-                    isAnnotated = true;
-                    break;
+            Class<?>[] parameterClasses;
+            if (timeout.getMethodParams() == null) {
+                parameterClasses = new Class[0];
+            } else {
+                List<String> methodParams = timeout.getMethodParams().getMethodParam();
+                parameterClasses = new Class[methodParams.size()];
+                for (int i = 0; i < methodParams.size(); i++) {
+                    parameterClasses[i] = loadClass(methodParams.get(i));
                 }
             }
-            
-            if (isAnnotated) {
-                Class<?> returnType = method.getReturnType();
-                if (!returnType.equals(Void.TYPE)) {
-                    fail(bean, "timeout.badReturnType", timeout.getMethodName(), returnType.getName());
-                }
-            }else{
-                fail(bean, "timeout.missing.possibleTypo", timeout.getMethodName(), getMethods(ejbClass, timeout.getMethodName()).size());
+            Method method = getMethod(ejbClass, timeout.getMethodName(), parameterClasses);
+            Class<?>[] parameterTypes = method.getParameterTypes();
+            if (parameterTypes.length != 0 && (parameterTypes.length != 1 || parameterTypes[0] != javax.ejb.Timer.class)) {
+                fail(bean, "timeout.invalidArguments", timeout.getMethodName(), Join.join(",", Join.CLASS_CALLBACK, parameterClasses));
+            }
+            Class<?> returnType = method.getReturnType();
+            if (!returnType.equals(Void.TYPE)) {
+                fail(bean, "timeout.badReturnType", timeout.getMethodName(), returnType.getName());
             }
-           
         } catch (NoSuchMethodException e) {
             List<Method> possibleMethods = getMethods(ejbClass, timeout.getMethodName());
 
             if (possibleMethods.size() == 0) {
                 fail(bean, "timeout.missing", timeout.getMethodName());
-            } else if (possibleMethods.size() == 1) {
-                fail(bean, "timeout.invalidArguments", timeout.getMethodName(), getParameters(possibleMethods.get(0)));
-                Class<?> returnType = possibleMethods.get(0).getReturnType();
-
-                if (!returnType.equals(Void.TYPE)) {
-                    fail(bean, "timeout.badReturnType", timeout.getMethodName(), returnType.getName());
-                }
             } else {
                 fail(bean, "timeout.missing.possibleTypo", timeout.getMethodName(), possibleMethods.size());
             }
+        } catch(OpenEJBException e){
+            //ignore ? Could not load the class of the parameter
         }
     }
 

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Join.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Join.java?rev=980605&r1=980604&r2=980605&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Join.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Join.java Thu Jul 29 23:17:05 2010
@@ -17,27 +17,78 @@
 package org.apache.openejb.util;
 
 import java.util.Collection;
-import java.util.ArrayList;
+
+import org.springframework.asm.commons.Method;
 
 /**
  * @version $Rev$ $Date$
  */
 public class Join {
+
+    public static final MethodCallback METHOD_CALLBACK = new MethodCallback();
+
+    public static final ClassCallback CLASS_CALLBACK = new ClassCallback();
+
     public static String join(String delimiter, Collection collection) {
+        if(collection.size() == 0) {
+            return "";
+        }
         StringBuilder sb = new StringBuilder();
         for (Object obj : collection) {
             sb.append(obj).append(delimiter);
         }
-        if (collection.size() > 0) sb.delete(sb.length()-delimiter.length(), sb.length());
-        return sb.toString();
+        return  sb.substring(0, sb.length()-delimiter.length());
     }
 
     public static String join(String delimiter, Object... collection) {
+        if(collection.length ==0) {
+            return "";
+        }
         StringBuilder sb = new StringBuilder();
         for (Object obj : collection) {
             sb.append(obj).append(delimiter);
         }
-        if (collection.length > 0) sb.delete(sb.length()-delimiter.length(), sb.length());
-        return sb.toString();
+        return  sb.substring(0, sb.length()-delimiter.length());
+    }
+
+    public static <T> String join(String delimiter, NameCallback<T> nameCallback, T... collection) {
+        if (collection.length == 0) {
+            return "";
+        }
+        StringBuilder sb = new StringBuilder();
+        for (T obj : collection) {
+            sb.append(nameCallback.getName(obj)).append(delimiter);
+        }
+        return sb.substring(0, sb.length() - delimiter.length());
+    }
+
+    public static <T> String join(String delimiter, NameCallback<T> nameCallback, Collection<T> collection) {
+        if (collection.size() == 0) {
+            return "";
+        }
+        StringBuilder sb = new StringBuilder();
+        for (T obj : collection) {
+            sb.append(nameCallback.getName(obj)).append(delimiter);
+        }
+        return sb.substring(0, sb.length() - delimiter.length());
+    }
+
+    public static interface NameCallback<T> {
+
+        public String getName(T object);
+    }
+
+    public static class MethodCallback implements NameCallback<Method> {
+
+        public String getName(Method method) {
+            return method.getName();
+        }
+    }
+
+    public static class ClassCallback implements NameCallback<Class<?>> {
+
+        public String getName(Class<?> cls) {
+            return cls.getName();
+        }
     }
 }

Modified: openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/CheckInvalidTimeoutTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/CheckInvalidTimeoutTest.java?rev=980605&r1=980604&r2=980605&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/CheckInvalidTimeoutTest.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/CheckInvalidTimeoutTest.java Thu Jul 29 23:17:05 2010
@@ -21,6 +21,7 @@ import javax.ejb.Timeout;
 import junit.framework.TestCase;
 
 import org.apache.openejb.jee.EjbJar;
+import org.apache.openejb.jee.NamedMethod;
 import org.apache.openejb.jee.StatelessBean;
 import org.junit.runner.RunWith;
 
@@ -32,7 +33,9 @@ public class CheckInvalidTimeoutTest ext
         EjbJar ejbJar = new EjbJar();
         ejbJar.addEnterpriseBean(new StatelessBean(TestBean.class));
         ejbJar.addEnterpriseBean(new StatelessBean(FooBean.class));
-        ejbJar.addEnterpriseBean(new StatelessBean(BarBean.class));
+        StatelessBean barBean = new StatelessBean(BarBean.class);
+        barBean.setTimeoutMethod(new NamedMethod("foo", "java.lang.String"));
+        ejbJar.addEnterpriseBean(barBean);
         return ejbJar;
     }
 // A case where the class has the method with wrong signature annotated with @Timeout
@@ -40,7 +43,7 @@ public class CheckInvalidTimeoutTest ext
     // timeout.invalidArguments
     public static class TestBean {
         @Timeout
-        public Object bar() {
+        public Object bar(Object m) {
             return null;
         }
     }
@@ -52,14 +55,11 @@ public class CheckInvalidTimeoutTest ext
         @Timeout
         public void bar(javax.ejb.Timer timer) {}
     }
-//  A case where the class has overloaded methods, but the method with the wrong signature has been annotated with @Timeout
+    //  A case where incorrect timeout method is configured in the deployment descriptor
     // timeout.missing.possibleTypo
     public static class BarBean {
-       
+
         public void foo(javax.ejb.Timer timer) {}
-        
-        @Timeout
-        public void foo() {}
     }
 
 }