1 22 package org.jboss.ejb3.interceptor; 23 24 import java.lang.reflect.Method ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import javax.jms.MessageListener ; 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 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 clazz = MessageListener .class; 57 Method m = clazz.getDeclaredMethod("onMessage", new Class [] {javax.jms.Message .class}); 58 MESSAGE_LISTENER_ON_MESSAGE = MethodHashing.calculateHash(m); 59 } 60 catch (Exception e) 61 { 62 throw new RuntimeException ("Error initialising hash for MessageListener.onMessage()", e); 63 } 64 } 65 66 public String getName() 67 { 68 return getClass().getName(); 69 } 70 71 public Object createPerVM() 72 { 73 throw new RuntimeException ("NOT ALLOWED"); 74 } 75 76 public Object createPerInstance(Advisor advisor, InstanceAdvisor instanceAdvisor) 77 { 78 throw new RuntimeException ("NOT ALLOWED"); 79 } 80 81 public Object createPerJoinpoint(Advisor advisor, InstanceAdvisor instanceAdvisor, Joinpoint jp) 82 { 83 throw new RuntimeException ("NOT ALLOWED"); 84 } 85 86 public Object createPerJoinpoint(Advisor advisor, Joinpoint jp) 87 { 88 if (jp instanceof MethodJoinpoint) 89 { 90 EJBContainer container = (EJBContainer) advisor; 91 Class beanClass = container.getBeanClass(); 92 93 try 94 { 95 Method method =((MethodJoinpoint)jp).getMethod(); 96 if (isBusinessMethod(container, method)) 97 { 98 InterceptorInfo[] infos = container.getInterceptorRepository().getBusinessInterceptors(container, method); 99 Method [] 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 e) 105 { 106 throw new RuntimeException ("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 createPerClass(Advisor advisor) 113 { 114 throw new RuntimeException ("NOT ALLOWED"); 115 } 116 117 private boolean isBusinessMethod(EJBContainer container, Method 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 <Class > businessInterfaces = getBusinessInterfaces(container); 127 for (Class businessInterface : businessInterfaces) 128 { 129 for (Method 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 <Class > getBusinessInterfaces(EJBContainer container) 143 { 144 ArrayList <Class > interfaces = new ArrayList <Class >(); 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 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 [] remotes = ProxyFactoryHelper.getRemoteInterfaces(container); 173 Class [] 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 iface = man.value(); 189 if (iface != null) 190 { 191 interfaces.add(iface); 192 } 193 } 194 195 Class [] implIfaces = container.getBeanClass().getInterfaces(); 196 for (Class 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 |