1 22 package org.jboss.aop.metadata; 23 import javassist.CtMethod; 24 import org.jboss.aop.joinpoint.Invocation; 25 import org.jboss.aop.joinpoint.MethodInvocation; 26 import org.jboss.aop.util.PayloadKey; 27 28 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap; 29 30 import java.lang.reflect.Method ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 40 public class MethodMetaData implements MetaDataResolver 41 { 42 Map methodMetaData = new ConcurrentReaderHashMap(); 43 HashMap inexactMatches; 44 45 public boolean hasTag(String group) 46 { 47 Iterator values = methodMetaData.values().iterator(); 48 while (values.hasNext()) 49 { 50 SimpleMetaData map = (SimpleMetaData)values.next(); 51 if (map.hasTag(group)) return true; 52 } 53 return false; 54 } 55 public void tagMethod(Method method, Object tag) 56 { 57 addMethodMetaData(method, tag, EMPTY_TAG, new Object (), PayloadKey.TRANSIENT); 58 } 59 public void addMethodMetaData(Method method, Object tag, Object attr, Object value) 60 { 61 addMethodMetaData(method, tag, attr, value, PayloadKey.MARSHALLED, true); 62 } 63 public void addMethodMetaData(Method method, Object tag, Object attr, Object value, boolean exactMatch) 64 { 65 addMethodMetaData(method, tag, attr, value, PayloadKey.MARSHALLED, exactMatch); 66 } 67 public void addMethodMetaData(Method method, Object tag, Object attr, Object value, PayloadKey type) 68 { 69 addMethodMetaData(method.toString(), tag, attr, value, type, true); 70 } 71 public void addMethodMetaData(Method method, Object tag, Object attr, Object value, PayloadKey type, boolean exactMatch) 72 { 73 addMethodMetaData(method.toString(), tag, attr, value, type, exactMatch); 74 } 75 76 private void addMethodMetaData(String key, Object tag, Object attr, Object value, PayloadKey type, boolean exactMatch) 77 { 78 synchronized (methodMetaData) 79 { 80 SimpleMetaData methodData = (SimpleMetaData)methodMetaData.get(key); 81 if (methodData == null) 82 { 83 methodData = new SimpleMetaData(); 84 methodMetaData.put(key, methodData); 85 } 86 methodData.addMetaData(tag, attr, value, type); 87 } 88 manageInexactMatches(key, tag, attr, exactMatch); 89 } 90 91 private synchronized void manageInexactMatches(String key, Object tag, Object attr, boolean exactMatch) 92 { 93 if (!exactMatch) 94 { 95 if (inexactMatches == null) 96 { 97 inexactMatches = new HashMap(); 98 } 99 100 HashMap tags = (HashMap)inexactMatches.get(key); 101 if (tags == null) 102 { 103 tags = new HashMap(); 104 inexactMatches.put(key, tags); 105 } 106 107 HashMap attrs = (HashMap)tags.get(tag); 108 if (attrs == null) 109 { 110 attrs = new HashMap(); 111 tags.put(tag, attrs); 112 } 113 attrs.put(attr, Boolean.TRUE); 114 } 115 else 116 { 117 if (inexactMatches == null)return; 118 HashMap tags = (HashMap)inexactMatches.get(key); 119 if (tags == null) return; 120 HashMap attrs = (HashMap)tags.get(tag); 121 if (attrs == null) return; 122 attrs.remove(attr); 123 } 124 125 } 126 127 public synchronized boolean tagWasMatchedInexactly(Method method, Object tag, Object attr) 128 { 129 if (inexactMatches == null) return false; 130 HashMap tags = (HashMap)inexactMatches.get(method.toString()); 131 if (tags == null) return false; 132 HashMap attrs = (HashMap)tags.get(tag); 133 if (attrs == null) return false; 134 return (attrs.get(attr) != null); 135 } 136 137 public boolean hasTag(Method method, String tag) 138 { 139 SimpleMetaData meta = getMethodMetaData(method); 140 if (meta == null) return false; 141 return meta.hasTag(tag); 142 } 143 144 public Iterator getMethods() 145 { 146 return methodMetaData.keySet().iterator(); 147 } 148 149 154 public SimpleMetaData getMethodMetaData(String method) 155 { 156 return (SimpleMetaData)methodMetaData.get(method); 157 } 158 159 public SimpleMetaData getMethodMetaData(Method method) 160 { 161 return getMethodMetaData(method.toString()); 162 } 163 164 public Object getMethodMetaData(Method method, Object tag, Object attr) 165 { 166 SimpleMetaData methodData = (SimpleMetaData)methodMetaData.get(method.toString()); 167 if (methodData == null) return null; 168 return methodData.getMetaData(tag, attr); 169 } 170 171 public void clear() 172 { 173 methodMetaData.clear(); 174 } 175 176 public Object resolve(Invocation invocation, Object tag, Object attr) 177 { 178 MethodInvocation methodInvocation = (MethodInvocation)invocation; 179 return getMethodMetaData(methodInvocation.getMethod(), tag, attr); 180 } 181 182 public SimpleMetaData getAllMetaData(Invocation invocation) 183 { 184 MethodInvocation methodInvocation = (MethodInvocation)invocation; 185 return (SimpleMetaData)methodMetaData.get(methodInvocation.getMethod().toString()); 186 } 187 188 191 private String getCtMethodKey(CtMethod method) 192 { 193 return method.getName() + ":" + method.getSignature(); 194 } 195 196 public void tagMethod(CtMethod method, Object tag) 197 { 198 addMethodMetaData(method, tag, EMPTY_TAG, PayloadKey.TRANSIENT); 199 } 200 public void addMethodMetaData(CtMethod method, Object tag, Object attr, Object value) 201 { 202 addMethodMetaData(getCtMethodKey(method), tag, attr, value, PayloadKey.MARSHALLED, true); 203 } 204 public void addMethodMetaData(CtMethod method, Object tag, Object attr, Object value, PayloadKey type) 205 { 206 addMethodMetaData(getCtMethodKey(method), tag, attr, value, type, true); 207 } 208 209 public boolean hasGroup(CtMethod method, String tag) 210 { 211 SimpleMetaData meta = getMethodMetaData(getCtMethodKey(method)); 212 if (meta == null) return false; 213 return meta.hasTag(tag); 214 } 215 216 217 } 218 | Popular Tags |