1 22 package org.jboss.metadata.plugins.context; 23 24 import java.lang.annotation.Annotation ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.List ; 29 import java.util.concurrent.CopyOnWriteArrayList ; 30 31 import org.jboss.metadata.spi.context.MetaDataContext; 32 import org.jboss.metadata.spi.retrieval.AnnotationItem; 33 import org.jboss.metadata.spi.retrieval.AnnotationsItem; 34 import org.jboss.metadata.spi.retrieval.MetaDataItem; 35 import org.jboss.metadata.spi.retrieval.MetaDataRetrieval; 36 import org.jboss.metadata.spi.retrieval.MetaDatasItem; 37 import org.jboss.metadata.spi.retrieval.ValidTime; 38 import org.jboss.metadata.spi.retrieval.cummulative.CummulativeAnnotationsItem; 39 import org.jboss.metadata.spi.retrieval.cummulative.CummulativeMetaDatasItem; 40 import org.jboss.metadata.spi.scope.Scope; 41 import org.jboss.metadata.spi.scope.ScopeKey; 42 43 49 public class AbstractMetaDataContext implements MetaDataContext 50 { 51 52 private List <MetaDataRetrieval> retrievals; 53 54 55 private MetaDataContext parent; 56 57 58 private volatile ScopeKey scopeKey; 59 60 65 public AbstractMetaDataContext(MetaDataRetrieval retrieval) 66 { 67 this(null, retrieval); 68 } 69 70 76 public AbstractMetaDataContext(MetaDataContext parent, MetaDataRetrieval retrieval) 77 { 78 this(parent, Collections.singletonList(retrieval)); 79 } 80 81 87 public AbstractMetaDataContext(MetaDataContext parent, List <MetaDataRetrieval> retrievals) 88 { 89 if (retrievals == null) 90 throw new IllegalArgumentException ("Null retrievals"); 91 if (retrievals.isEmpty()) 92 throw new IllegalArgumentException ("Must have at least one retrieval"); 93 for (MetaDataRetrieval retrieval : retrievals) 94 { 95 if (retrieval == null) 96 throw new IllegalArgumentException ("Null retrieval"); 97 } 98 99 this.parent = parent; 100 this.retrievals = retrievals; 101 } 102 103 public ScopeKey getScope() 104 { 105 if (scopeKey == null) 106 { 107 ScopeKey key = new ScopeKey(); 108 for (MetaDataRetrieval retrieval : getRetrievals()) 109 { 110 ScopeKey retrievalKey = retrieval.getScope(); 111 Collection <Scope> scopes = retrievalKey.getScopes(); 112 for (Scope scope : scopes) 113 key.addScope(scope); 114 } 115 scopeKey = key; 116 } 117 return scopeKey; 118 } 119 120 public ValidTime getValidTime() 121 { 122 ValidTime result = null; 123 long resultLong = Long.MIN_VALUE; 124 125 if (parent != null) 126 { 127 result = parent.getValidTime(); 128 resultLong = result.getValidTime(); 129 } 130 131 for (int i = 0; i < retrievals.size(); ++i) 132 { 133 MetaDataRetrieval retrieval = retrievals.get(i); 134 ValidTime temp = retrieval.getValidTime(); 135 long tempLong = temp.getValidTime(); 136 if (tempLong > resultLong || result == null) 137 { 138 result = temp; 139 resultLong = tempLong; 140 } 141 } 142 143 return result; 144 } 145 146 public MetaDataContext getParent() 147 { 148 return parent; 149 } 150 151 public List <MetaDataRetrieval> getRetrievals() 152 { 153 if (parent == null) 154 return retrievals; 155 156 List <MetaDataRetrieval> result = new ArrayList <MetaDataRetrieval>(retrievals); 157 result.add(parent); 158 return result; 159 } 160 161 public List <MetaDataRetrieval> getLocalRetrievals() 162 { 163 return retrievals; 164 } 165 166 public void append(MetaDataRetrieval retrieval) 167 { 168 if (retrieval == null) 169 throw new IllegalArgumentException ("Null retrieval"); 170 171 if (retrievals instanceof CopyOnWriteArrayList == false) 172 retrievals = new CopyOnWriteArrayList <MetaDataRetrieval>(retrievals); 173 174 retrievals.add(retrieval); 175 scopeKey = null; 176 } 177 178 public void prepend(MetaDataRetrieval retrieval) 179 { 180 if (retrieval == null) 181 throw new IllegalArgumentException ("Null retrieval"); 182 183 if (retrievals instanceof CopyOnWriteArrayList == false) 184 retrievals = new CopyOnWriteArrayList <MetaDataRetrieval>(retrievals); 185 186 retrievals.add(0, retrieval); 187 scopeKey = null; 188 } 189 190 public void remove(MetaDataRetrieval retrieval) 191 { 192 if (retrieval == null) 193 throw new IllegalArgumentException ("Null retrieval"); 194 195 if (retrievals.size() == 1) 196 throw new IllegalStateException ("Must have at least one retrieval"); 197 198 retrievals.remove(retrieval); 199 scopeKey = null; 200 } 201 202 public AnnotationsItem retrieveAnnotations() 203 { 204 return new CummulativeAnnotationsItem(this, true); 205 } 206 207 public AnnotationsItem retrieveLocalAnnotations() 208 { 209 return new CummulativeAnnotationsItem(this, false); 210 } 211 212 public <T extends Annotation > AnnotationItem<T> retrieveAnnotation(Class <T> annotationType) 213 { 214 for (int i = 0; i < retrievals.size(); ++i) 215 { 216 MetaDataRetrieval retrieval = retrievals.get(i); 217 AnnotationItem<T> item = retrieval.retrieveAnnotation(annotationType); 218 if (item != null) 219 return item; 220 } 221 222 if (parent != null) 223 return parent.retrieveAnnotation(annotationType); 224 225 return null; 226 } 227 228 public MetaDatasItem retrieveMetaData() 229 { 230 return new CummulativeMetaDatasItem(this, true); 231 } 232 233 public MetaDatasItem retrieveLocalMetaData() 234 { 235 return new CummulativeMetaDatasItem(this, false); 236 } 237 238 public <T> MetaDataItem<T> retrieveMetaData(Class <T> type) 239 { 240 for (int i = 0; i < retrievals.size(); ++i) 241 { 242 MetaDataRetrieval retrieval = retrievals.get(i); 243 MetaDataItem<T> item = retrieval.retrieveMetaData(type); 244 if (item != null) 245 return item; 246 } 247 248 if (parent != null) 249 return parent.retrieveMetaData(type); 250 251 return null; 252 } 253 254 public MetaDataItem retrieveMetaData(String name) 255 { 256 for (int i = 0; i < retrievals.size(); ++i) 257 { 258 MetaDataRetrieval retrieval = retrievals.get(i); 259 MetaDataItem item = retrieval.retrieveMetaData(name); 260 if (item != null) 261 return item; 262 } 263 264 if (parent != null) 265 return parent.retrieveMetaData(name); 266 267 return null; 268 } 269 } 270 | Popular Tags |