1 22 package org.jboss.ejb3.interceptor; 23 24 import java.lang.reflect.Method ; 25 import java.security.AccessController ; 26 import java.security.PrivilegedActionException ; 27 import java.security.PrivilegedExceptionAction ; 28 import java.util.ArrayList ; 29 30 import org.jboss.ejb3.metamodel.Interceptor; 31 32 39 public class InterceptorInfo 40 { 41 Class clazz; 42 Interceptor xml; 43 44 protected Method aroundInvoke; 46 protected Method postConstruct; 47 protected Method postActivate; 48 protected Method preDestroy; 49 protected Method prePassivate; 50 51 protected Method [] aroundInvokeHierarchy; 53 protected Method [] postConstructHierarchy; 54 protected Method [] postActivateHierarchy; 55 protected Method [] preDestroyHierarchy; 56 protected Method [] prePassivateHierarchy; 57 58 boolean haveCalculatedHierarchy; 59 60 protected InterceptorInfo() 61 { 62 } 63 64 public InterceptorInfo(Class clazz) 65 { 66 this.clazz = clazz; 67 } 68 69 public InterceptorInfo(InterceptorInfo interceptorInfo) 70 { 71 this.clazz = interceptorInfo.clazz; 72 this.aroundInvoke = interceptorInfo.aroundInvoke; 73 this.postConstruct = interceptorInfo.postConstruct; 74 this.postActivate = interceptorInfo.postActivate; 75 this.preDestroy = interceptorInfo.preDestroy; 76 this.prePassivate = interceptorInfo.prePassivate; 77 this.aroundInvokeHierarchy = interceptorInfo.aroundInvokeHierarchy; 78 this.postConstructHierarchy = interceptorInfo.postConstructHierarchy; 79 this.postActivateHierarchy = interceptorInfo.postActivateHierarchy; 80 this.preDestroyHierarchy = interceptorInfo.preDestroyHierarchy; 81 this.prePassivateHierarchy = interceptorInfo.prePassivateHierarchy; 82 } 83 84 protected void setXml(Interceptor xml) 85 { 86 this.xml = xml; 87 } 88 89 public Interceptor getXml() 90 { 91 return xml; 92 } 93 94 public Method getAroundInvoke() 95 { 96 return aroundInvoke; 97 } 98 99 protected void setAroundInvoke(Method aroundInvoke) 100 { 101 if (aroundInvoke == null) return; 102 103 if (this.aroundInvoke != null && !this.aroundInvoke.equals(aroundInvoke)) 104 { 105 throw new RuntimeException ("Interceptors can only have one around-invoke/@AroundInvoke method - " + clazz.getName()); 106 } 107 this.aroundInvoke = makeAccessible(aroundInvoke); 108 } 109 110 public Class getClazz() 111 { 112 return clazz; 113 } 114 115 public boolean haveCalculatedHierarchy() 116 { 117 return haveCalculatedHierarchy; 118 } 119 120 public Method getPostActivate() 121 { 122 return postActivate; 123 } 124 125 protected void setPostActivate(Method postActivate) 126 { 127 if (postActivate == null) return; 128 129 if (this.postActivate != null && !this.postActivate.equals(postActivate)) 130 { 131 throw new RuntimeException ("Interceptors can only have one post-activate/@PostActivate method - " + clazz.getName()); 132 } 133 this.postActivate = makeAccessible(postActivate); 134 } 135 136 public Method getPostConstruct() 137 { 138 return postConstruct; 139 } 140 141 protected void setPostConstruct(Method postConstruct) 142 { 143 if (postConstruct == null) return; 144 145 if (this.postConstruct != null && !this.postConstruct.equals(postConstruct)) 146 { 147 throw new RuntimeException ("Interceptors can only have one post-construct/@PostConstruct method - " + clazz.getName()); 148 } 149 this.postConstruct = makeAccessible(postConstruct); 150 } 151 152 public Method getPreDestroy() 153 { 154 return preDestroy; 155 } 156 157 protected void setPreDestroy(Method preDestroy) 158 { 159 if (preDestroy == null) return; 160 161 if (this.preDestroy != null && !this.preDestroy.equals(preDestroy)) 162 { 163 throw new RuntimeException ("Interceptors can only have one pre-destroy/@PreDestroy method - " + clazz.getName()); 164 } 165 this.preDestroy = makeAccessible(preDestroy); 166 } 167 168 public Method getPrePassivate() 169 { 170 return prePassivate; 171 } 172 173 protected void setPrePassivate(Method prePassivate) 174 { 175 if (prePassivate == null) return; 176 177 if (this.prePassivate != null && !this.prePassivate.equals(prePassivate)) 178 { 179 throw new RuntimeException ("Interceptors can only have one pre-passivate/@PrePassivate method - " + clazz.getName()); 180 } 181 this.prePassivate = makeAccessible(prePassivate); 182 } 183 184 public Method [] getAroundInvokes() 185 { 186 return aroundInvokeHierarchy; 187 } 188 189 public Method [] getPostActivates() 190 { 191 return postActivateHierarchy; 192 } 193 194 public Method [] getPostConstructs() 195 { 196 return postConstructHierarchy; 197 } 198 199 public Method [] getPreDestroys() 200 { 201 return preDestroyHierarchy; 202 } 203 204 public Method [] getPrePassivates() 205 { 206 return prePassivateHierarchy; 207 } 208 209 private Method makeAccessible(final Method method) 210 { 211 try 212 { 213 AccessController.doPrivileged(new PrivilegedExceptionAction () { 214 public Object run() 215 { 216 method.setAccessible(true); 217 return null; 218 } 219 }); 220 } 221 catch (PrivilegedActionException e) 222 { 223 throw new RuntimeException (e.getException()); 224 } 225 226 return method; 227 } 228 229 public String toString() 230 { 231 StringBuffer sb = new StringBuffer ("InterceptorInfo{class=" + clazz); 232 appendMethods(sb); 233 sb.append("}"); 234 return sb.toString(); 235 } 236 237 protected void appendMethods(StringBuffer sb) 238 { 239 appendMethodString(sb, "aroundInvoke", aroundInvoke); 240 appendMethodString(sb, "postConstruct", postConstruct); 241 appendMethodString(sb, "postActivate", postActivate); 242 appendMethodString(sb, "prePassivate", prePassivate); 243 appendMethodString(sb, "preDestroy", preDestroy); 244 } 245 246 protected void appendMethodString(StringBuffer buf, String methodType, Method m) 247 { 248 if (m != null) 249 { 250 buf.append(", " + methodType + "=" + m.getName()); 251 } 252 } 253 254 public void calculateHierarchy(InterceptorInfo superInfo) 255 { 256 if (haveCalculatedHierarchy) 257 { 258 return; 259 } 260 261 postConstructHierarchy = initaliseMethods((superInfo != null) ? superInfo.postConstructHierarchy : null, postConstruct); 262 postActivateHierarchy = initaliseMethods((superInfo != null) ? superInfo.postActivateHierarchy : null, postActivate); 263 aroundInvokeHierarchy = initaliseMethods((superInfo != null) ? superInfo.aroundInvokeHierarchy : null, aroundInvoke); 264 prePassivateHierarchy = initaliseMethods((superInfo != null) ? superInfo.prePassivateHierarchy : null, prePassivate); 265 preDestroyHierarchy = initaliseMethods((superInfo != null) ? superInfo.preDestroyHierarchy : null, preDestroy); 266 267 haveCalculatedHierarchy = true; 268 } 269 270 private Method [] initaliseMethods(Method [] superMethods, Method myMethod) 271 { 272 if (superMethods == null && myMethod == null) 273 { 274 return null; 275 } 276 ArrayList hierarchy = new ArrayList (); 277 if (superMethods != null) 278 { 279 for (int i = 0 ; i < superMethods.length ; ++i) 281 { 282 if (!haveMethod(superMethods[i])) 283 { 284 hierarchy.add(superMethods[i]); 285 } 286 } 287 } 288 289 if (myMethod != null) 290 { 291 hierarchy.add(myMethod); 292 } 293 294 return (Method [])hierarchy.toArray(new Method [hierarchy.size()]); 295 } 296 297 private boolean haveMethod(Method method) 298 { 299 try 300 { 301 clazz.getDeclaredMethod(method.getName(), method.getParameterTypes()); 302 return true; 303 } 304 catch (NoSuchMethodException e) 305 { 306 return false; 307 } 308 } 309 310 @Override 311 public boolean equals(Object obj) 312 { 313 if (obj instanceof InterceptorInfo) 314 { 315 return clazz.equals(((InterceptorInfo)obj).getClazz()); 316 } 317 return false; 318 } 319 320 @Override 321 public int hashCode() 322 { 323 return clazz.getName().hashCode(); 324 } 325 326 327 } 328 | Popular Tags |