001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.core.dbobj;
066:
067: import com.jcorporate.expresso.core.cache.CacheException;
068: import com.jcorporate.expresso.core.cache.CacheManager;
069: import com.jcorporate.expresso.core.cache.CacheSystem;
070: import com.jcorporate.expresso.core.dataobjects.DataField;
071: import com.jcorporate.expresso.core.dataobjects.DefaultDataField;
072: import com.jcorporate.expresso.core.dataobjects.jdbc.JDBCObjectMetaData;
073: import com.jcorporate.expresso.core.db.DBException;
074: import org.apache.log4j.Logger;
075:
076: import java.util.Iterator;
077:
078: /**
079: * Utility methods for putting a DBObject into a cache.
080: *
081: * @author Michael Rimov
082: */
083:
084: public class CacheUtils {
085:
086: private static final Logger log = Logger
087: .getLogger(CacheUtils.class);
088:
089: public CacheUtils() {
090: }
091:
092: /**
093: * Adds a non-modified object to the cache. This is most often called
094: * after DataObject.retrieve(). No related cache notification will occur
095: * when this happens
096: *
097: * @param theDBObj the object to add to the cache.
098: * @throws DBException upon add error (either Cache related or dbobject related)
099: */
100: public void addUnmodifiedToCache(DBObject theDBObj)
101: throws DBException {
102: JDBCObjectMetaData metadata = theDBObj.getJDBCMetaData();
103: String 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
121: .getDataContext());
122: if (cs == null) {
123: return;
124: }
125: try {
126: prepareCache(cs, cacheSize, theClassName);
127: DBObject newObj = prepareForStorage(theDBObj, metadata);
128:
129: Integer i = (Integer) 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, i.intValue() * 1000 * 60);
135: }
136:
137: if (log.isDebugEnabled()) {
138: log.debug("Adding item " + theDBObj.getKey()
139: + " to cache for " + theClassName);
140: }
141: } catch (CacheException ce) {
142: throw new DBException(ce);
143: }
144:
145: }
146:
147: /**
148: * When using basic caching, this method is used to add a new DB object to the
149: * cache, where it is later used when retrieveing data instead of going directly
150: * to the database. This functionality can be controlled with the "dbCache"
151: * property in the properties file(s).
152: *
153: * @param theDBObj com.jcorporate.expresso.core.dbobj.DBObject The object to be
154: * cached
155: * @throws DBException upon add error (either Cache related or dbobject related)
156: */
157: public void addToCache(DBObject theDBObj) throws DBException {
158: JDBCObjectMetaData metadata = theDBObj.getJDBCMetaData();
159: String theClassName = theDBObj.getClass().getName();
160:
161: //
162: //Don't cache anything without key fields
163: //
164: if (metadata.getKeyFieldListArray().size() == 0) {
165: return;
166: }
167:
168: /**
169: * Don't even proceed if cache size is 0
170: */
171: int cacheSize = theDBObj.getCacheSize();
172: if (cacheSize == 0) {
173: return;
174: }
175:
176: CacheSystem cs = CacheManager.getCacheSystem(theDBObj
177: .getDataContext());
178: if (cs == null) {
179: return;
180: }
181: try {
182: prepareCache(cs, cacheSize, theClassName);
183: DBObject newObj = prepareForStorage(theDBObj, metadata);
184:
185: Integer i = (Integer) theDBObj.getAttribute("TTL");
186: if (i == null || i.intValue() == 0) {
187: cs.addItem(theClassName, newObj);
188: } else {
189: //Set the cache value.
190: cs.addItem(theClassName, newObj,
191: i.intValue() * 1000 * 60);
192: }
193:
194: if (log.isDebugEnabled()) {
195: log.debug("Adding item " + theDBObj.getKey()
196: + " to cache for " + theClassName);
197: }
198: } catch (CacheException ce) {
199: throw new DBException(ce);
200: }
201: } /* addToCache(DBObject) */
202:
203: /**
204: * Creates the appropriate cache if it doesn't already exist
205: *
206: * @param cs the cache system to add
207: * @param cacheSize the size of the cache
208: * @param cacheName the name of the cache to prepare
209: * @throws CacheException upon creation error
210: */
211: private void prepareCache(CacheSystem cs, int cacheSize,
212: String cacheName) throws CacheException {
213: if (!cs.existsCache(cacheName)) {
214: cs.createCache(cacheName, false, cacheSize);
215: }
216:
217: }
218:
219: /**
220: * Creates a DBObject for storage in a cache, prepares the cache storage
221: *
222: * @param theDBObj the source object
223: * @param metadata the object's metadata
224: * @return DBObject instance ready to store
225: * @throws DBException upon DBObject access error
226: */
227: private DBObject prepareForStorage(DBObject theDBObj,
228: JDBCObjectMetaData metadata) throws DBException {
229:
230: DBObject newObj = theDBObj.newInstance();
231: newObj.setDataContext(theDBObj.getDataContext());
232:
233: DBField oneField = null;
234:
235: for (Iterator i = metadata.getAllFieldsMap().values()
236: .iterator(); i.hasNext();) {
237: oneField = (DBField) i.next();
238:
239: if (!oneField.isVirtual() && !oneField.isBinaryObjectType()) {
240: DataField df = DefaultDataField.getInstance(oneField,
241: newObj);
242: df.setValue(theDBObj.getDataField(oneField.getName())
243: .getValue());
244: newObj.setDataField(oneField.getName(), df);
245: }
246: } /* for each field */
247: return newObj;
248: }
249: }
|