1 22 package org.jboss.metadata.plugins.loader.memory; 23 24 import java.lang.annotation.Annotation ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Map ; 28 import java.util.concurrent.ConcurrentHashMap ; 29 30 import org.jboss.metadata.plugins.loader.AbstractMutableMetaDataLoader; 31 import org.jboss.metadata.spi.retrieval.AnnotationItem; 32 import org.jboss.metadata.spi.retrieval.AnnotationsItem; 33 import org.jboss.metadata.spi.retrieval.Item; 34 import org.jboss.metadata.spi.retrieval.MetaDataItem; 35 import org.jboss.metadata.spi.retrieval.MetaDatasItem; 36 import org.jboss.metadata.spi.retrieval.basic.BasicAnnotationItem; 37 import org.jboss.metadata.spi.retrieval.basic.BasicAnnotationsItem; 38 import org.jboss.metadata.spi.retrieval.basic.BasicMetaDataItem; 39 import org.jboss.metadata.spi.retrieval.basic.BasicMetaDatasItem; 40 import org.jboss.metadata.spi.scope.ScopeKey; 41 42 48 public class MemoryMetaDataLoader extends AbstractMutableMetaDataLoader 49 { 50 51 private volatile Map <String , BasicAnnotationItem> annotations; 52 53 54 private volatile Map <String , BasicMetaDataItem> metaDataByName; 55 56 57 private volatile BasicAnnotationsItem cachedAnnotationsItem; 58 59 60 private volatile BasicMetaDatasItem cachedMetaDatasItem; 61 62 63 private final boolean cachable; 64 65 68 public MemoryMetaDataLoader() 69 { 70 this(true, false); 71 } 72 73 79 public MemoryMetaDataLoader(boolean cachable, boolean restricted) 80 { 81 super(restricted); 82 this.cachable = cachable; 83 } 84 85 90 public MemoryMetaDataLoader(ScopeKey scope) 91 { 92 this(scope, true, false); 93 } 94 95 102 public MemoryMetaDataLoader(ScopeKey scope, boolean cachable, boolean restricted) 103 { 104 super(scope, restricted); 105 this.cachable = cachable; 106 } 107 108 public <T> boolean isCachable(Item<T> item) 109 { 110 return cachable; 111 } 112 113 public AnnotationsItem retrieveAnnotations() 114 { 115 BasicAnnotationsItem result = cachedAnnotationsItem; 116 if (result != null && result.isValid()) 117 return result; 118 119 Map <String , BasicAnnotationItem> temp = annotations; 120 if (temp == null) 121 return noAnnotations(); 122 123 Collection <BasicAnnotationItem> values = temp.values(); 124 if (values.isEmpty()) 125 return noAnnotations(); 126 127 AnnotationItem[] items = values.toArray(new AnnotationItem[values.size()]); 128 result = new BasicAnnotationsItem(this, items); 129 cachedAnnotationsItem = result; 130 return result; 131 } 132 133 @SuppressWarnings ("unchecked") 134 public <T extends Annotation > AnnotationItem<T> retrieveAnnotation(Class <T> annotationType) 135 { 136 Map <String , BasicAnnotationItem> temp = annotations; 137 if (temp == null) 138 return null; 139 return temp.get(annotationType.getName()); 140 } 141 142 @SuppressWarnings ("unchecked") 143 public <T extends Annotation > T addAnnotation(T annotation) 144 { 145 if (annotation == null) 146 throw new IllegalArgumentException ("Null annotation"); 147 checkRestricted(annotation); 148 149 synchronized (this) 150 { 151 if (annotations == null) 152 annotations = new ConcurrentHashMap <String , BasicAnnotationItem>(); 153 } 154 155 T result = null; 156 157 Class <? extends Annotation > annotationType = annotation.annotationType(); 158 BasicAnnotationItem<T> old = annotations.get(annotationType.getName()); 159 if (old != null) 160 { 161 result = old.getAnnotation(); 162 if (result == annotation) 163 return result; 164 old.invalidate(); 165 } 166 167 BasicAnnotationItem<T> item = new BasicAnnotationItem<T>(this, annotation); 168 annotations.put(annotationType.getName(), item); 169 invalidateAnnotationsItem(); 170 invalidateMetaDatasItem(); 171 invalidate(); 172 return result; 173 } 174 175 @SuppressWarnings ("unchecked") 176 public <T extends Annotation > T removeAnnotation(Class <T> annotationType) 177 { 178 if (annotations == null) 179 return null; 180 BasicAnnotationItem<T> annotation = annotations.remove(annotationType.getName()); 181 if (annotation == null) 182 return null; 183 annotation.invalidate(); 184 invalidateAnnotationsItem(); 185 invalidateMetaDatasItem(); 186 return annotation.getAnnotation(); 187 } 188 189 public MetaDatasItem retrieveMetaData() 190 { 191 BasicMetaDatasItem result = cachedMetaDatasItem; 192 if (result != null && result.isValid()) 193 return result; 194 195 Collection <BasicMetaDataItem> all = null; 196 Map <String , BasicAnnotationItem> temp1 = annotations; 197 if (temp1 != null && temp1.size() > 0) 198 { 199 if (all == null) 200 all = new ArrayList <BasicMetaDataItem>(); 201 Collection <BasicAnnotationItem> values = temp1.values(); 202 all.addAll(values); 203 } 204 Map <String , BasicMetaDataItem> temp2 = metaDataByName; 205 if (temp2 != null && temp2.size() > 0) 206 { 207 if (all == null) 208 all = new ArrayList <BasicMetaDataItem>(); 209 Collection <BasicMetaDataItem> values = temp2.values(); 210 all.addAll(values); 211 } 212 213 if (all == null) 214 return noMetaDatas(); 215 216 MetaDataItem[] metaDataItems = all.toArray(new MetaDataItem[all.size()]); 217 result = new BasicMetaDatasItem(this, metaDataItems); 218 cachedMetaDatasItem = result; 219 return result; 220 } 221 222 @SuppressWarnings ("unchecked") 223 public <T> MetaDataItem<T> retrieveMetaData(Class <T> type) 224 { 225 MetaDataItem<T> result = super.retrieveMetaData(type); 226 if (result != null) 227 return result; 228 229 Map temp = metaDataByName; 230 if (temp == null) 231 return null; 232 return (MetaDataItem<T>) temp.get(type.getName()); 233 } 234 235 public MetaDataItem retrieveMetaData(String name) 236 { 237 Map <String , BasicMetaDataItem> temp = metaDataByName; 238 if (temp != null) 239 { 240 MetaDataItem result = temp.get(name); 241 if (result != null) 242 return result; 243 } 244 245 Map <String , BasicAnnotationItem> temp2 = annotations; 246 if (temp2 != null) 247 return temp2.get(name); 248 249 return null; 250 } 251 252 @SuppressWarnings ("unchecked") 253 public <T> T addMetaData(T metaData, Class <T> type) 254 { 255 if (metaData == null) 256 throw new IllegalArgumentException ("Null metaData"); 257 if (type == null) 258 throw new IllegalArgumentException ("Null type"); 259 260 if (metaData instanceof Annotation ) 261 return (T) addAnnotation((Annotation ) metaData); 262 263 checkRestricted(type); 264 265 synchronized (this) 266 { 267 if (metaDataByName == null) 268 metaDataByName = new ConcurrentHashMap <String , BasicMetaDataItem>(); 269 } 270 271 T result = null; 272 273 BasicMetaDataItem<T> old = metaDataByName.get(type.getName()); 274 if (old != null) 275 { 276 result = old.getValue(); 277 if (result == metaData) 278 return result; 279 old.invalidate(); 280 } 281 BasicMetaDataItem<T> item = new BasicMetaDataItem<T>(this, type.getName(), metaData); 282 metaDataByName.put(type.getName(), item); 283 invalidateMetaDatasItem(); 284 invalidate(); 285 return result; 286 } 287 288 @SuppressWarnings ("unchecked") 289 public <T> T removeMetaData(Class <T> type) 290 { 291 if (type == null) 292 throw new IllegalArgumentException ("Null type"); 293 294 if (type.isAnnotation()) 295 return (T) removeAnnotation((Class <Annotation >) type); 296 297 if (metaDataByName == null) 298 return null; 299 300 BasicMetaDataItem<T> result = metaDataByName.remove(type.getName()); 301 if (result == null) 302 return null; 303 result.invalidate(); 304 invalidateMetaDatasItem(); 305 return result.getValue(); 306 } 307 308 @SuppressWarnings ("unchecked") 309 public <T> T addMetaData(String name, T metaData, Class <T> type) 310 { 311 if (name == null) 312 throw new IllegalArgumentException ("Null name"); 313 if (metaData == null) 314 throw new IllegalArgumentException ("Null metaData"); 315 if (type == null) 316 throw new IllegalArgumentException ("Null type"); 317 318 checkRestricted(type); 319 320 synchronized (this) 321 { 322 if (metaDataByName == null) 323 metaDataByName = new ConcurrentHashMap <String , BasicMetaDataItem>(); 324 } 325 326 T result = null; 327 328 BasicMetaDataItem<T> old = metaDataByName.get(name); 329 if (old != null) 330 { 331 result = old.getValue(); 332 if (result == metaData) 333 return result; 334 old.invalidate(); 335 } 336 BasicMetaDataItem<T> item = new BasicMetaDataItem<T>(this, name, metaData); 337 metaDataByName.put(name, item); 338 invalidateMetaDatasItem(); 339 invalidate(); 340 return result; 341 } 342 343 @SuppressWarnings ("unchecked") 344 public <T> T removeMetaData(String name, Class <T> type) 345 { 346 if (name == null) 347 throw new IllegalArgumentException ("Null name"); 348 349 Map <String , BasicMetaDataItem> temp = metaDataByName; 350 if (temp == null) 351 return null; 352 353 BasicMetaDataItem<T> result = temp.remove(name); 354 if (result == null) 355 return null; 356 result.invalidate(); 357 invalidateMetaDatasItem(); 358 return result.getValue(); 359 } 360 361 364 protected void invalidateAnnotationsItem() 365 { 366 BasicAnnotationsItem temp = cachedAnnotationsItem; 367 if (temp != null) 368 { 369 temp.invalidate(); 370 cachedAnnotationsItem = null; 371 } 372 } 373 374 379 protected BasicAnnotationsItem noAnnotations() 380 { 381 BasicAnnotationsItem result = new BasicAnnotationsItem(this, BasicAnnotationsItem.NO_ANNOTATION_ITEMS); 382 cachedAnnotationsItem = result; 383 return result; 384 } 385 386 389 protected void invalidateMetaDatasItem() 390 { 391 BasicMetaDatasItem temp = cachedMetaDatasItem; 392 if (temp != null) 393 { 394 temp.invalidate(); 395 cachedMetaDatasItem = null; 396 } 397 } 398 399 404 protected BasicMetaDatasItem noMetaDatas() 405 { 406 BasicMetaDatasItem result = new BasicMetaDatasItem(this, BasicMetaDatasItem.NO_META_DATA_ITEMS); 407 cachedMetaDatasItem = result; 408 return result; 409 } 410 } 411 | Popular Tags |