1 7 package com.tirsen.nanning.definition; 8 9 import java.lang.reflect.Method ; 10 import java.util.HashMap ; 11 import java.util.HashSet ; 12 import java.util.Map ; 13 import java.util.Set ; 14 15 import com.tirsen.nanning.*; 16 17 27 public class InterceptorDefinition { 28 private final Class interceptorClass; 29 private Interceptor singletonInterceptor; 30 private Map mapAttributes; 31 private Set negativeCache = new HashSet (); 32 private Set positiveCache = new HashSet (); 33 34 public InterceptorDefinition(Class interceptorClass) { 35 if (!Interceptor.class.isAssignableFrom(interceptorClass)) { 36 throw new IllegalArgumentException (interceptorClass + " is not an interceptor."); 37 } 38 this.interceptorClass = interceptorClass; 39 } 40 41 public Interceptor newInstance() { 42 if (singletonInterceptor != null) { 43 return singletonInterceptor; 44 } 45 try { 46 Interceptor instance = (Interceptor) interceptorClass.newInstance(); 47 if (SingletonInterceptor.class.isAssignableFrom(interceptorClass)) { 48 singletonInterceptor = instance; 49 } 50 51 if (instance instanceof DefinitionAwareInterceptor) { 52 ((DefinitionAwareInterceptor) instance).setInterceptorDefinition(this); 53 } 54 return instance; 55 } catch (Exception e) { 56 throw new AspectException(e); 57 } 58 } 59 60 public Class getInterceptorClass() { 61 return interceptorClass; 62 } 63 64 public void setAttribute(String name, Object value) { 65 if (mapAttributes == null) mapAttributes = new HashMap (); 66 mapAttributes.put(name, value); 67 } 68 69 public Object getAttribute(String name) { 70 if (mapAttributes == null) { 71 return null; 72 } else { 73 return mapAttributes.get(name); 74 } 75 } 76 77 public boolean interceptsMethod(Method method) { 78 if (negativeCache.contains(method)) { 79 return false; 80 } 81 if (positiveCache.contains(method)) { 82 return true; 83 } 84 85 Interceptor interceptor = newInstance(); 86 if (interceptor instanceof FilterMethodsInterceptor) { 87 if (((FilterMethodsInterceptor) interceptor).interceptsMethod(method)) { 88 positiveCache.add(method); 89 return true; 90 } 91 } else if (interceptor instanceof MethodInterceptor) { 92 positiveCache.add(method); 93 return true; 94 } 95 negativeCache.add(interceptor); 96 return false; 97 } 98 99 public boolean interceptsConstructor(Class interfaceClass) { 100 Interceptor interceptor = newInstance(); 101 if (interceptor instanceof ConstructionInterceptor) { 102 return ((ConstructionInterceptor) interceptor).interceptsConstructor(interfaceClass); 103 } 104 return false; 105 } 106 107 public Interceptor getSingleton() { 108 if (singletonInterceptor == null) { 109 newInstance(); 110 } 111 if (singletonInterceptor == null) { 112 throw new IllegalStateException ("This is not a singleton-interceptor: " + interceptorClass); 113 } 114 115 return singletonInterceptor; 116 } 117 } 118 | Popular Tags |