KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.Serializable JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.WeakHashMap JavaDoc;
30
31 import org.jboss.aop.advice.AspectDefinition;
32 import org.jboss.aop.joinpoint.Joinpoint;
33 import org.jboss.aop.metadata.SimpleMetaData;
34
35 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
36
37 /**
38  * Initialisation and getting of instance and joinpoint aspects needed by the various kinds of
39  * InstanceAdvisor implementations
40  *
41  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
42  * @version $Revision: 43757 $
43  */

44 public class InstanceAdvisorDelegate implements Serializable JavaDoc
45 {
46    private static final long serialVersionUID = -5421366346785427537L;
47    
48    protected transient WeakReference JavaDoc classAdvisor;
49    InstanceAdvisor instanceAdvisor;
50    protected transient WeakHashMap JavaDoc aspects;
51    protected transient WeakHashMap JavaDoc joinpointAspects;
52    protected SimpleMetaData metadata;
53
54
55    public InstanceAdvisorDelegate(Advisor classAdvisor, InstanceAdvisor instanceAdvisor)
56    {
57       this.instanceAdvisor = instanceAdvisor;
58       this.classAdvisor = new WeakReference JavaDoc(classAdvisor);
59    }
60
61    public Advisor getAdvisor()
62    {
63       if (classAdvisor != null)
64       {
65          return (Advisor)classAdvisor.get();
66       }
67       return null;
68    }
69    
70    public void initialize()
71    {
72       initializeAspects();
73       initializeJoinpointAspects();
74    }
75    
76    private Advisor getClassAdvisor()
77    {
78       return getAdvisor();
79    }
80    
81    private synchronized void initializeAspects()
82    {
83       if (getClassAdvisor() == null) return;
84       if (aspects != null) return; // doublecheck I know, but I don't want to do synchronization if not needed
85
//ClassAdvisor cadvisor = (ClassAdvisor) classAdvisor;
86
Set JavaDoc defs = getClassAdvisor().getPerInstanceAspectDefinitions();
87       if (instanceAdvisor instanceof Advisor)
88       {
89          Advisor ia = (Advisor)instanceAdvisor;
90          Set JavaDoc instanceDefs = ia.getPerInstanceAspectDefinitions();
91          defs.addAll(instanceDefs);
92       }
93       if (defs.size() > 0)
94       {
95          aspects = new WeakHashMap JavaDoc();
96          Iterator JavaDoc it = defs.iterator();
97          while (it.hasNext())
98          {
99             AspectDefinition def = (AspectDefinition) it.next();
100             Object JavaDoc aspect = def.getFactory().createPerInstance(getClassAdvisor(), instanceAdvisor);
101             aspects.put(def, aspect);
102          }
103       }
104    }
105
106    private synchronized void initializeJoinpointAspects()
107    {
108       if (getClassAdvisor() == null) return;
109       if (joinpointAspects != null) return; // doublecheck I know, but I don't want to do synchronization if not needed
110
Map JavaDoc jpAspects = getClassAdvisor().getPerInstanceJoinpointAspectDefinitions();
111       if (instanceAdvisor instanceof Advisor)
112       {
113          Advisor ia = (Advisor)instanceAdvisor;
114          Map JavaDoc instanceJpAspects = ia.getPerInstanceJoinpointAspectDefinitions();
115          jpAspects.putAll(instanceJpAspects);
116       }
117       
118       if (jpAspects.size() > 0)
119       {
120          joinpointAspects = new WeakHashMap JavaDoc();
121          Iterator JavaDoc it = jpAspects.keySet().iterator();
122          while (it.hasNext())
123          {
124             AspectDefinition def = (AspectDefinition) it.next();
125             ConcurrentReaderHashMap joins = new ConcurrentReaderHashMap();
126             joinpointAspects.put(def, joins);
127             Set JavaDoc joinpoints = (Set JavaDoc) jpAspects.get(def);
128             Iterator JavaDoc jps = joinpoints.iterator();
129             while (jps.hasNext())
130             {
131                Object JavaDoc joinpoint = jps.next();
132                joins.put(joinpoint, def.getFactory().createPerJoinpoint(getClassAdvisor(), instanceAdvisor, (Joinpoint) joinpoint));
133             }
134          }
135       }
136    }
137    
138    public Object JavaDoc getPerInstanceAspect(String JavaDoc def)
139    {
140       Iterator JavaDoc it = aspects.keySet().iterator();
141       while (it.hasNext())
142       {
143          AspectDefinition d = (AspectDefinition) it.next();
144          if (d.getName().equals(def)) return aspects.get(d);
145       }
146       return null;
147    }
148    public Object JavaDoc getPerInstanceAspect(AspectDefinition def)
149    {
150       // aspects is a weak hash map of AspectDefinitions so that perinstance advices can be undeployed/redeployed
151
if (aspects == null)
152       {
153          initializeAspects();
154          return aspects.get(def);
155       }
156       Object JavaDoc aspect = aspects.get(def);
157       if (aspect == null)
158       {
159          synchronized (this) // doublecheck, but I don't want to synchronize everywhere and dynamic aspects are rare
160
{
161             aspect = aspects.get(def);
162             if (aspect != null) return aspect;
163             if (classAdvisor != null && getClassAdvisor() instanceof ClassAdvisor)
164             {
165                ClassAdvisor cadvisor = (ClassAdvisor) getClassAdvisor();
166                cadvisor.getPerInstanceAspectDefinitions().add(def);
167                aspect = def.getFactory().createPerInstance(null, null);
168                WeakHashMap JavaDoc copy = new WeakHashMap JavaDoc(aspects);
169                copy.put(def, aspect);
170                aspects = copy;
171             }
172          }
173       }
174       return aspect;
175    }
176
177    public Object JavaDoc getPerInstanceJoinpointAspect(Joinpoint joinpoint, AspectDefinition def)
178    {
179       // aspects is a weak hash map of AspectDefinitions so that perinstance advices can be undeployed/redeployed
180
if (joinpointAspects == null)
181       {
182          initializeJoinpointAspects();
183          return getJoinpointAspect(def, joinpoint);
184       }
185       Object JavaDoc aspect = getJoinpointAspect(def, joinpoint);
186       if (aspect == null)
187       {
188          synchronized (this) // doublecheck, but I don't want to synchronize everywhere and dynamic aspects are rare
189
{
190             aspect = getJoinpointAspect(def, joinpoint);
191             if (aspect != null) return aspect;
192             if (classAdvisor != null && getClassAdvisor() instanceof ClassAdvisor)
193             {
194                ClassAdvisor cadvisor = (ClassAdvisor) getClassAdvisor();
195                cadvisor.addPerInstanceJoinpointAspect(joinpoint, def);
196                aspect = def.getFactory().createPerJoinpoint(getClassAdvisor(), instanceAdvisor, joinpoint);
197                WeakHashMap JavaDoc copy = new WeakHashMap JavaDoc(joinpointAspects);
198                Map JavaDoc map = (Map JavaDoc) copy.get(def);
199                if (map == null)
200                {
201                   map = new ConcurrentReaderHashMap();
202                }
203                map.put(joinpoint, aspect);
204                joinpointAspects = copy;
205             }
206          }
207       }
208       return aspect;
209    }
210
211    private Object JavaDoc getJoinpointAspect(AspectDefinition def, Joinpoint joinpoint)
212    {
213       if (joinpointAspects == null) return null;
214       Map JavaDoc map = (Map JavaDoc) joinpointAspects.get(def);
215       Object JavaDoc aspect = map.get(joinpoint);
216       return aspect;
217    }
218    
219    public SimpleMetaData getMetaData()
220    {
221       if (metadata == null)
222       {
223          synchronized (this) // doublecheck :(
224
{
225             if (metadata == null)
226             {
227                metadata = new SimpleMetaData();
228             }
229          }
230       }
231       return metadata;
232    }
233
234 }
235
Popular Tags