1 22 package org.jboss.aop; 23 24 import java.io.Serializable ; 25 import java.lang.ref.WeakReference ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import java.util.WeakHashMap ; 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 44 public class InstanceAdvisorDelegate implements Serializable 45 { 46 private static final long serialVersionUID = -5421366346785427537L; 47 48 protected transient WeakReference classAdvisor; 49 InstanceAdvisor instanceAdvisor; 50 protected transient WeakHashMap aspects; 51 protected transient WeakHashMap joinpointAspects; 52 protected SimpleMetaData metadata; 53 54 55 public InstanceAdvisorDelegate(Advisor classAdvisor, InstanceAdvisor instanceAdvisor) 56 { 57 this.instanceAdvisor = instanceAdvisor; 58 this.classAdvisor = new WeakReference (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; Set defs = getClassAdvisor().getPerInstanceAspectDefinitions(); 87 if (instanceAdvisor instanceof Advisor) 88 { 89 Advisor ia = (Advisor)instanceAdvisor; 90 Set instanceDefs = ia.getPerInstanceAspectDefinitions(); 91 defs.addAll(instanceDefs); 92 } 93 if (defs.size() > 0) 94 { 95 aspects = new WeakHashMap (); 96 Iterator it = defs.iterator(); 97 while (it.hasNext()) 98 { 99 AspectDefinition def = (AspectDefinition) it.next(); 100 Object 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; Map jpAspects = getClassAdvisor().getPerInstanceJoinpointAspectDefinitions(); 111 if (instanceAdvisor instanceof Advisor) 112 { 113 Advisor ia = (Advisor)instanceAdvisor; 114 Map instanceJpAspects = ia.getPerInstanceJoinpointAspectDefinitions(); 115 jpAspects.putAll(instanceJpAspects); 116 } 117 118 if (jpAspects.size() > 0) 119 { 120 joinpointAspects = new WeakHashMap (); 121 Iterator 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 joinpoints = (Set ) jpAspects.get(def); 128 Iterator jps = joinpoints.iterator(); 129 while (jps.hasNext()) 130 { 131 Object joinpoint = jps.next(); 132 joins.put(joinpoint, def.getFactory().createPerJoinpoint(getClassAdvisor(), instanceAdvisor, (Joinpoint) joinpoint)); 133 } 134 } 135 } 136 } 137 138 public Object getPerInstanceAspect(String def) 139 { 140 Iterator 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 getPerInstanceAspect(AspectDefinition def) 149 { 150 if (aspects == null) 152 { 153 initializeAspects(); 154 return aspects.get(def); 155 } 156 Object aspect = aspects.get(def); 157 if (aspect == null) 158 { 159 synchronized (this) { 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 copy = new WeakHashMap (aspects); 169 copy.put(def, aspect); 170 aspects = copy; 171 } 172 } 173 } 174 return aspect; 175 } 176 177 public Object getPerInstanceJoinpointAspect(Joinpoint joinpoint, AspectDefinition def) 178 { 179 if (joinpointAspects == null) 181 { 182 initializeJoinpointAspects(); 183 return getJoinpointAspect(def, joinpoint); 184 } 185 Object aspect = getJoinpointAspect(def, joinpoint); 186 if (aspect == null) 187 { 188 synchronized (this) { 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 copy = new WeakHashMap (joinpointAspects); 198 Map map = (Map ) 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 getJoinpointAspect(AspectDefinition def, Joinpoint joinpoint) 212 { 213 if (joinpointAspects == null) return null; 214 Map map = (Map ) joinpointAspects.get(def); 215 Object aspect = map.get(joinpoint); 216 return aspect; 217 } 218 219 public SimpleMetaData getMetaData() 220 { 221 if (metadata == null) 222 { 223 synchronized (this) { 225 if (metadata == null) 226 { 227 metadata = new SimpleMetaData(); 228 } 229 } 230 } 231 return metadata; 232 } 233 234 } 235 | Popular Tags |