KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > 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.jboss.ejb3.callback;
8
9 import javax.ejb.CallbackListener;
10
11 import org.jboss.aop.Advisor;
12
13 import java.lang.reflect.Method JavaDoc;
14
15 /**
16  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
17  * @version $Revision: 1.2.2.1 $
18  */

19 public class CallbackResolver
20 {
21    public static Callback resolveCallback(Class JavaDoc beanClass, Class JavaDoc annotation, Class JavaDoc listener) throws Exception JavaDoc
22    {
23       Callback callback = null;
24       Method JavaDoc[] 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 JavaDoc returnType = methods[i].getReturnType();
34                Class JavaDoc[] args = methods[i].getParameterTypes();
35                if (returnType != Void.TYPE && args.length != 0)
36                {
37                   throw new RuntimeException JavaDoc("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 JavaDoc("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 JavaDoc returnType = methods[i].getReturnType();
61                      Class JavaDoc[] args = methods[i].getParameterTypes();
62                      if (returnType != Void.TYPE && args.length != 0)
63                      {
64                         throw new RuntimeException JavaDoc("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 JavaDoc("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 JavaDoc e)
77          {
78             throw new RuntimeException JavaDoc(e.getCause());
79          }
80       }
81
82       return callback;
83    }
84    
85    public static Callback resolveCallback(Advisor beanAdvisor, Class JavaDoc beanClass, Class JavaDoc annotation, Class JavaDoc listener) throws Exception JavaDoc
86    {
87       Callback callback = null;
88       Method JavaDoc[] 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 JavaDoc returnType = methods[i].getReturnType();
98                Class JavaDoc[] args = methods[i].getParameterTypes();
99                if (returnType != Void.TYPE && args.length != 0)
100                {
101                   throw new RuntimeException JavaDoc("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 JavaDoc("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 JavaDoc returnType = methods[i].getReturnType();
125                      Class JavaDoc[] args = methods[i].getParameterTypes();
126                      if (returnType != Void.TYPE && args.length != 0)
127                      {
128                         throw new RuntimeException JavaDoc("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 JavaDoc("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 JavaDoc e)
141          {
142             throw new RuntimeException JavaDoc(e.getCause());
143          }
144       }
145
146       return callback;
147    }
148 }
149
Popular Tags