1 7 package org.jboss.ejb3.callback; 8 9 import javax.ejb.CallbackListener; 10 11 import org.jboss.aop.Advisor; 12 13 import java.lang.reflect.Method ; 14 15 19 public class CallbackResolver 20 { 21 public static Callback resolveCallback(Class beanClass, Class annotation, Class listener) throws Exception 22 { 23 Callback callback = null; 24 Method [] methods = beanClass.getDeclaredMethods(); 25 26 for (int i = 0 ; i < methods.length ; i++) 27 { 28 if (methods[i].getAnnotation(annotation) != null) 29 { 30 if (callback == null) 31 { 32 callback = new BeanCallback(methods[i]); 33 Class returnType = methods[i].getReturnType(); 34 Class [] args = methods[i].getParameterTypes(); 35 if (returnType != Void.TYPE && args.length != 0) 36 { 37 throw new RuntimeException ("Callback methods annotated on the bean class must return void and take no arguments: " + annotation.getName() + " - " + methods[i]); 38 } 39 } 40 else 41 { 42 throw new RuntimeException ("You can only annotate one callback method with " 43 + annotation.getName() + " in bean class: " + beanClass.getName()); 44 } 45 } 46 } 47 48 if (listener != null) 49 { 50 try 51 { 52 methods = listener.getDeclaredMethods(); 53 for (int i = 0 ; i < methods.length ; i++) 54 { 55 if (methods[i].getAnnotation(annotation) != null) 56 { 57 if (callback == null) 58 { 59 callback = new ListenerCallback(methods[i], listener.newInstance()); 60 Class returnType = methods[i].getReturnType(); 61 Class [] args = methods[i].getParameterTypes(); 62 if (returnType != Void.TYPE && args.length != 0) 63 { 64 throw new RuntimeException ("Callback methods annotated in a listener bean class must return void and take one argument: " + annotation.getName() + " - " + methods[i]); 65 } 66 } 67 else 68 { 69 throw new RuntimeException ("You can only annotate one callback method with " 70 + annotation.getName() + " in bean class: " + beanClass.getName() + " and callback listener: " 71 + listener.getName()); 72 } 73 } 74 } 75 } 76 catch(Exception e) 77 { 78 throw new RuntimeException (e.getCause()); 79 } 80 } 81 82 return callback; 83 } 84 85 public static Callback resolveCallback(Advisor beanAdvisor, Class beanClass, Class annotation, Class listener) throws Exception 86 { 87 Callback callback = null; 88 Method [] methods = beanClass.getDeclaredMethods(); 89 90 for (int i = 0 ; i < methods.length ; i++) 91 { 92 if ( (methods[i].getAnnotation(annotation) != null) || (beanAdvisor.resolveAnnotation(methods[i], annotation) != null)) 93 { 94 if (callback == null) 95 { 96 callback = new BeanCallback(methods[i]); 97 Class returnType = methods[i].getReturnType(); 98 Class [] args = methods[i].getParameterTypes(); 99 if (returnType != Void.TYPE && args.length != 0) 100 { 101 throw new RuntimeException ("Callback methods annotated on the bean class must return void and take no arguments: " + annotation.getName() + " - " + methods[i]); 102 } 103 } 104 else 105 { 106 throw new RuntimeException ("You can only annotate one callback method with " 107 + annotation.getName() + " in bean class: " + beanClass.getName()); 108 } 109 } 110 } 111 112 if (listener != null) 113 { 114 try 115 { 116 methods = listener.getDeclaredMethods(); 117 for (int i = 0 ; i < methods.length ; i++) 118 { 119 if (methods[i].getAnnotation(annotation) != null) 120 { 121 if (callback == null) 122 { 123 callback = new ListenerCallback(methods[i], listener.newInstance()); 124 Class returnType = methods[i].getReturnType(); 125 Class [] args = methods[i].getParameterTypes(); 126 if (returnType != Void.TYPE && args.length != 0) 127 { 128 throw new RuntimeException ("Callback methods annotated in a listener bean class must return void and take one argument: " + annotation.getName() + " - " + methods[i]); 129 } 130 } 131 else 132 { 133 throw new RuntimeException ("You can only annotate one callback method with " 134 + annotation.getName() + " in bean class: " + beanClass.getName() + " and callback listener: " 135 + listener.getName()); 136 } 137 } 138 } 139 } 140 catch(Exception e) 141 { 142 throw new RuntimeException (e.getCause()); 143 } 144 } 145 146 return callback; 147 } 148 } 149 | Popular Tags |