1 22 package org.jboss.aop; 23 24 import org.jboss.aop.advice.AdviceStack; 25 import org.jboss.aop.advice.AspectDefinition; 26 import org.jboss.aop.advice.Interceptor; 27 import org.jboss.aop.advice.InterceptorFactory; 28 import org.jboss.aop.joinpoint.Joinpoint; 29 import org.jboss.aop.metadata.SimpleMetaData; 30 31 import java.lang.ref.WeakReference ; 32 import java.util.ArrayList ; 33 import java.util.Arrays ; 34 import java.util.Iterator ; 35 import java.util.WeakHashMap ; 36 37 43 public class ClassInstanceAdvisor implements InstanceAdvisor, java.io.Serializable 44 { 45 static final long serialVersionUID = -3057976129116723527L; 46 47 protected ArrayList insertedInterceptors = null; 48 protected ArrayList appendedInterceptors = null; 49 protected WeakReference instanceRef; 50 protected transient WeakReference classAdvisorRef; 51 public boolean hasInstanceAspects = false; 52 55 private InterceptorChainObserver interceptorChainObserver; 56 InstanceAdvisorDelegate delegate; 57 public ClassInstanceAdvisor() 58 { 59 delegate = new InstanceAdvisorDelegate(null, this); 60 delegate.initialize(); 61 } 62 63 public ClassInstanceAdvisor(Object obj) 64 { 65 this.instanceRef = new WeakReference (obj); 66 if (obj instanceof Advised) 67 { 68 Advised advised = (Advised) obj; 69 Advisor advizor = advised._getAdvisor(); 70 setAdvisorAndInitialise(advizor); 71 } 72 } 73 74 public ClassInstanceAdvisor(Advisor advizor) 75 { 76 setAdvisorAndInitialise(advizor); 77 } 78 79 private void setAdvisorAndInitialise(Advisor advizor) 80 { 81 this.classAdvisorRef = new WeakReference (advizor); 82 83 if (advizor instanceof ClassAdvisor) 84 { 85 delegate = new InstanceAdvisorDelegate(advizor, this); 86 delegate.initialize(); 87 this.interceptorChainObserver = ((ClassAdvisor) advizor).getInterceptorChainObserver(); 88 } 89 } 90 91 public boolean hasInterceptors() 92 { 93 return appendedInterceptors != null || insertedInterceptors != null; 94 } 95 public Object getPerInstanceAspect(String def) 96 { 97 return delegate.getPerInstanceAspect(def); 98 } 99 100 public Object getPerInstanceAspect(AspectDefinition def) 101 { 102 return delegate.getPerInstanceAspect(def); 103 } 104 105 public Object getPerInstanceJoinpointAspect(Joinpoint joinpoint, AspectDefinition def) 106 { 107 return delegate.getPerInstanceJoinpointAspect(joinpoint, def); 108 } 109 110 public SimpleMetaData getMetaData() 111 { 112 return delegate.getMetaData(); 113 } 114 115 public Interceptor[] getInterceptors() 116 { 117 ArrayList newlist = new ArrayList (); 118 if (insertedInterceptors != null) newlist.addAll(insertedInterceptors); 119 if (appendedInterceptors != null) newlist.addAll(appendedInterceptors); 120 return (Interceptor[]) newlist.toArray(new Interceptor[newlist.size()]); 121 } 122 123 126 public Interceptor[] getInterceptors(Interceptor[] advisorChain) 127 { 128 if (insertedInterceptors == null && appendedInterceptors == null) return advisorChain; 129 ArrayList newlist = new ArrayList (); 130 if (insertedInterceptors != null) newlist.addAll(insertedInterceptors); 131 if (advisorChain != null) 132 { 133 newlist.addAll(Arrays.asList(advisorChain)); 134 } 135 if (appendedInterceptors != null) newlist.addAll(appendedInterceptors); 136 return (Interceptor[]) newlist.toArray(new Interceptor[newlist.size()]); 137 } 138 139 public void insertInterceptor(int index, Interceptor interceptor) 140 { 141 ArrayList newList = new ArrayList (); 142 if (insertedInterceptors != null) 143 { 144 newList.addAll(insertedInterceptors); 145 } 146 newList.add(index, interceptor); 147 insertedInterceptors = newList; 148 hasInstanceAspects = true; 149 if (interceptorChainObserver != null) 150 { 151 interceptorChainObserver.instanceInterceptorAdded(this); 152 } 153 } 154 155 public void insertInterceptor(Interceptor interceptor) 156 { 157 ArrayList newList = new ArrayList (); 158 if (insertedInterceptors != null) 159 { 160 newList.addAll(insertedInterceptors); 161 } 162 newList.add(interceptor); 163 insertedInterceptors = newList; 164 hasInstanceAspects = true; 165 if (interceptorChainObserver != null) 166 { 167 interceptorChainObserver.instanceInterceptorAdded(this); 168 } 169 } 170 171 public void appendInterceptor(Interceptor interceptor) 172 { 173 ArrayList newList = new ArrayList (); 174 if (appendedInterceptors != null) 175 { 176 newList.addAll(appendedInterceptors); 177 } 178 newList.add(interceptor); 179 appendedInterceptors = newList; 180 hasInstanceAspects = true; 181 if (interceptorChainObserver != null) 182 { 183 interceptorChainObserver.instanceInterceptorAdded(this); 184 } 185 } 186 187 public void appendInterceptor(int index, Interceptor interceptor) 188 { 189 ArrayList newList = new ArrayList (); 190 if (appendedInterceptors != null) 191 { 192 newList.addAll(appendedInterceptors); 193 } 194 newList.add(index, interceptor); 195 appendedInterceptors = newList; 196 hasInstanceAspects = true; 197 if (interceptorChainObserver != null) 198 { 199 interceptorChainObserver.instanceInterceptorAdded(this); 200 } 201 } 202 203 206 public void removeInterceptor(String name) 207 { 208 int interceptorsRemoved = internalRemoveInterceptor(name); 209 if (interceptorChainObserver != null) 210 { 211 interceptorChainObserver.instanceInterceptorsRemoved(this, interceptorsRemoved); 212 } 213 } 214 215 219 private int internalRemoveInterceptor(String name) 220 { 221 int interceptorsRemoved = 0; 222 if (insertedInterceptors != null) 223 { 224 for (int i = 0; i < insertedInterceptors.size(); i++) 225 { 226 Interceptor interceptor = (Interceptor) insertedInterceptors.get(i); 227 if (interceptor.getName().equals(name)) 228 { 229 ArrayList newList = new ArrayList (); 230 newList.addAll(insertedInterceptors); 231 newList.remove(i); 232 insertedInterceptors = newList; 233 interceptorsRemoved ++; 234 } 235 } 236 } 237 if (appendedInterceptors != null) 238 { 239 for (int i = 0; i < appendedInterceptors.size(); i++) 240 { 241 Interceptor interceptor = (Interceptor) appendedInterceptors.get(i); 242 if (interceptor.getName().equals(name)) 243 { 244 ArrayList newList = new ArrayList (); 245 newList.addAll(appendedInterceptors); 246 newList.remove(i); 247 appendedInterceptors = newList; 248 interceptorsRemoved ++; 249 } 250 } 251 } 252 hasInstanceAspects = ((insertedInterceptors != null && insertedInterceptors.size() > 0) 253 || (appendedInterceptors != null && appendedInterceptors.size() > 0)); 254 return interceptorsRemoved; 255 } 256 257 public final boolean hasAspects() 258 { 259 return hasInstanceAspects; 260 } 261 262 public void insertInterceptorStack(String stackName) 263 { 264 AdviceStack stack = AspectManager.instance().getAdviceStack(stackName); 265 if (stack == null) throw new RuntimeException ("Stack " + stackName + " not found."); 266 267 ClassAdvisor classAdvisor = null; 268 if (getInstance() instanceof Advised) 269 { 270 Advised advised = (Advised) getInstance(); 271 classAdvisor = ((ClassAdvisor) advised._getAdvisor()); 272 } 273 int interceptorsAdded = 0; 274 Iterator it = stack.getInterceptorFactories().iterator(); 275 while (it.hasNext()) 276 { 277 InterceptorFactory factory = (InterceptorFactory) it.next(); 278 if (!factory.isDeployed()) continue; 279 Interceptor interceptor = factory.create(classAdvisor, null); 280 insertInterceptor(interceptor); 281 interceptorsAdded ++; 282 } 283 if (interceptorChainObserver != null) 284 { 285 this.interceptorChainObserver.instanceInterceptorsAdded(this, interceptorsAdded); 286 } 287 } 288 289 public void appendInterceptorStack(String stackName) 290 { 291 AdviceStack stack = AspectManager.instance().getAdviceStack(stackName); 292 if (stack == null) throw new RuntimeException ("Stack " + stackName + " not found."); 293 294 ClassAdvisor classAdvisor = null; 295 if (getInstance() instanceof Advised) 296 { 297 Advised advised = (Advised) getInstance(); 298 classAdvisor = ((ClassAdvisor) advised._getAdvisor()); 299 } 300 int interceptorsAdded = 0; 301 Iterator it = stack.getInterceptorFactories().iterator(); 302 while (it.hasNext()) 303 { 304 InterceptorFactory factory = (InterceptorFactory) it.next(); 305 if (!factory.isDeployed()) continue; 306 Interceptor interceptor = factory.create(classAdvisor, null); 307 appendInterceptor(interceptor); 308 interceptorsAdded ++; 309 } 310 if (interceptorChainObserver != null) 311 { 312 this.interceptorChainObserver.instanceInterceptorsAdded(this, interceptorsAdded); 313 } 314 } 315 316 public void removeInterceptorStack(String stackName) 317 { 318 AdviceStack stack = AspectManager.instance().getAdviceStack(stackName); 319 if (stack == null) throw new RuntimeException ("Stack " + stackName + " not found."); 320 321 ClassAdvisor classAdvisor = null; 322 if (getInstance() instanceof Advised) 323 { 324 Advised advised = (Advised) getInstance(); 325 classAdvisor = ((ClassAdvisor) advised._getAdvisor()); 326 } 327 int interceptorsRemoved = 0; 328 Iterator it = stack.getInterceptorFactories().iterator(); 329 while (it.hasNext()) 330 { 331 InterceptorFactory factory = (InterceptorFactory) it.next(); 332 if (!factory.isDeployed()) continue; 333 Interceptor interceptor = factory.create(classAdvisor, null); 334 interceptorsRemoved += internalRemoveInterceptor(interceptor.getName()); 335 } 336 if (interceptorChainObserver != null) 337 { 338 this.interceptorChainObserver.instanceInterceptorsRemoved(this, interceptorsRemoved); 339 } 340 } 341 342 343 344 public Domain getDomain() 345 { 346 throw new RuntimeException ("getDomain() is only available when you use weaving with generated advisors"); 347 } 348 349 352 protected void finalize() 353 { 354 ClassLoader classLoader = getClassAdvisor().getClazz().getClassLoader(); 355 if (this.interceptorChainObserver == null || !getClassAdvisor().getManager().getRegisteredCLs().containsKey(classLoader)) 356 { 357 return; 358 } 359 this.interceptorChainObserver.allInstanceInterceptorsRemoved(this); 360 } 361 362 private Advisor getClassAdvisor() 363 { 364 if (classAdvisorRef != null) 365 { 366 return (Advisor)classAdvisorRef.get(); 367 } 368 return null; 369 } 370 371 private Object getInstance() 372 { 373 if (instanceRef != null) 374 { 375 return (Advisor)instanceRef.get(); 376 } 377 return null; 378 } 379 } | Popular Tags |