KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > metadata > spi > retrieval > cummulative > CummulativeMetaDatasItem


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.spi.retrieval.cummulative;
23
24 import java.util.HashSet JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.jboss.metadata.spi.context.MetaDataContext;
29 import org.jboss.metadata.spi.retrieval.MetaDataItem;
30 import org.jboss.metadata.spi.retrieval.MetaDataRetrieval;
31 import org.jboss.metadata.spi.retrieval.MetaDatasItem;
32 import org.jboss.metadata.spi.retrieval.simple.SimpleMetaDatasItem;
33
34 /**
35  * CummulativeMetaDatasItem.
36  *
37  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
38  * @version $Revision: 57307 $
39  */

40 public class CummulativeMetaDatasItem extends SimpleMetaDatasItem
41 {
42    /** The context */
43    private MetaDataContext context;
44    
45    /** Whether to include the parent */
46    private boolean includeParent;
47
48    /** The valid time */
49    private long validTime;
50    
51    /**
52     * Create a new CummulativeMetaDatasItem.
53     *
54     * @param context the context
55     * @param includeParent whether to include the parent
56     */

57    public CummulativeMetaDatasItem(MetaDataContext context, boolean includeParent)
58    {
59       if (context == null)
60          throw new IllegalArgumentException JavaDoc("Null context");
61
62       this.context = context;
63       this.includeParent = includeParent;
64       init(context.getValidTime().getValidTime());
65    }
66
67    public Object JavaDoc[] getValue()
68    {
69       checkValid();
70       return super.getValue();
71    }
72    
73    public MetaDataItem[] getMetaDatas()
74    {
75       checkValid();
76       return super.getMetaDatas();
77    }
78
79    public boolean isCachable()
80    {
81       return true;
82    }
83
84    public boolean isValid()
85    {
86       return true;
87    }
88
89    /**
90     * Check whether we are valid
91     */

92    protected void checkValid()
93    {
94       MetaDataItem[] items = super.getMetaDatas();
95       boolean valid = (items != null);
96       
97       long newValidTime = context.getValidTime().getValidTime();
98       if (validTime < newValidTime)
99          valid = false;
100       
101       if (items != null)
102       {
103          for (MetaDataItem item : items)
104          {
105             if (item.isValid() == false)
106                valid = false;
107          }
108       }
109       
110       if (valid == false)
111          init(newValidTime);
112    }
113
114    /**
115     * Initialise
116     *
117     * @param validTime the valid time
118     */

119    protected void init(long validTime)
120    {
121       Set JavaDoc<MetaDataItem> temp = null;
122
123       List JavaDoc<MetaDataRetrieval> retrievals;
124       
125       if (includeParent)
126          retrievals = context.getRetrievals();
127       else
128          retrievals = context.getLocalRetrievals();
129
130       for (MetaDataRetrieval retrieval : retrievals)
131       {
132          MetaDatasItem item = retrieval.retrieveMetaData();
133          if (item != null)
134          {
135             MetaDataItem[] items = item.getMetaDatas();
136             for (MetaDataItem it : items)
137             {
138                if (temp == null)
139                   temp = new HashSet JavaDoc<MetaDataItem>();
140                temp.add(it);
141             }
142          }
143       }
144       
145       MetaDataItem[] items = NO_META_DATA_ITEMS;
146       if (temp != null)
147          items = temp.toArray(new MetaDataItem[temp.size()]);
148       setMetaDataItems(items);
149       this.validTime = validTime;
150    }
151 }
152
Popular Tags