1 22 package org.jboss.aop.advice; 23 24 import java.io.StringReader ; 25 import java.lang.ref.WeakReference ; 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.Collection ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 import org.jboss.aop.Advisor; 34 import org.jboss.aop.AspectManager; 35 import org.jboss.aop.pointcut.Pointcut; 36 import org.jboss.aop.pointcut.PointcutExpression; 37 import org.jboss.aop.pointcut.ast.ASTCFlowExpression; 38 import org.jboss.aop.pointcut.ast.ParseException; 39 import org.jboss.aop.pointcut.ast.PointcutExpressionParser; 40 41 47 public class AdviceBinding 48 { 49 private static volatile long counter = 0; 50 51 protected String name; 52 protected Pointcut pointcut; 53 protected ASTCFlowExpression cflow; 54 protected String cflowString; 55 56 protected Collection advisors = new HashSet (); 58 protected InterceptorFactory[] interceptorFactories = new InterceptorFactory[0]; 59 60 public AdviceBinding() {} 61 62 public AdviceBinding(String name, Pointcut p, ASTCFlowExpression cflow, String cflowString, InterceptorFactory[] factories) throws ParseException 63 { 64 this.name = name; 65 interceptorFactories = factories; 66 this.cflow = cflow; 67 68 pointcut = p; 69 this.cflowString = cflowString; 70 } 71 72 79 public AdviceBinding(String pointcutExpression, String cflow) throws ParseException 80 { 81 this(Long.toString(System.currentTimeMillis()) + ":" + Long.toString(counter++), pointcutExpression, cflow); 82 } 83 84 91 public AdviceBinding(String name, String pointcutExpression, String cflow) throws ParseException 92 { 93 this.name = name; 94 setPointcutExpression(pointcutExpression); 95 setCFlowExpression(cflow); 96 interceptorFactories = new InterceptorFactory[0]; 97 } 98 99 public void setCFlowExpression(String cflow) 100 throws ParseException 101 { 102 if (cflow != null) 103 { 104 cflowString = cflow; 105 this.cflow = new PointcutExpressionParser(new StringReader (cflowString)).CFlowExpression(); 106 } 107 } 108 109 public void setPointcutExpression(String pointcutExpression) 110 throws ParseException 111 { 112 pointcut = new PointcutExpression(Long.toString(System.currentTimeMillis()) + ":" + Long.toString(counter++), pointcutExpression); 113 } 114 115 public void addInterceptorFactory(InterceptorFactory factory) 116 { 117 List list = Arrays.asList(interceptorFactories); 118 list = new ArrayList (list); 119 list.add(factory); 120 interceptorFactories = (InterceptorFactory[]) list.toArray(new InterceptorFactory[list.size()]); 121 } 122 123 124 131 public void addInterceptor(Class clazz) 132 { 133 addInterceptorFactory(new GenericInterceptorFactory(clazz)); 134 } 135 136 public String getName() 137 { 138 return name; 139 } 140 141 public InterceptorFactory[] getInterceptorFactories() 142 { 143 return interceptorFactories; 144 } 145 146 public void setName(String name) 147 { 148 this.name = name; 149 } 150 151 public void addAdvisor(Advisor advisor) 152 { 153 if (AspectManager.verbose) System.out.println("[debug] added advisor: " + advisor.getName() + " from binding: " + name); 154 synchronized (advisors) 157 { 158 if (advisors.size() > 0) 159 { 160 Iterator it = advisors.iterator(); 161 while (it.hasNext()) 162 { 163 WeakReference ref = (WeakReference ) it.next(); 164 Object obj = ref.get(); 165 if (obj == null) it.remove(); 166 else if(obj.equals(advisor)) 167 { 168 return; } 170 } 171 } 172 advisors.add(new WeakReference (advisor)); 173 } 174 175 } 176 177 public boolean hasAdvisors() 178 { 179 return advisors.size() > 0; 180 } 181 182 public ArrayList getAdvisors() 183 { 184 ArrayList list = new ArrayList (advisors.size()); 185 synchronized (advisors) 186 { 187 Iterator it = advisors.iterator(); 188 while (it.hasNext()) 189 { 190 WeakReference ref = (WeakReference ) it.next(); 191 Object advisor = ref.get(); 192 if (advisor != null) 193 { 194 list.add(advisor); 195 } 196 else 197 { 198 it.remove(); 199 } 200 } 201 } 202 return list; 203 } 204 205 public void clearAdvisors() 206 { 207 synchronized (advisors) 208 { 209 for (Iterator it = advisors.iterator(); it.hasNext();) 210 { 211 WeakReference ref = (WeakReference ) it.next(); 212 Object obj = ref.get(); 213 if (obj != null) 214 { 215 Advisor advisor = (Advisor) obj; 216 if (advisor.getManager().isAdvisorRegistered(advisor)) 217 { 218 advisor.removeAdviceBinding(this); 219 } 220 } 221 } 222 advisors.clear(); 223 } 224 } 225 226 public boolean equals(Object obj) 227 { 228 if (obj == this) return true; 229 if (!(obj instanceof AdviceBinding)) return false; 230 return ((AdviceBinding) obj).getName().equals(name); 231 } 232 233 public int hashCode() 234 { 235 return name.hashCode(); 236 } 237 238 public Pointcut getPointcut() 239 { 240 return pointcut; 241 } 242 243 public ASTCFlowExpression getCFlow() 244 { 245 return cflow; 246 } 247 248 public String getCFlowString() 249 { 250 return cflowString; 251 } 252 } 253 | Popular Tags |