KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > catalog > SPSNameCacheable


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.catalog.SPSNameCacheable
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.catalog;
23
24 import org.apache.derby.iapi.services.cache.Cacheable;
25 import org.apache.derby.iapi.services.cache.CacheManager;
26
27 import org.apache.derby.iapi.services.stream.HeaderPrintWriter;
28
29 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
30 import org.apache.derby.iapi.sql.dictionary.SPSDescriptor;
31
32 import org.apache.derby.iapi.error.StandardException;
33
34 import org.apache.derby.iapi.services.sanity.SanityManager;
35
36 /**
37  * This class implements a Cacheable for a DataDictionary cache of
38  * sps descriptors, with the lookup key being the name/schema of the sps.
39  * Assumes client passes in a string that includes the schema name.
40  * <p>
41  * The cache ensures that the class of the target sps is loaded
42  * if the sps is found in cache. This is ensured by calling
43  * loadGeneratedClass() on the sps when it is added to the cache.
44  * Each subsequent user of the sps cache will do its own load/unload
45  * on the class. Because the class manager/loader maintains reference
46  * counts on the classes it is handling, the user load/unload will
47  * just increment/decrement the use count. Only when the sps is
48  * uncached will it be unloaded.
49  */

50 class SPSNameCacheable implements Cacheable
51 {
52     private TableKey identity;
53     private SPSDescriptor spsd;
54     private final DataDictionaryImpl dd;
55
56
57     SPSNameCacheable(DataDictionaryImpl dd) {
58         this.dd = dd;
59     }
60
61     /* Cacheable interface */
62
63     /** @see Cacheable#clearIdentity */
64     public void clearIdentity()
65     {
66         if (spsd != null)
67         {
68             dd.spsCacheEntryRemoved(spsd);
69
70             if (SanityManager.DEBUG)
71             {
72                 if (SanityManager.DEBUG_ON("SPSNameCacheTrace"))
73                 {
74                     System.out.println("SPSCACHE: clearIdentity() on "+spsd.getName());
75                 }
76             }
77             spsd = null;
78             identity = null;
79         }
80     }
81
82     /** @see Cacheable#getIdentity */
83     public Object JavaDoc getIdentity()
84     {
85         return identity;
86     }
87
88     /** @see Cacheable#createIdentity */
89     public Cacheable createIdentity(Object JavaDoc key, Object JavaDoc createParameter)
90     {
91         if (SanityManager.DEBUG)
92         {
93             if (!(key instanceof TableKey))
94             {
95                 SanityManager.THROWASSERT("Key for a SPSNameCacheElement is a " +
96                         key.getClass().getName() +
97                         " instead of a TableKey");
98             }
99             if (!(createParameter instanceof SPSDescriptor))
100             {
101                 SanityManager.THROWASSERT("Create parameter for a SPSNameCacheElement is a " +
102                     createParameter.getClass().getName() +
103                     "instead of a SPSDescriptorImpl");
104             }
105         }
106
107         identity = (TableKey)key;
108         spsd = (SPSDescriptor) createParameter;
109         
110         if (spsd != null)
111         {
112             if (SanityManager.DEBUG)
113             {
114                 if (SanityManager.DEBUG_ON("SPSNameCacheTrace"))
115                 {
116                     System.out.println("SPSCACHE: createIdentity() on "+spsd.getName());
117                 }
118             }
119
120             dd.spsCacheEntryAdded(spsd);
121             try
122             {
123                 spsd.loadGeneratedClass();
124             } catch (StandardException e)
125             {
126                 /*
127                 ** We cannot throw an exception here, and although
128                 ** we don't expect a problem, we'll put some debugging
129                 ** under sanity just in case. Note that even if we
130                 ** do get an exception here, everything else will work
131                 ** ok -- subsequent attempts to access the generated
132                 ** class for this sps will do a load themselves, and
133                 ** they will throw their exception back to the user.
134                 */

135                 if (SanityManager.DEBUG)
136                 {
137                     System.out.println("Error loading class for "+spsd.getName());
138                     System.out.println(e);
139                     e.printStackTrace();
140                 }
141             }
142             return this;
143         }
144         else
145         {
146             return null;
147         }
148     }
149
150     /**
151      * @see Cacheable#setIdentity
152      *
153      * @exception StandardException Thrown on error
154      */

155     public Cacheable setIdentity(Object JavaDoc key) throws StandardException
156     {
157         if (SanityManager.DEBUG)
158         {
159             if (!(key instanceof TableKey))
160             {
161                 SanityManager.THROWASSERT("Key for a SPSNameCacheable Element is a " +
162                     key.getClass().getName() +
163                     " instead of a TableKey");
164             }
165         }
166
167         
168         identity = (TableKey)key ;
169         spsd = dd.getUncachedSPSDescriptor(identity);
170         if (spsd != null)
171         {
172             if (SanityManager.DEBUG)
173             {
174                 if (SanityManager.DEBUG_ON("SPSNameCacheTrace"))
175                 {
176                     System.out.println("SPSCACHE: setIdentity() on "+spsd.getName());
177                 }
178             }
179
180             dd.spsCacheEntryAdded(spsd);
181             try
182             {
183                 spsd.loadGeneratedClass();
184             } catch (StandardException e)
185             {
186                 /*
187                 ** We cannot throw an exception here, and although
188                 ** we don't expect a problem, we'll put some debugging
189                 ** under sanity just in case. Note that even if we
190                 ** do get an exception here, everything else will work
191                 ** ok -- subsequent attempts to access the generated
192                 ** class for this sps will do a load themselves, and
193                 ** they will throw their exception back to the user.
194                 */

195                 if (SanityManager.DEBUG)
196                 {
197                     System.out.println("Error loading class for "+spsd.getName());
198                     System.out.println(e);
199                     e.printStackTrace();
200                 }
201             }
202             return this;
203         }
204         else
205         {
206             return null;
207         }
208     }
209
210     /* Cacheable interface */
211
212     /** @see Cacheable#clean */
213     public void clean(boolean forRemove)
214     {
215         return;
216     }
217
218     /** @see Cacheable#isDirty */
219     public boolean isDirty()
220     {
221         return false;
222     }
223
224     /**
225      * Get the sps descriptor that is associated with this Cacheable
226      */

227     public SPSDescriptor getSPSDescriptor()
228     {
229         return spsd;
230     }
231
232     /**
233      * Check the consistency of the table descriptor held by this TDCacheable
234      * versus an uncached table descriptor.
235      *
236      * @param uncachedSpsd The uncached descriptor to compare to
237      * @param identity The identity of the table descriptor
238      * @param reportInconsistent A HeaderPrintWriter to send complaints to
239      *
240      * @return true if the descriptors are the same, false if they're different
241      *
242      * @exception StandardException Thrown on error
243      */

244     private boolean checkConsistency(SPSDescriptor uncachedSpsd,
245                                         Object JavaDoc identity,
246                                         HeaderPrintWriter reportInconsistent)
247             throws StandardException
248     {
249         boolean retval = true;
250
251         if (SanityManager.DEBUG)
252         {
253             if (uncachedSpsd == null)
254             {
255                 reportInconsistent.println(
256                     "Inconsistent SPSNameCacheable: identity = " + identity +
257                     ", uncached table descriptor not found.");
258                 retval = false;
259             }
260             else
261             {
262                 if (
263                     (!uncachedSpsd.getText().equals(spsd.getText())) ||
264                     (!uncachedSpsd.getUsingText().equals(spsd.getUsingText())) ||
265                     (!uncachedSpsd.getQualifiedName().equals(spsd.getQualifiedName()))
266                 )
267                 {
268                     reportInconsistent.println(
269                         "Inconsistent SPSNameCacheable: identity = " + identity +
270                         ", cached SPS = " +
271                         spsd +
272                         ", uncached SPS = " +
273                         uncachedSpsd);
274
275                     retval = false;
276                 }
277             }
278         }
279
280         return retval;
281     }
282 }
283
Popular Tags