1 22 package org.jboss.metadata.spi.retrieval.cummulative; 23 24 import java.lang.annotation.Annotation ; 25 import java.util.HashSet ; 26 import java.util.List ; 27 import java.util.Set ; 28 29 import org.jboss.metadata.spi.context.MetaDataContext; 30 import org.jboss.metadata.spi.retrieval.AnnotationItem; 31 import org.jboss.metadata.spi.retrieval.AnnotationsItem; 32 import org.jboss.metadata.spi.retrieval.MetaDataRetrieval; 33 import org.jboss.metadata.spi.retrieval.simple.SimpleAnnotationsItem; 34 35 41 public class CummulativeAnnotationsItem extends SimpleAnnotationsItem 42 { 43 44 private MetaDataContext context; 45 46 47 private boolean includeParent; 48 49 50 private long validTime; 51 52 58 public CummulativeAnnotationsItem(MetaDataContext context, boolean includeParent) 59 { 60 if (context == null) 61 throw new IllegalArgumentException ("Null context"); 62 63 this.context = context; 64 this.includeParent = includeParent; 65 init(context.getValidTime().getValidTime()); 66 } 67 68 public Annotation [] getValue() 69 { 70 checkValid(); 71 return super.getValue(); 72 } 73 74 public AnnotationItem[] getAnnotations() 75 { 76 checkValid(); 77 return super.getAnnotations(); 78 } 79 80 public boolean isCachable() 81 { 82 return true; 83 } 84 85 public boolean isValid() 86 { 87 return true; 88 } 89 90 93 protected void checkValid() 94 { 95 AnnotationItem[] items = super.getAnnotations(); 96 boolean valid = (items != null); 97 98 long newValidTime = context.getValidTime().getValidTime(); 99 if (validTime < newValidTime) 100 valid = false; 101 102 if (valid && items != null) 103 { 104 for (AnnotationItem item : items) 105 { 106 if (item.isValid() == false) 107 valid = false; 108 } 109 } 110 111 if (valid == false) 112 init(newValidTime); 113 } 114 115 120 protected void init(long validTime) 121 { 122 Set <AnnotationItem> temp = null; 123 124 List <MetaDataRetrieval> retrievals; 125 126 if (includeParent) 127 retrievals = context.getRetrievals(); 128 else 129 retrievals = context.getLocalRetrievals(); 130 131 for (MetaDataRetrieval retrieval : retrievals) 132 { 133 AnnotationsItem item = retrieval.retrieveAnnotations(); 134 if (item != null) 135 { 136 AnnotationItem[] items = item.getAnnotations(); 137 for (AnnotationItem it : items) 138 { 139 if (temp == null) 140 temp = new HashSet <AnnotationItem>(); 141 temp.add(it); 142 } 143 } 144 } 145 146 AnnotationItem[] items = NO_ANNOTATION_ITEMS; 147 if (temp != null) 148 items = temp.toArray(new AnnotationItem[temp.size()]); 149 setAnnotationItems(items); 150 this.validTime = validTime; 151 } 152 } 153 | Popular Tags |