1 22 package org.jboss.metadata.plugins.loader.thread; 23 24 import java.lang.annotation.Annotation ; 25 import java.security.AccessController ; 26 27 import org.jboss.metadata.plugins.loader.AbstractMutableMetaDataLoader; 28 import org.jboss.metadata.plugins.loader.memory.MemoryMetaDataLoader; 29 import org.jboss.metadata.spi.retrieval.AnnotationItem; 30 import org.jboss.metadata.spi.retrieval.AnnotationsItem; 31 import org.jboss.metadata.spi.retrieval.Item; 32 import org.jboss.metadata.spi.retrieval.MetaDataItem; 33 import org.jboss.metadata.spi.retrieval.MetaDatasItem; 34 import org.jboss.metadata.spi.retrieval.ValidTime; 35 import org.jboss.metadata.spi.retrieval.basic.BasicAnnotationsItem; 36 import org.jboss.metadata.spi.retrieval.basic.BasicMetaDatasItem; 37 import org.jboss.metadata.spi.scope.CommonLevels; 38 import org.jboss.metadata.spi.scope.Scope; 39 import org.jboss.metadata.spi.scope.ScopeKey; 40 41 47 public class ThreadLocalMetaDataLoader extends AbstractMutableMetaDataLoader 48 { 49 50 public static final ThreadLocalMetaDataLoader INSTANCE = new ThreadLocalMetaDataLoader(); 51 52 private static final GetThreadName GET_THREAD_NAME = new GetThreadName(); 53 54 55 private ThreadLocal <MemoryMetaDataLoader> threadLocal = new ThreadLocal <MemoryMetaDataLoader>(); 56 57 58 private final BasicAnnotationsItem NO_ANNOTATIONS = new BasicAnnotationsItem(this, BasicAnnotationsItem.NO_ANNOTATION_ITEMS); 59 60 61 private final BasicMetaDatasItem NO_META_DATA = new BasicMetaDatasItem(this, BasicMetaDatasItem.NO_META_DATA_ITEMS); 62 63 68 private static final ScopeKey getThreadScopeKey() 69 { 70 String name = AccessController.doPrivileged(GET_THREAD_NAME); 71 Scope scope = new Scope(CommonLevels.THREAD, name); 72 return new ScopeKey(scope); 73 } 74 75 78 private ThreadLocalMetaDataLoader() 79 { 80 super(true); 81 } 82 83 86 public void clear() 87 { 88 threadLocal.set(null); 89 } 90 91 public ScopeKey getScope() 92 { 93 MemoryMetaDataLoader delegate = threadLocal.get(); 94 if (delegate == null) 95 return getThreadScopeKey(); 96 else 97 return delegate.getScope(); 98 } 99 100 public <T> boolean isCachable(Item<T> item) 103 { 104 return false; 105 } 106 107 public ValidTime getValidTime() 108 { 109 MemoryMetaDataLoader delegate = threadLocal.get(); 110 if (delegate == null) 111 return super.getValidTime(); 112 return delegate.getValidTime(); 113 } 114 115 public void invalidate() 116 { 117 MemoryMetaDataLoader delegate = threadLocal.get(); 118 if (delegate == null) 119 super.invalidate(); 120 else 121 delegate.invalidate(); 122 } 123 124 public AnnotationsItem retrieveAnnotations() 125 { 126 MemoryMetaDataLoader delegate = threadLocal.get(); 127 if (delegate == null) 128 return NO_ANNOTATIONS; 129 return delegate.retrieveAnnotations(); 130 } 131 132 public <T extends Annotation > AnnotationItem<T> retrieveAnnotation(Class <T> annotationType) 133 { 134 MemoryMetaDataLoader delegate = threadLocal.get(); 135 if (delegate == null) 136 return null; 137 return delegate.retrieveAnnotation(annotationType); 138 } 139 140 public <T extends Annotation > T addAnnotation(T annotation) 141 { 142 MemoryMetaDataLoader delegate = threadLocal.get(); 143 if (delegate == null) 144 { 145 delegate = new MemoryMetaDataLoader(getThreadScopeKey(), false, true); 146 threadLocal.set(delegate); 147 } 148 return delegate.addAnnotation(annotation); 149 } 150 151 public <T extends Annotation > T removeAnnotation(Class <T> annotationType) 152 { 153 MemoryMetaDataLoader delegate = threadLocal.get(); 154 if (delegate == null) 155 return null; 156 return delegate.removeAnnotation(annotationType); 157 } 158 159 public MetaDatasItem retrieveMetaData() 160 { 161 MemoryMetaDataLoader delegate = threadLocal.get(); 162 if (delegate == null) 163 return NO_META_DATA; 164 return delegate.retrieveMetaData(); 165 } 166 167 public <T> MetaDataItem<T> retrieveMetaData(Class <T> type) 168 { 169 MemoryMetaDataLoader delegate = threadLocal.get(); 170 if (delegate == null) 171 return null; 172 return delegate.retrieveMetaData(type); 173 } 174 175 public MetaDataItem retrieveMetaData(String name) 176 { 177 MemoryMetaDataLoader delegate = threadLocal.get(); 178 if (delegate == null) 179 return null; 180 return delegate.retrieveMetaData(name); 181 } 182 183 public <T> T addMetaData(T metaData, Class <T> type) 184 { 185 MemoryMetaDataLoader delegate = threadLocal.get(); 186 if (delegate == null) 187 { 188 delegate = new MemoryMetaDataLoader(getThreadScopeKey(), false, true); 189 threadLocal.set(delegate); 190 } 191 return delegate.addMetaData(metaData, type); 192 } 193 194 public <T> T removeMetaData(Class <T> type) 195 { 196 MemoryMetaDataLoader delegate = threadLocal.get(); 197 if (delegate == null) 198 return null; 199 return delegate.removeMetaData(type); 200 } 201 202 public <T> T addMetaData(String name, T metaData, Class <T> type) 203 { 204 MemoryMetaDataLoader delegate = threadLocal.get(); 205 if (delegate == null) 206 { 207 delegate = new MemoryMetaDataLoader(getThreadScopeKey(), false, true); 208 threadLocal.set(delegate); 209 } 210 return delegate.addMetaData(name, metaData, type); 211 } 212 213 public <T> T removeMetaData(String name, Class <T> type) 214 { 215 MemoryMetaDataLoader delegate = threadLocal.get(); 216 if (delegate == null) 217 return null; 218 return delegate.removeMetaData(name, type); 219 } 220 } 221 | Popular Tags |