KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > ClassInstanceAdvisor


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

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 JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Arrays JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.WeakHashMap JavaDoc;
36
37 /**
38  * Holds an object instance's metadata and attached interceptors
39  *
40  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
41  * @version $Revision: 45215 $
42  */

43 public class ClassInstanceAdvisor implements InstanceAdvisor, java.io.Serializable JavaDoc
44 {
45    static final long serialVersionUID = -3057976129116723527L;
46
47    protected ArrayList JavaDoc insertedInterceptors = null;
48    protected ArrayList JavaDoc appendedInterceptors = null;
49    protected WeakReference JavaDoc instanceRef;
50    protected transient WeakReference JavaDoc classAdvisorRef;
51    public boolean hasInstanceAspects = false;
52    /**
53     * aspects is a weak hash map of AspectDefinitions so that perinstance advices can be undeployed/redeployed
54     */

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 JavaDoc obj)
64    {
65       this.instanceRef = new WeakReference JavaDoc(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 JavaDoc(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 JavaDoc getPerInstanceAspect(String JavaDoc def)
96    {
97       return delegate.getPerInstanceAspect(def);
98    }
99
100    public Object JavaDoc getPerInstanceAspect(AspectDefinition def)
101    {
102       return delegate.getPerInstanceAspect(def);
103    }
104
105    public Object JavaDoc 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 JavaDoc newlist = new ArrayList JavaDoc();
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    /**
124     * Called by the advisor
125     */

126    public Interceptor[] getInterceptors(Interceptor[] advisorChain)
127    {
128       if (insertedInterceptors == null && appendedInterceptors == null) return advisorChain;
129       ArrayList JavaDoc newlist = new ArrayList JavaDoc();
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 JavaDoc newList = new ArrayList JavaDoc();
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 JavaDoc newList = new ArrayList JavaDoc();
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 JavaDoc newList = new ArrayList JavaDoc();
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 JavaDoc newList = new ArrayList JavaDoc();
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    /**
204     * This will not remove interceptor pointcuts! You will have to do this through AspectManager
205     */

206    public void removeInterceptor(String JavaDoc name)
207    {
208       int interceptorsRemoved = internalRemoveInterceptor(name);
209       if (interceptorChainObserver != null)
210       {
211          interceptorChainObserver.instanceInterceptorsRemoved(this, interceptorsRemoved);
212       }
213    }
214
215    /**
216     * @param name
217     * @return
218     */

219    private int internalRemoveInterceptor(String JavaDoc 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 JavaDoc newList = new ArrayList JavaDoc();
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 JavaDoc newList = new ArrayList JavaDoc();
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 JavaDoc stackName)
263    {
264       AdviceStack stack = AspectManager.instance().getAdviceStack(stackName);
265       if (stack == null) throw new RuntimeException JavaDoc("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 JavaDoc 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 JavaDoc stackName)
290    {
291       AdviceStack stack = AspectManager.instance().getAdviceStack(stackName);
292       if (stack == null) throw new RuntimeException JavaDoc("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 JavaDoc 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 JavaDoc stackName)
317    {
318       AdviceStack stack = AspectManager.instance().getAdviceStack(stackName);
319       if (stack == null) throw new RuntimeException JavaDoc("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 JavaDoc 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 JavaDoc("getDomain() is only available when you use weaving with generated advisors");
347    }
348
349    /**
350     * Added to notify interceptor chain observer of interceptor chain garbage collection.
351     */

352    protected void finalize()
353    {
354       ClassLoader JavaDoc 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 JavaDoc getInstance()
372    {
373       if (instanceRef != null)
374       {
375          return (Advisor)instanceRef.get();
376       }
377       return null;
378    }
379 }
Popular Tags