KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > metadata > plugins > loader > thread > ThreadLocalMetaDataLoader


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.metadata.plugins.loader.thread;
23
24 import java.lang.annotation.Annotation JavaDoc;
25 import java.security.AccessController JavaDoc;
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 /**
42  * ThreadLocalMetaDataLoader.
43  *
44  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
45  * @version $Revision: 46146 $
46  */

47 public class ThreadLocalMetaDataLoader extends AbstractMutableMetaDataLoader
48 {
49    /** The singleton instance */
50    public static final ThreadLocalMetaDataLoader INSTANCE = new ThreadLocalMetaDataLoader();
51
52    private static final GetThreadName GET_THREAD_NAME = new GetThreadName();
53    
54    /** The thread local */
55    private ThreadLocal JavaDoc<MemoryMetaDataLoader> threadLocal = new ThreadLocal JavaDoc<MemoryMetaDataLoader>();
56
57    /** No annotations */
58    private final BasicAnnotationsItem NO_ANNOTATIONS = new BasicAnnotationsItem(this, BasicAnnotationsItem.NO_ANNOTATION_ITEMS);
59
60    /** No meta data */
61    private final BasicMetaDatasItem NO_META_DATA = new BasicMetaDatasItem(this, BasicMetaDatasItem.NO_META_DATA_ITEMS);
62    
63    /**
64     * Get the thread scope key
65     *
66     * @return the scope key
67     */

68    private static final ScopeKey getThreadScopeKey()
69    {
70       String JavaDoc name = AccessController.doPrivileged(GET_THREAD_NAME);
71       Scope scope = new Scope(CommonLevels.THREAD, name);
72       return new ScopeKey(scope);
73    }
74    
75    /**
76     * Singleton
77     */

78    private ThreadLocalMetaDataLoader()
79    {
80       super(true);
81    }
82  
83    /**
84     * Clear all the values for the current thread
85     */

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    // The only items we produce are the no annotations and no metadata
101
// these should not be cached
102
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 JavaDoc> AnnotationItem<T> retrieveAnnotation(Class JavaDoc<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 JavaDoc> 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 JavaDoc> T removeAnnotation(Class JavaDoc<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 JavaDoc<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 JavaDoc 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 JavaDoc<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 JavaDoc<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 JavaDoc name, T metaData, Class JavaDoc<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 JavaDoc name, Class JavaDoc<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