KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.concurrent.CopyOnWriteArrayList JavaDoc;
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 /**
44  * AbstractMetaDataContext.
45  *
46  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
47  * @version $Revision: 46146 $
48  */

49 public class AbstractMetaDataContext implements MetaDataContext
50 {
51    /** The meta data retrievals */
52    private List JavaDoc<MetaDataRetrieval> retrievals;
53
54    /** The parent context */
55    private MetaDataContext parent;
56    
57    /** The scope */
58    private volatile ScopeKey scopeKey;
59    
60    /**
61     * Create a new AbstractMetaDataContext.
62     *
63     * @param retrieval the retrieval
64     */

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

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

87    public AbstractMetaDataContext(MetaDataContext parent, List JavaDoc<MetaDataRetrieval> retrievals)
88    {
89       if (retrievals == null)
90          throw new IllegalArgumentException JavaDoc("Null retrievals");
91       if (retrievals.isEmpty())
92          throw new IllegalArgumentException JavaDoc("Must have at least one retrieval");
93       for (MetaDataRetrieval retrieval : retrievals)
94       {
95          if (retrieval == null)
96             throw new IllegalArgumentException JavaDoc("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 JavaDoc<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 JavaDoc<MetaDataRetrieval> getRetrievals()
152    {
153       if (parent == null)
154          return retrievals;
155       
156       List JavaDoc<MetaDataRetrieval> result = new ArrayList JavaDoc<MetaDataRetrieval>(retrievals);
157       result.add(parent);
158       return result;
159    }
160
161    public List JavaDoc<MetaDataRetrieval> getLocalRetrievals()
162    {
163       return retrievals;
164    }
165    
166    public void append(MetaDataRetrieval retrieval)
167    {
168       if (retrieval == null)
169          throw new IllegalArgumentException JavaDoc("Null retrieval");
170       
171       if (retrievals instanceof CopyOnWriteArrayList JavaDoc == false)
172          retrievals = new CopyOnWriteArrayList JavaDoc<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 JavaDoc("Null retrieval");
182       
183       if (retrievals instanceof CopyOnWriteArrayList JavaDoc == false)
184          retrievals = new CopyOnWriteArrayList JavaDoc<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 JavaDoc("Null retrieval");
194
195       if (retrievals.size() == 1)
196          throw new IllegalStateException JavaDoc("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 JavaDoc> AnnotationItem<T> retrieveAnnotation(Class JavaDoc<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 JavaDoc<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 JavaDoc 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