KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > metadata > plugins > context > CachingMetaDataContext


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.context;
23
24 import java.lang.annotation.Annotation JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.concurrent.ConcurrentHashMap JavaDoc;
29
30 import org.jboss.metadata.spi.context.MetaDataContext;
31 import org.jboss.metadata.spi.retrieval.AnnotationItem;
32 import org.jboss.metadata.spi.retrieval.AnnotationsItem;
33 import org.jboss.metadata.spi.retrieval.MetaDataItem;
34 import org.jboss.metadata.spi.retrieval.MetaDataRetrieval;
35 import org.jboss.metadata.spi.retrieval.MetaDatasItem;
36
37 /**
38  * CachingMetaDataContext.
39  *
40  * @TODO LRU Cache
41  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
42  * @version $Revision: 57133 $
43  */

44 public class CachingMetaDataContext extends AbstractMetaDataContext
45 {
46    /** The annotations */
47    private volatile Map JavaDoc<String JavaDoc, AnnotationItem> annotations;
48
49    /** MetaData by name */
50    private volatile Map JavaDoc<String JavaDoc, MetaDataItem> metaDataByName;
51
52    /** All annotations */
53    private volatile AnnotationsItem cachedAnnotationsItem;
54
55    /** All meta data */
56    private volatile MetaDatasItem cachedMetaDatasItem;
57
58    /** The valid time */
59    private volatile long validTime;
60    
61    /**
62     * Create a new CachingMetaDataContext.
63     *
64     * @param retrieval the retrieval
65     */

66    public CachingMetaDataContext(MetaDataRetrieval retrieval)
67    {
68       this(null, retrieval);
69    }
70    
71    /**
72     * Create a new CachingMetaDataContext.
73     *
74     * @param parent the parent
75     * @param retrieval the retrieval
76     */

77    public CachingMetaDataContext(MetaDataContext parent, MetaDataRetrieval retrieval)
78    {
79       this(parent, Collections.singletonList(retrieval));
80    }
81    
82    /**
83     * Create a new CachingMetaDataContext.
84     *
85     * @param parent the parent
86     * @param retrievals the retrievals
87     */

88    public CachingMetaDataContext(MetaDataContext parent, List JavaDoc<MetaDataRetrieval> retrievals)
89    {
90       super(parent, retrievals);
91       validTime = super.getValidTime().getValidTime();
92    }
93
94    public AnnotationsItem retrieveAnnotations()
95    {
96       if (cachedAnnotationsItem == null)
97          cachedAnnotationsItem = super.retrieveAnnotations();
98       return cachedAnnotationsItem;
99    }
100
101    @SuppressWarnings JavaDoc("unchecked")
102    public <T extends Annotation JavaDoc> AnnotationItem<T> retrieveAnnotation(Class JavaDoc<T> annotationType)
103    {
104       if (annotationType == null)
105          throw new IllegalArgumentException JavaDoc("Null annotationType");
106       
107       String JavaDoc annotationName = annotationType.getName();
108
109       long newValidTime = super.getValidTime().getValidTime();
110       if (validTime < newValidTime)
111       {
112          if (annotations != null)
113             annotations.clear();
114          if (metaDataByName != null)
115             metaDataByName.clear();
116          validTime = newValidTime;
117       }
118       
119       if (annotations != null)
120       {
121          AnnotationItem<T> result = annotations.get(annotationName);
122          if (result != null)
123          {
124             if (result.isValid())
125                return result;
126             annotations.remove(annotationName);
127          }
128       }
129
130       AnnotationItem<T> result = super.retrieveAnnotation(annotationType);
131       if (result != null && result.isCachable())
132       {
133          if (annotations == null)
134             annotations = new ConcurrentHashMap JavaDoc<String JavaDoc, AnnotationItem>();
135          annotations.put(annotationName, result);
136       }
137       
138       return result;
139    }
140
141    public MetaDatasItem retrieveMetaData()
142    {
143       if (cachedMetaDatasItem == null)
144          cachedMetaDatasItem = super.retrieveMetaData();
145       return cachedMetaDatasItem;
146    }
147
148    @SuppressWarnings JavaDoc("unchecked")
149    public <T> MetaDataItem<T> retrieveMetaData(Class JavaDoc<T> type)
150    {
151       if (type == null)
152          throw new IllegalArgumentException JavaDoc("Null type");
153       
154       String JavaDoc name = type.getName();
155
156       long newValidTime = super.getValidTime().getValidTime();
157       if (validTime < newValidTime)
158       {
159          if (annotations != null)
160             annotations.clear();
161          if (metaDataByName != null)
162             metaDataByName.clear();
163          validTime = newValidTime;
164       }
165
166       if (metaDataByName != null)
167       {
168          MetaDataItem<T> result = metaDataByName.get(name);
169          if (result != null)
170          {
171             if (result.isValid())
172                return result;
173             metaDataByName.remove(name);
174          }
175       }
176
177       MetaDataItem<T> result = super.retrieveMetaData(type);
178       if (result != null && result.isCachable())
179       {
180          if (metaDataByName == null)
181             metaDataByName = new ConcurrentHashMap JavaDoc<String JavaDoc, MetaDataItem>();
182          metaDataByName.put(name, result);
183       }
184       
185       return result;
186    }
187
188    public MetaDataItem retrieveMetaData(String JavaDoc name)
189    {
190       if (name == null)
191          throw new IllegalArgumentException JavaDoc("Null name");
192
193       long newValidTime = super.getValidTime().getValidTime();
194       if (validTime < newValidTime)
195       {
196          if (annotations != null)
197             annotations.clear();
198          if (metaDataByName != null)
199             metaDataByName.clear();
200          validTime = newValidTime;
201       }
202
203       if (metaDataByName != null)
204       {
205          MetaDataItem result = metaDataByName.get(name);
206          if (result != null)
207          {
208             if (result.isValid())
209                return result;
210             metaDataByName.remove(name);
211          }
212       }
213
214       MetaDataItem result = super.retrieveMetaData(name);
215       if (result != null && result.isCachable())
216       {
217          if (metaDataByName == null)
218             metaDataByName = new ConcurrentHashMap JavaDoc<String JavaDoc, MetaDataItem>();
219          metaDataByName.put(name, result);
220       }
221       
222       return result;
223    }
224 }
225
Popular Tags