KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > dsi > ColumnRefCache


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.dsi;
20
21 import java.util.logging.*;
22
23 import org.openharmonise.commons.cache.*;
24 import org.openharmonise.commons.dsi.*;
25 import org.openharmonise.rm.metadata.Profile;
26
27
28 /**
29  * Cache of <code>ColumnRef</code> objects used within Harmonise.
30  * Column references are stored with a cache key composed of the class
31  * name, column name and a boolean of whether the column referfence refers
32  * to a historical table or not.
33  *
34  * <p>Note: Care should be used in dealing with <code>ColumnRef</code> objects
35  * taken from this cache as they are mutable and therefore there is a risk
36  * associated to any changes you might make to an object taken from
37  * the cache. For example, it is possible to reset the table name associated
38  * to the column reference to accomodate an alias in a select statement
39  * maybe, however this could possibly break further use.</p>
40  *
41  * @author mike
42  * @version $Revision: 1.2 $
43  *
44  */

45 public class ColumnRefCache extends AbstractCache {
46
47     /**
48      * Name of this cache
49      */

50     private static final String JavaDoc CACHE_NAME = "columnref";
51     
52     /**
53      * Singleton instance of this cache
54      */

55     private static ColumnRefCache m_instance = null;
56     
57     /**
58      * Logger for this class
59      */

60     private static final Logger m_logger = Logger.getLogger(ColumnRefCache.class.getName());
61
62     /**
63      * Creates new instance of a column reference cache
64      *
65      * @throws CacheException if an error occurs creating the cache
66      */

67     private ColumnRefCache() throws CacheException {
68         super(CACHE_NAME);
69     }
70
71     /**
72      * Returns the singleton instance of the cache
73      *
74      * @return
75      * @throws CacheException
76      */

77     static public ColumnRefCache getInstance() throws CacheException {
78         if (m_instance == null) {
79             m_instance = new ColumnRefCache();
80         }
81
82         return m_instance;
83     }
84
85     /**
86      * Returns a cached <code>ColumnRef</code> for the given column associated
87      * to the given <code>DataStoreObject</code>. If the <code>ColumnRef</code>
88      * is not in the cache it is retrieved from the object's <code>getInstanceColumnRef</code>
89      * method.
90      *
91      * <p>Note: this method calls the <code>getInstanceColumnRef</code> and
92      * therefore should not be used in the <code>getInstanceColumnRef</code>
93      * method of a <code>DataStoreObject</code> in order to avoid an
94      * ininite loop.</p>
95      *
96      * @param dsObj the data store object
97      * @param sColumn the database column name
98      * @param bIsHist <code>true</code> of the column is in the historical
99      * table, otherwise <code>false</code>
100      * @return the <code>ColumnRef</code> associated with the specified
101      * database column
102      * @throws CacheException if an error occurs obtaining the
103      * <code>ColumnRef</code>
104      */

105     public ColumnRef getColumnRef(
106         DataStoreObject dsObj,
107         String JavaDoc sColumn,
108         boolean bIsHist)
109         throws CacheException {
110             String JavaDoc sCacheKey = null;
111             
112         //need to differentiate between profiles, i.e. document profiles, section profiles, etc..
113
if(dsObj instanceof Profile) {
114             sCacheKey = getCacheKey(dsObj.getDBTableName(), sColumn, bIsHist);
115         } else {
116             sCacheKey = getCacheKey(dsObj.getClass(), sColumn, bIsHist);
117         }
118          
119         ColumnRef colref = (ColumnRef) getObject(sCacheKey);
120
121         if (colref == null) {
122             try {
123                 colref = dsObj.getInstanceColumnRef(sColumn, bIsHist);
124             } catch (DataStoreException e) {
125                 throw new CacheException("Error occured getting column ref", e);
126             }
127
128             super.addToCache(sCacheKey, colref);
129         }
130
131         return colref;
132
133     }
134
135     /* (non-Javadoc)
136      * @see org.openharmonise.commons.cache.AbstractCache#getObject(java.lang.Object)
137      */

138     public Object JavaDoc getObject(final Object JavaDoc key) {
139
140         Object JavaDoc cached_object = null;
141         try {
142             cached_object = super.getObject(key);
143         } catch (CacheException e) {
144             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
145         }
146         return cached_object;
147     }
148
149     /**
150      * Returns a cached <code>ColumnRef</code> for the given column associated
151      * to the given class if there is one in the cache, otherwise a null is returned.
152      *
153      * @param clss the <code>Class</code> of a <code>DataStoreObject</code>
154      * @param sColumn the database column name
155      * @param bIsHist <code>true</code> of the column is in the historical
156      * table, otherwise <code>false</code>
157      * @return a cached <code>ColumnRef</code> for the specified database column
158      * @throws CacheException if any errors occur
159      */

160     public ColumnRef getColumnRef(Class JavaDoc clss, String JavaDoc sColumn, boolean bIsHist)
161         throws CacheException {
162
163         String JavaDoc sCacheKey = getCacheKey(clss, sColumn, bIsHist);
164         ColumnRef colref = (ColumnRef) getObject(sCacheKey);
165
166         return colref;
167     }
168
169     /**
170      * Adds the given <code>ColumnRef</code> to this cache for the given
171      * column associated to the given class.
172      *
173      * @param clss the class of the data store object associated to given the <code>ColumnRef</code>
174      * @param sColumn the database column name
175      * @param bIsHist <code>true</code> of the column is in the historical
176      * table, otherwise <code>false</code>
177      * @param colref the column reference to cache
178      */

179     public void addToCache(
180         Class JavaDoc clss,
181         String JavaDoc sColumn,
182         boolean bIsHist,
183         ColumnRef colref) {
184         String JavaDoc sCacheKey = getCacheKey(clss, sColumn, bIsHist);
185         super.addToCache(sCacheKey, colref);
186     }
187
188     /* (non-Javadoc)
189      * @see org.openharmonise.commons.cache.AbstractCache#getCacheableObject(java.lang.Object)
190      */

191     protected Object JavaDoc getCacheableObject(Object JavaDoc key) throws Exception JavaDoc {
192
193         return null;
194     }
195
196     /**
197      * Returns a unique key to be used in the cache composed of the
198      * combination of class, column and historical boolean.
199      *
200      * @param clss the class name
201      * @param sColumn the column name
202      * @param bIsHist <code>true</code> if the column is in the historical table, otherwise <code>false</code>
203      * @return a unique key for the column reference
204      */

205     protected String JavaDoc getCacheKey(Class JavaDoc clss, String JavaDoc sColumn, boolean bIsHist) {
206         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
207         strbuf.append(clss.getName()).append(sColumn).append(
208             String.valueOf(bIsHist));
209         return strbuf.toString();
210     }
211     
212     /**
213      * Returns a unique key to be used in the cache composed the combination
214      * of class, column and historical boolean.
215      *
216      * @param clss the class name
217      * @param sColumn the column name
218      * @param bIsHist <code>true</code> if the column is in the historical table, otherwise <code>false</code>
219      * @return a unique key for the column reference
220      */

221     protected String JavaDoc getCacheKey(String JavaDoc sTable, String JavaDoc sColumn, boolean bIsHist) {
222         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
223         strbuf.append(sTable).append(sColumn).append(
224             String.valueOf(bIsHist));
225         return strbuf.toString();
226     }
227
228 }
229
Popular Tags