KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ejb > callback > CallbackResolver


1 /*
2  * JBoss, the OpenSource EJB server
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.hibernate.ejb.callback;
8
9 import java.lang.reflect.Method JavaDoc;
10
11 /**
12  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
13  * @version $Revision: 1.4 $
14  */

15 public final class CallbackResolver {
16     private CallbackResolver() {}
17
18     public static Callback resolveCallback(Class JavaDoc beanClass, Class JavaDoc annotation, Class JavaDoc listener) throws Exception JavaDoc {
19         Callback callback = null;
20         Method JavaDoc[] methods = beanClass.getDeclaredMethods();
21
22         for ( int i = 0 ; i < methods.length ; i++ ) {
23             if ( methods[i].getAnnotation( annotation ) != null ) {
24                 if ( callback == null ) {
25                     callback = new BeanCallback( methods[i] );
26                     Class JavaDoc returnType = methods[i].getReturnType();
27                     Class JavaDoc[] args = methods[i].getParameterTypes();
28                     if ( returnType != Void.TYPE && args.length != 0 ) {
29                         throw new RuntimeException JavaDoc(
30                                 "Callback methods annotated on the bean class must return void and take no arguments: " + annotation.getName() + " - " + methods[i]
31                         );
32                     }
33                 }
34                 else {
35                     throw new RuntimeException JavaDoc(
36                             "You can only annotate one callback method with "
37                             + annotation.getName() + " in bean class: " + beanClass.getName()
38                     );
39                 }
40             }
41         }
42
43         if ( listener != null ) {
44             try {
45                 methods = listener.getDeclaredMethods();
46                 for ( int i = 0 ; i < methods.length ; i++ ) {
47                     if ( methods[i].getAnnotation( annotation ) != null ) {
48                         if ( callback == null ) {
49                             callback = new ListenerCallback( methods[i], listener.newInstance() );
50                             Class JavaDoc returnType = methods[i].getReturnType();
51                             Class JavaDoc[] args = methods[i].getParameterTypes();
52                             if ( returnType != Void.TYPE && args.length != 0 ) {
53                                 throw new RuntimeException JavaDoc(
54                                         "Callback methods annotated in a listener bean class must return void and take one argument: " + annotation.getName() + " - " + methods[i]
55                                 );
56                             }
57                         }
58                         else {
59                             throw new RuntimeException JavaDoc(
60                                     "You can only annotate one callback method with "
61                                     + annotation.getName() + " in bean class: " + beanClass.getName() + " and callback listener: "
62                                     + listener.getName()
63                             );
64                         }
65                     }
66                 }
67             }
68             catch (Exception JavaDoc e) {
69                 throw new RuntimeException JavaDoc( e.getCause() );
70             }
71         }
72
73         return callback;
74     }
75
76 }
77
Popular Tags