KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > dbobj > CacheUtils


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.dbobj;
66
67 import com.jcorporate.expresso.core.cache.CacheException;
68 import com.jcorporate.expresso.core.cache.CacheManager;
69 import com.jcorporate.expresso.core.cache.CacheSystem;
70 import com.jcorporate.expresso.core.dataobjects.DataField;
71 import com.jcorporate.expresso.core.dataobjects.DefaultDataField;
72 import com.jcorporate.expresso.core.dataobjects.jdbc.JDBCObjectMetaData;
73 import com.jcorporate.expresso.core.db.DBException;
74 import org.apache.log4j.Logger;
75
76 import java.util.Iterator JavaDoc;
77
78
79 /**
80  * Utility methods for putting a DBObject into a cache.
81  *
82  * @author Michael Rimov
83  */

84
85 public class CacheUtils {
86
87     private static final Logger log = Logger.getLogger(CacheUtils.class);
88
89     public CacheUtils() {
90     }
91
92
93     /**
94      * Adds a non-modified object to the cache. This is most often called
95      * after DataObject.retrieve(). No related cache notification will occur
96      * when this happens
97      *
98      * @param theDBObj the object to add to the cache.
99      * @throws DBException upon add error (either Cache related or dbobject related)
100      */

101     public void addUnmodifiedToCache(DBObject theDBObj) throws DBException {
102         JDBCObjectMetaData metadata = theDBObj.getJDBCMetaData();
103         String JavaDoc theClassName = theDBObj.getClass().getName();
104
105         //
106
//Don't cache anything without key fields
107
//
108
if (metadata.getKeyFieldListArray().size() == 0) {
109             return;
110         }
111
112         /**
113          * Don't even proceed if cache size is 0
114          */

115         int cacheSize = theDBObj.getCacheSize();
116         if (cacheSize == 0) {
117             return;
118         }
119
120         CacheSystem cs = CacheManager.getCacheSystem(theDBObj.getDataContext());
121         if (cs == null) {
122             return;
123         }
124         try {
125             prepareCache(cs, cacheSize, theClassName);
126             DBObject newObj = prepareForStorage(theDBObj, metadata);
127
128
129             Integer JavaDoc i = (Integer JavaDoc) theDBObj.getAttribute("TTL");
130             if (i == null || i.intValue() == 0) {
131                 cs.put(theClassName, newObj);
132             } else {
133                 //Set the cache value.
134
cs.put(theClassName, newObj,
135                         i.intValue() * 1000 * 60);
136             }
137
138
139             if (log.isDebugEnabled()) {
140                 log.debug("Adding item " + theDBObj.getKey() +
141                         " to cache for " + theClassName);
142             }
143         } catch (CacheException ce) {
144             throw new DBException(ce);
145         }
146
147     }
148
149
150     /**
151      * When using basic caching, this method is used to add a new DB object to the
152      * cache, where it is later used when retrieveing data instead of going directly
153      * to the database. This functionality can be controlled with the "dbCache"
154      * property in the properties file(s).
155      *
156      * @param theDBObj com.jcorporate.expresso.core.dbobj.DBObject The object to be
157      * cached
158      * @throws DBException upon add error (either Cache related or dbobject related)
159      */

160     public void addToCache(DBObject theDBObj)
161             throws DBException {
162         JDBCObjectMetaData metadata = theDBObj.getJDBCMetaData();
163         String JavaDoc theClassName = theDBObj.getClass().getName();
164
165         //
166
//Don't cache anything without key fields
167
//
168
if (metadata.getKeyFieldListArray().size() == 0) {
169             return;
170         }
171
172         /**
173          * Don't even proceed if cache size is 0
174          */

175         int cacheSize = theDBObj.getCacheSize();
176         if (cacheSize == 0) {
177             return;
178         }
179
180         CacheSystem cs = CacheManager.getCacheSystem(theDBObj.getDataContext());
181         if (cs == null) {
182             return;
183         }
184         try {
185             prepareCache(cs, cacheSize, theClassName);
186             DBObject newObj = prepareForStorage(theDBObj, metadata);
187
188
189             Integer JavaDoc i = (Integer JavaDoc) theDBObj.getAttribute("TTL");
190             if (i == null || i.intValue() == 0) {
191                 cs.addItem(theClassName, newObj);
192             } else {
193                 //Set the cache value.
194
cs.addItem(theClassName, newObj,
195                         i.intValue() * 1000 * 60);
196             }
197
198
199             if (log.isDebugEnabled()) {
200                 log.debug("Adding item " + theDBObj.getKey() +
201                         " to cache for " + theClassName);
202             }
203         } catch (CacheException ce) {
204             throw new DBException(ce);
205         }
206     } /* addToCache(DBObject) */
207
208     /**
209      * Creates the appropriate cache if it doesn't already exist
210      *
211      * @param cs the cache system to add
212      * @param cacheSize the size of the cache
213      * @param cacheName the name of the cache to prepare
214      * @throws CacheException upon creation error
215      */

216     private void prepareCache(CacheSystem cs, int cacheSize, String JavaDoc cacheName) throws CacheException {
217         if (!cs.existsCache(cacheName)) {
218             cs.createCache(cacheName, false,
219                     cacheSize);
220         }
221
222     }
223
224     /**
225      * Creates a DBObject for storage in a cache, prepares the cache storage
226      *
227      * @param theDBObj the source object
228      * @param metadata the object's metadata
229      * @return DBObject instance ready to store
230      * @throws DBException upon DBObject access error
231      */

232     private DBObject prepareForStorage(DBObject theDBObj,
233                                        JDBCObjectMetaData metadata)
234             throws DBException {
235
236         DBObject newObj = theDBObj.newInstance();
237         newObj.setDataContext(theDBObj.getDataContext());
238
239         DBField oneField = null;
240
241         for (Iterator JavaDoc i = metadata.getAllFieldsMap().values().iterator();
242              i.hasNext();) {
243             oneField = (DBField) i.next();
244
245             if (!oneField.isVirtual() && !oneField.isBinaryObjectType()) {
246                 DataField df = DefaultDataField.getInstance(oneField, newObj);
247                 df.setValue(theDBObj.getDataField(oneField.getName()).getValue());
248                 newObj.setDataField(oneField.getName(), df);
249             }
250         } /* for each field */
251         return newObj;
252     }
253 }
Popular Tags