1 22 package org.jboss.aop.proxy.container; 23 24 import java.io.Serializable ; 25 import java.lang.ref.WeakReference ; 26 import java.util.Arrays ; 27 import java.util.Comparator ; 28 29 31 36 public class ContainerProxyCacheKey implements Serializable 37 { 38 private static final long serialVersionUID = 8758283842273747310L; 39 private static final WeakReference [] EMTPY_WR_ARRAY = new WeakReference [0]; 40 private static final AOPProxyFactoryMixin[] EMPTY_MIXIN_ARRAY = new AOPProxyFactoryMixin[0]; 41 42 private WeakReference clazzRef; 43 private WeakReference [] addedInterfaces = EMTPY_WR_ARRAY; 44 45 private Object metaDataContext; 48 49 private AOPProxyFactoryMixin[] addedMixins = EMPTY_MIXIN_ARRAY; 50 private int hashcode = 0; 51 52 public ContainerProxyCacheKey(Class clazz) 53 { 54 this.clazzRef = new WeakReference (clazz); 55 } 56 57 public ContainerProxyCacheKey(Class clazz, Class [] addedInterfaces, Object metaDataContext) 59 { 60 this(clazz); 61 this.addedInterfaces = ContainerCacheUtil.getSortedWeakReferenceForInterfaces(addedInterfaces); 62 this.metaDataContext = metaDataContext; 63 } 64 65 public ContainerProxyCacheKey(Class clazz, Class [] addedInterfaces, AOPProxyFactoryMixin[] addedMixins, Object metaDataContext) 66 { 67 this(clazz, addedInterfaces, metaDataContext); 68 69 if (addedMixins != null) 70 { 71 this.addedMixins = addedMixins; 72 Arrays.sort(this.addedMixins, MixinAlphabetical.singleton); 73 } 74 } 75 76 public Class getClazz() 77 { 78 Class clazz = (Class )clazzRef.get(); 79 if (clazz != null) 80 { 81 return clazz; 82 } 83 return null; 84 } 85 86 public boolean equals(Object obj) 87 { 88 if (this == obj) 89 { 90 return true; 91 } 92 93 if (obj.getClass() != ContainerProxyCacheKey.class) 94 { 95 return false; 96 } 97 98 ContainerProxyCacheKey other = (ContainerProxyCacheKey)obj; 99 100 if (!compareMetadataContext(other)) 101 { 102 return false; 103 } 104 if (!compareClass(other)) 105 { 106 return false; 107 } 108 if (!compareAddedInterfaces(other)) 109 { 110 return false; 111 } 112 if (!compareAddedMixins(other)) 113 { 114 return false; 115 } 116 117 return true; 118 } 119 120 public int hashCode() 121 { 122 if (hashcode == 0) 123 { 124 125 Class clazz = (Class )clazzRef.get(); 126 StringBuffer sb = new StringBuffer (); 127 128 if (clazz != null) 129 { 130 sb.append(clazz.getName()); 131 } 132 133 if (addedInterfaces != null) 134 { 135 for (int i = 0 ; i < addedInterfaces.length ; i++) 136 { 137 sb.append(";"); 138 sb.append(((Class )addedInterfaces[i].get()).getName()); 139 } 140 } 141 142 hashcode = sb.toString().hashCode(); 143 144 if (metaDataContext != null) 145 { 146 hashcode += metaDataContext.hashCode(); 147 } 148 } 149 150 return hashcode; 151 } 152 153 public String toString() 154 { 155 StringBuffer buf = new StringBuffer ("ContainerProxyCache"); 156 buf.append(((Class )clazzRef.get()).getName()); 157 buf.append(";interfaces="); 158 if (addedInterfaces == null) 159 { 160 buf.append("null"); 161 } 162 else 163 { 164 buf.append(Arrays.asList(addedInterfaces)); 165 } 166 buf.append(";mixins="); 167 if (addedMixins == null) 168 { 169 buf.append("null"); 170 } 171 else 172 { 173 buf.append(Arrays.asList(addedMixins)); 174 } 175 return buf.toString(); 176 } 177 178 private boolean compareMetadataContext(ContainerProxyCacheKey other) 179 { 180 if (this.metaDataContext == null && other.metaDataContext == null) 181 { 182 } 183 else if ((this.metaDataContext != null && other.metaDataContext != null)) 184 { 185 if (!this.metaDataContext.equals(other.metaDataContext)) 186 { 187 return false; 188 } 189 } 190 else 191 { 192 return false; 193 } 194 return true; 195 } 196 197 private boolean compareClass(ContainerProxyCacheKey other) 198 { 199 return ContainerCacheUtil.compareClassRefs(this.clazzRef, other.clazzRef); 200 } 201 202 private boolean compareAddedInterfaces(ContainerProxyCacheKey other) 203 { 204 return ContainerCacheUtil.compareInterfaceRefs(this.addedInterfaces, other.addedInterfaces); 205 } 206 207 private boolean compareAddedMixins(ContainerProxyCacheKey other) 208 { 209 if ((this.addedMixins == null && other.addedMixins != null) || 210 (this.addedMixins == null && other.addedMixins != null)) 211 { 212 return false; 213 } 214 215 if (this.addedMixins != null && other.addedMixins != null) 216 { 217 if (this.addedMixins.length != other.addedMixins.length) 218 { 219 return false; 220 } 221 222 for (int i = 0 ; i < this.addedMixins.length ; i++) 223 { 224 if (!this.addedMixins[i].equals(other.addedMixins[i])) 225 { 226 return false; 227 } 228 } 229 } 230 231 return true; 232 } 233 234 static class MixinAlphabetical implements Comparator 235 { 236 static MixinAlphabetical singleton = new MixinAlphabetical(); 237 238 public int compare(Object o1, Object o2) 239 { 240 String name1 = ((AOPProxyFactoryMixin)o1).getMixin().getName(); 241 String name2 = ((AOPProxyFactoryMixin)o2).getMixin().getName(); 242 return (name1).compareTo(name2); 243 } 244 } 245 } | Popular Tags |