KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > metadata > plugins > loader > memory > MemoryMetaDataLoader


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.memory;
23
24 import java.lang.annotation.Annotation JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.concurrent.ConcurrentHashMap JavaDoc;
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 /**
43  * MemoryMetaDataLoader.
44  *
45  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
46  * @version $Revision: 57133 $
47  */

48 public class MemoryMetaDataLoader extends AbstractMutableMetaDataLoader
49 {
50    /** The annotations */
51    private volatile Map JavaDoc<String JavaDoc, BasicAnnotationItem> annotations;
52
53    /** MetaData by name */
54    private volatile Map JavaDoc<String JavaDoc, BasicMetaDataItem> metaDataByName;
55
56    /** All annotations */
57    private volatile BasicAnnotationsItem cachedAnnotationsItem;
58
59    /** All meta data */
60    private volatile BasicMetaDatasItem cachedMetaDatasItem;
61
62    /** Whether we should cache items */
63    private final boolean cachable;
64    
65    /**
66     * Create a new MemoryMetaDataLoader.
67     */

68    public MemoryMetaDataLoader()
69    {
70       this(true, false);
71    }
72    
73    /**
74     * Create a new MemoryMetaDataLoader.
75     *
76     * @param cachable whether items produced should be cachable
77     * @param restricted whether restricted items are allowed
78     */

79    public MemoryMetaDataLoader(boolean cachable, boolean restricted)
80    {
81       super(restricted);
82       this.cachable = cachable;
83    }
84    
85    /**
86     * Create a new MemoryMetaDataLoader.
87     *
88     * @param scope the scope key
89     */

90    public MemoryMetaDataLoader(ScopeKey scope)
91    {
92       this(scope, true, false);
93    }
94    
95    /**
96     * Create a new MemoryMetaDataLoader.
97     *
98     * @param scope the scope key
99     * @param cachable whether items produced should be cachable
100     * @param restricted whether restricted items are allowed
101     */

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 JavaDoc<String JavaDoc, BasicAnnotationItem> temp = annotations;
120       if (temp == null)
121          return noAnnotations();
122       
123       Collection JavaDoc<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 JavaDoc("unchecked")
134    public <T extends Annotation JavaDoc> AnnotationItem<T> retrieveAnnotation(Class JavaDoc<T> annotationType)
135    {
136       Map JavaDoc<String JavaDoc, BasicAnnotationItem> temp = annotations;
137       if (temp == null)
138          return null;
139       return temp.get(annotationType.getName());
140    }
141
142    @SuppressWarnings JavaDoc("unchecked")
143    public <T extends Annotation JavaDoc> T addAnnotation(T annotation)
144    {
145       if (annotation == null)
146          throw new IllegalArgumentException JavaDoc("Null annotation");
147       checkRestricted(annotation);
148       
149       synchronized (this)
150       {
151          if (annotations == null)
152             annotations = new ConcurrentHashMap JavaDoc<String JavaDoc, BasicAnnotationItem>();
153       }
154
155       T result = null;
156
157       Class JavaDoc<? extends Annotation JavaDoc> 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 JavaDoc("unchecked")
176    public <T extends Annotation JavaDoc> T removeAnnotation(Class JavaDoc<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 JavaDoc<BasicMetaDataItem> all = null;
196       Map JavaDoc<String JavaDoc, BasicAnnotationItem> temp1 = annotations;
197       if (temp1 != null && temp1.size() > 0)
198       {
199          if (all == null)
200             all = new ArrayList JavaDoc<BasicMetaDataItem>();
201          Collection JavaDoc<BasicAnnotationItem> values = temp1.values();
202          all.addAll(values);
203       }
204       Map JavaDoc<String JavaDoc, BasicMetaDataItem> temp2 = metaDataByName;
205       if (temp2 != null && temp2.size() > 0)
206       {
207          if (all == null)
208             all = new ArrayList JavaDoc<BasicMetaDataItem>();
209          Collection JavaDoc<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 JavaDoc("unchecked")
223    public <T> MetaDataItem<T> retrieveMetaData(Class JavaDoc<T> type)
224    {
225       MetaDataItem<T> result = super.retrieveMetaData(type);
226       if (result != null)
227          return result;
228       
229       Map JavaDoc temp = metaDataByName;
230       if (temp == null)
231          return null;
232       return (MetaDataItem<T>) temp.get(type.getName());
233    }
234
235    public MetaDataItem retrieveMetaData(String JavaDoc name)
236    {
237       Map JavaDoc<String JavaDoc, 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 JavaDoc<String JavaDoc, BasicAnnotationItem> temp2 = annotations;
246       if (temp2 != null)
247          return temp2.get(name);
248       
249       return null;
250    }
251
252    @SuppressWarnings JavaDoc("unchecked")
253    public <T> T addMetaData(T metaData, Class JavaDoc<T> type)
254    {
255       if (metaData == null)
256          throw new IllegalArgumentException JavaDoc("Null metaData");
257       if (type == null)
258          throw new IllegalArgumentException JavaDoc("Null type");
259       
260       if (metaData instanceof Annotation JavaDoc)
261          return (T) addAnnotation((Annotation JavaDoc) metaData);
262
263       checkRestricted(type);
264
265       synchronized (this)
266       {
267          if (metaDataByName == null)
268             metaDataByName = new ConcurrentHashMap JavaDoc<String JavaDoc, 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 JavaDoc("unchecked")
289    public <T> T removeMetaData(Class JavaDoc<T> type)
290    {
291       if (type == null)
292          throw new IllegalArgumentException JavaDoc("Null type");
293
294       if (type.isAnnotation())
295          return (T) removeAnnotation((Class JavaDoc<Annotation JavaDoc>) 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 JavaDoc("unchecked")
309    public <T> T addMetaData(String JavaDoc name, T metaData, Class JavaDoc<T> type)
310    {
311       if (name == null)
312          throw new IllegalArgumentException JavaDoc("Null name");
313       if (metaData == null)
314          throw new IllegalArgumentException JavaDoc("Null metaData");
315       if (type == null)
316          throw new IllegalArgumentException JavaDoc("Null type");
317       
318       checkRestricted(type);
319
320       synchronized (this)
321       {
322          if (metaDataByName == null)
323             metaDataByName = new ConcurrentHashMap JavaDoc<String JavaDoc, 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 JavaDoc("unchecked")
344    public <T> T removeMetaData(String JavaDoc name, Class JavaDoc<T> type)
345    {
346       if (name == null)
347          throw new IllegalArgumentException JavaDoc("Null name");
348
349       Map JavaDoc<String JavaDoc, 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    /**
362     * Invalidate the annotations item
363     */

364    protected void invalidateAnnotationsItem()
365    {
366       BasicAnnotationsItem temp = cachedAnnotationsItem;
367       if (temp != null)
368       {
369          temp.invalidate();
370          cachedAnnotationsItem = null;
371       }
372    }
373
374    /**
375     * Set no annotations
376     *
377     * @return no annotations
378     */

379    protected BasicAnnotationsItem noAnnotations()
380    {
381       BasicAnnotationsItem result = new BasicAnnotationsItem(this, BasicAnnotationsItem.NO_ANNOTATION_ITEMS);
382       cachedAnnotationsItem = result;
383       return result;
384    }
385
386    /**
387     * Invalidate the metaDatas item
388     */

389    protected void invalidateMetaDatasItem()
390    {
391       BasicMetaDatasItem temp = cachedMetaDatasItem;
392       if (temp != null)
393       {
394          temp.invalidate();
395          cachedMetaDatasItem = null;
396       }
397    }
398
399    /**
400     * Set no meta data
401     *
402     * @return no meta data
403     */

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