KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > interceptor > EJB3InterceptorsFactory


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb3.interceptor;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import javax.jms.MessageListener JavaDoc;
28 import org.jboss.annotation.ejb.Management;
29 import org.jboss.annotation.ejb.Producer;
30 import org.jboss.annotation.ejb.Producers;
31 import org.jboss.aop.Advisor;
32 import org.jboss.aop.InstanceAdvisor;
33 import org.jboss.aop.joinpoint.Joinpoint;
34 import org.jboss.aop.joinpoint.MethodJoinpoint;
35 import org.jboss.ejb3.EJBContainer;
36 import org.jboss.ejb3.ProxyFactoryHelper;
37 import org.jboss.ejb3.mdb.ConsumerContainer;
38 import org.jboss.ejb3.mdb.MDB;
39 import org.jboss.ejb3.service.ServiceContainer;
40 import org.jboss.logging.Logger;
41 import org.jboss.util.MethodHashing;
42
43 /**
44  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
45  * @version $Revision: 39709 $
46  */

47 public class EJB3InterceptorsFactory implements org.jboss.aop.advice.AspectFactory
48 {
49
50    static Logger log = Logger.getLogger(EJB3InterceptorsFactory.class);
51    final static long MESSAGE_LISTENER_ON_MESSAGE;
52    static
53    {
54       try
55       {
56          Class JavaDoc clazz = MessageListener JavaDoc.class;
57          Method JavaDoc m = clazz.getDeclaredMethod("onMessage", new Class JavaDoc[] {javax.jms.Message JavaDoc.class});
58          MESSAGE_LISTENER_ON_MESSAGE = MethodHashing.calculateHash(m);
59       }
60       catch (Exception JavaDoc e)
61       {
62          throw new RuntimeException JavaDoc("Error initialising hash for MessageListener.onMessage()", e);
63       }
64    }
65
66    public String JavaDoc getName()
67    {
68       return getClass().getName();
69    }
70
71    public Object JavaDoc createPerVM()
72    {
73       throw new RuntimeException JavaDoc("NOT ALLOWED");
74    }
75
76    public Object JavaDoc createPerInstance(Advisor advisor, InstanceAdvisor instanceAdvisor)
77    {
78       throw new RuntimeException JavaDoc("NOT ALLOWED");
79    }
80
81    public Object JavaDoc createPerJoinpoint(Advisor advisor, InstanceAdvisor instanceAdvisor, Joinpoint jp)
82    {
83       throw new RuntimeException JavaDoc("NOT ALLOWED");
84    }
85
86    public Object JavaDoc createPerJoinpoint(Advisor advisor, Joinpoint jp)
87    {
88       if (jp instanceof MethodJoinpoint)
89       {
90          EJBContainer container = (EJBContainer) advisor;
91          Class JavaDoc beanClass = container.getBeanClass();
92    
93          try
94          {
95             Method JavaDoc method =((MethodJoinpoint)jp).getMethod();
96             if (isBusinessMethod(container, method))
97             {
98                InterceptorInfo[] infos = container.getInterceptorRepository().getBusinessInterceptors(container, method);
99                Method JavaDoc[] beanAroundInvoke = container.getInterceptorRepository().getBeanClassAroundInvokes(container);
100                log.debug("Bound interceptors for joinpoint: " + method + " - " + infos);
101                return new EJB3InterceptorsInterceptor(infos, beanAroundInvoke);
102             }
103          }
104          catch (RuntimeException JavaDoc e)
105          {
106             throw new RuntimeException JavaDoc("An exception occurred initialising interceptors for " + beanClass + "." + ((MethodJoinpoint)jp).getMethod().getName(), e);
107          }
108       }
109       return new EJB3InterceptorsInterceptor(new InterceptorInfo[0], null);
110    }
111
112    public Object JavaDoc createPerClass(Advisor advisor)
113    {
114       throw new RuntimeException JavaDoc("NOT ALLOWED");
115    }
116
117    private boolean isBusinessMethod(EJBContainer container, Method JavaDoc method)
118    {
119       long hash = MethodHashing.calculateHash(method);
120       if (container instanceof MDB)
121       {
122          return hash == MESSAGE_LISTENER_ON_MESSAGE;
123       }
124       else
125       {
126          ArrayList JavaDoc<Class JavaDoc> businessInterfaces = getBusinessInterfaces(container);
127          for (Class JavaDoc businessInterface : businessInterfaces)
128          {
129             for (Method JavaDoc interfaceMethod : businessInterface.getMethods())
130             {
131                if (MethodHashing.calculateHash(interfaceMethod) == hash)
132                {
133                   return true;
134                }
135             }
136          }
137       }
138       
139       return false;
140    }
141    
142    private ArrayList JavaDoc<Class JavaDoc> getBusinessInterfaces(EJBContainer container)
143    {
144       ArrayList JavaDoc<Class JavaDoc> interfaces = new ArrayList JavaDoc<Class JavaDoc>();
145       if (container instanceof ConsumerContainer)
146       {
147          Producers producers = (Producers)container.resolveAnnotation(Producers.class);
148          if (producers != null)
149          {
150             for (Producer producer : producers.value())
151             {
152                interfaces.add(producer.producer());
153             }
154          }
155          
156          Producer producer = (Producer)container.resolveAnnotation(Producer.class);
157          if (producer != null)
158          {
159             interfaces.add(producer.producer());
160          }
161          
162          for (Class JavaDoc implIf : container.getBeanClass().getInterfaces())
163          {
164             if (implIf.getAnnotation(Producer.class) != null)
165             {
166                interfaces.add(implIf);
167             }
168          }
169       }
170       else
171       {
172          Class JavaDoc[] remotes = ProxyFactoryHelper.getRemoteInterfaces(container);
173          Class JavaDoc[] locals = ProxyFactoryHelper.getLocalInterfaces(container);
174          if (remotes != null)
175          {
176             interfaces.addAll(Arrays.asList(remotes));
177          }
178          if (locals != null)
179          {
180             interfaces.addAll(Arrays.asList(locals));
181          }
182          
183          if (container instanceof ServiceContainer)
184          {
185             Management man = (Management)container.resolveAnnotation(Management.class);
186             if (man != null)
187             {
188                Class JavaDoc iface = man.value();
189                if (iface != null)
190                {
191                   interfaces.add(iface);
192                }
193             }
194             
195             Class JavaDoc[] implIfaces = container.getBeanClass().getInterfaces();
196             for (Class JavaDoc iface : implIfaces)
197             {
198                if (iface.getAnnotation(Management.class) != null)
199                {
200                   interfaces.add(iface);
201                }
202             }
203          }
204       }
205       
206       return interfaces;
207    }
208    
209 }
210
Popular Tags