1 22 package org.jboss.ejb3.interceptor; 23 24 import java.lang.reflect.Method ; 25 import java.lang.reflect.Modifier ; 26 27 import javax.interceptor.InvocationContext; 28 import javax.ejb.PostActivate ; 29 import javax.annotation.PostConstruct; 30 import javax.annotation.PreDestroy; 31 import javax.ejb.PrePassivate ; 32 import javax.ejb.Timeout ; 33 import javax.ejb.Timer ; 34 35 import org.jboss.ejb3.BeanContext; 36 import org.jboss.ejb3.EJBContainer; 37 import org.jboss.ejb3.stateful.StatefulBeanContext; 38 import org.jboss.util.MethodHashing; 39 import org.jboss.logging.Logger; 40 41 46 public class LifecycleInterceptorHandler 47 { 48 private static final Logger log = Logger.getLogger(LifecycleInterceptorHandler.class); 49 50 private EJBContainer container; 51 private InterceptorInfo[] postConstructs; 52 private InterceptorInfo[] postActivates; 53 private InterceptorInfo[] prePassivates; 54 private InterceptorInfo[] preDestroys; 55 private Method [] beanPostConstructs; 56 private Method [] beanPostActivates; 57 private Method [] beanPrePassivates; 58 private Method [] beanPreDestroys; 59 private Method timeoutCallbackMethod; 60 private long timeoutCalllbackHash; 61 62 public LifecycleInterceptorHandler(EJBContainer container, Class [] handledCallbacks) 63 { 64 this.container = container; 65 InterceptorInfoRepository repostitory = container.getInterceptorRepository(); 66 for (Class clazz : handledCallbacks) 67 { 68 if (clazz == PostConstruct.class) 69 { 70 postConstructs = repostitory.getPostConstructInterceptors(container); 71 beanPostConstructs = repostitory.getBeanClassPostConstructs(container); 72 } 73 else if (clazz == PostActivate .class) 74 { 75 postActivates = repostitory.getPostActivateInterceptors(container); 76 beanPostActivates = repostitory.getBeanClassPostActivates(container); 77 } 78 else if (clazz == PrePassivate .class) 79 { 80 prePassivates = repostitory.getPrePassivateInterceptors(container); 81 beanPrePassivates = repostitory.getBeanClassPrePassivates(container); 82 } 83 else if (clazz == PreDestroy.class) 84 { 85 preDestroys = repostitory.getPreDestroyInterceptors(container); 86 beanPreDestroys = repostitory.getBeanClassPreDestroys(container); 87 } 88 else if (clazz == Timeout .class) 89 { 90 resolveTimeoutCallback(); 91 } 92 } 93 } 94 95 public long getTimeoutCalllbackHash() 96 { 97 return timeoutCalllbackHash; 98 } 99 100 public void postConstruct(BeanContext ctx) 101 { 102 try 103 { 104 InvocationContext ic = LifecycleInvocationContextImpl.getLifecycleInvocationContext( 105 PostConstruct.class, 106 ctx, 107 postConstructs, 108 beanPostConstructs); 109 ic.proceed(); 110 } 111 catch (Exception e) 112 { 113 throw new RuntimeException (e); 114 } 115 } 116 117 public void preDestroy(BeanContext ctx) 118 { 119 Object id = null; 120 if (ctx instanceof StatefulBeanContext) 121 { 122 id = ((StatefulBeanContext)ctx).getId(); 123 } 124 try 125 { 126 InvocationContext ic = LifecycleInvocationContextImpl.getLifecycleInvocationContext( 127 PreDestroy.class, 128 ctx, 129 preDestroys, 130 beanPreDestroys); 131 ic.proceed(); 132 } 133 catch (Exception e) 134 { 135 throw new RuntimeException (e); 136 } 137 } 138 139 public void postActivate(BeanContext ctx) 140 { 141 try 142 { 143 InvocationContext ic = LifecycleInvocationContextImpl.getLifecycleInvocationContext( 144 PostActivate .class, 145 ctx, 146 postActivates, 147 beanPostActivates); 148 ic.proceed(); 149 } 150 catch (Exception e) 151 { 152 throw new RuntimeException (e); 153 } 154 } 155 156 public void prePassivate(BeanContext ctx) 157 { 158 try 159 { 160 InvocationContext ic = LifecycleInvocationContextImpl.getLifecycleInvocationContext( 161 PrePassivate .class, 162 ctx, 163 prePassivates, 164 beanPrePassivates); 165 ic.proceed(); 166 } 167 catch (Exception e) 168 { 169 throw new RuntimeException (e); 170 } 171 } 172 173 public Method getTimeoutCallback() 174 { 175 return timeoutCallbackMethod; 176 } 177 178 179 private void resolveTimeoutCallback() 180 { 181 for (Method method : container.getBeanClass().getMethods()) 182 { 183 if (container.resolveAnnotation(method, Timeout .class) != null) 184 { 185 if (Modifier.isPublic(method.getModifiers()) && 186 method.getReturnType().equals(Void.TYPE) && 187 method.getParameterTypes().length == 1 && 188 method.getParameterTypes()[0].equals(Timer .class)) 189 { 190 timeoutCallbackMethod = method; 191 } 192 else 193 { 194 throw new RuntimeException ("@Timeout methods must have the signature: public void <METHOD>(javax.ejb.Timer timer) - " + method); 195 } 196 } 197 } 198 199 try 200 { 201 if (timeoutCallbackMethod == null && javax.ejb.TimedObject .class.isAssignableFrom(container.getBeanClass())) 202 { 203 Class [] params = new Class []{Timer .class}; 204 timeoutCallbackMethod = container.getBeanClass().getMethod("ejbTimeout", params); 205 } 206 } 207 catch (Exception e) 208 { 209 e.printStackTrace(); 210 } 211 212 if (timeoutCallbackMethod != null) 213 { 214 timeoutCalllbackHash = MethodHashing.calculateHash(timeoutCallbackMethod); 215 } 216 } 217 218 } 219 | Popular Tags |