001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * // Copyright (c) 1998, 2007, Oracle. All rights reserved.
005: *
006: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common Development
009: * and Distribution License("CDDL") (collectively, the "License"). You
010: * may not use this file except in compliance with the License. You can obtain
011: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
012: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
013: * language governing permissions and limitations under the License.
014: *
015: * When distributing the software, include this License Header Notice in each
016: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
017: * Sun designates this particular file as subject to the "Classpath" exception
018: * as provided by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the License
020: * Header, with the fields enclosed by brackets [] replaced by your own
021: * identifying information: "Portions Copyrighted [year]
022: * [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * If you wish your version of this file to be governed by only the CDDL or
027: * only the GPL Version 2, indicate your decision by adding "[Contributor]
028: * elects to include this software in this distribution under the [CDDL or GPL
029: * Version 2] license." If you don't indicate a single choice of license, a
030: * recipient has the option to distribute your version of this file under
031: * either the CDDL, the GPL Version 2 or to extend the choice of license to
032: * its licensees as provided above. However, if you add GPL Version 2 code
033: * and therefore, elected the GPL Version 2 license, then the option applies
034: * only if the new code is made subject to such option by the copyright
035: * holder.
036: */
037: package oracle.toplink.essentials.internal.identitymaps;
038:
039: import java.io.*;
040: import java.util.Arrays;
041: import java.util.Vector;
042: import oracle.toplink.essentials.internal.helper.*;
043: import oracle.toplink.essentials.sessions.Record;
044:
045: /**
046: * <p><b>Purpose</b>: Container class for storing objects in an IdentityMap.
047: * <p><b>Responsibilities</b>:<ul>
048: * <li> Hold key and object.
049: * <li> Maintain and update the current writeLockValue.
050: * </ul>
051: * @since TOPLink/Java 1.0
052: */
053: public class CacheKey implements Serializable, Cloneable {
054:
055: /** The key holds the vector of primary key values for the object. */
056: protected Vector key;
057:
058: /** Calculated hash value for CacheKey from primary key values. */
059: protected int hash;
060: protected Object object;
061:
062: //used to store a reference to the map this cachkey is in in cases where the
063: //cache key is to be removed, prevents us from having to track down the owning
064: //map
065: protected IdentityMap mapOwner;
066:
067: /** The writeLock value is being held as an object so that it might contain a number or timestamp. */
068: protected Object writeLockValue;
069:
070: /** The cached wrapper for the object, used in EJB. */
071: protected Object wrapper;
072:
073: /** The cache key hold a reference to the concurrency manager to perform the cache key level locking. */
074: protected ConcurrencyManager mutex;
075:
076: /** This is used for Document Preservation to cache the record that this object was built from */
077: protected Record record;
078:
079: /** This attribute is the system time in milli seconds that the object was last refreshed on */
080:
081: //CR #4365
082: // CR #2698903 - fix for the previous fix. No longer using millis.
083: protected long lastUpdatedQueryId;
084:
085: /** Invalidation State can be used to indicate whether this cache key is considered valid */
086: protected int invalidationState = CHECK_INVALIDATION_POLICY;
087:
088: /** The following constants are used for the invalidationState variable */
089: public static final int CHECK_INVALIDATION_POLICY = 0;
090: public static final int CACHE_KEY_INVALID = -1;
091:
092: /** The read time stores the millisecond value of the last time the object help by
093: this cache key was confirmed as up to date. */
094: protected long readTime = 0;
095:
096: public CacheKey(Vector primaryKeys) {
097: this .key = primaryKeys;
098: this .hash = computeHash(primaryKeys);
099: }
100:
101: public CacheKey(Vector primaryKey, Object object, Object lockValue) {
102: this (primaryKey);
103: //bug4649617 use setter instead of this.object = object to avoid hard reference on object in subclasses
104: setObject(object);
105: this .writeLockValue = lockValue;
106: }
107:
108: public CacheKey(Vector primaryKey, Object object, Object lockValue,
109: long readTime) {
110: this (primaryKey, object, lockValue);
111: this .readTime = readTime;
112: }
113:
114: /**
115: * Acquire the lock on the cachek key object.
116: */
117: public void acquire() {
118: getMutex().acquire(false);
119: }
120:
121: /**
122: * Acquire the lock on the cachek key object. For the merge process
123: * called with true from the merge process, if true then the refresh will not refresh the object
124: */
125: public void acquire(boolean forMerge) {
126: getMutex().acquire(forMerge);
127: }
128:
129: /**
130: * Acquire the lock on the cache key object. But only if the object has no lock on it
131: * Added for CR 2317
132: */
133: public boolean acquireNoWait() {
134: return getMutex().acquireNoWait(false);
135: }
136:
137: /**
138: * Acquire the lock on the cache key object. But only if the object has no lock on it
139: * Added for CR 2317
140: * called with true from the merge process, if true then the refresh will not refresh the object
141: */
142: public boolean acquireNoWait(boolean forMerge) {
143: return getMutex().acquireNoWait(forMerge);
144: }
145:
146: /**
147: * Acquire the deferred lcok.
148: */
149: public void acquireDeferredLock() {
150: getMutex().acquireDeferredLock();
151: }
152:
153: /**
154: * Check the read lock on the cachek key object.
155: * This can be called to ensure the cache key has a valid built object.
156: * It does not hold a lock, so the object could be refreshed afterwards.
157: */
158: public void checkReadLock() {
159: getMutex().checkReadLock();
160: }
161:
162: /**
163: * Acquire the read lock on the cachek key object.
164: */
165: public void acquireReadLock() {
166: getMutex().acquireReadLock();
167: }
168:
169: /**
170: * Acquire the read lock on the cache key object.
171: */
172: public boolean acquireReadLockNoWait() {
173: return getMutex().acquireReadLockNoWait();
174: }
175:
176: /**
177: * INTERNAL:
178: * Clones itself.
179: */
180: public Object clone() {
181: Object object = null;
182:
183: try {
184: object = super .clone();
185: } catch (Exception exception) {
186: throw new InternalError(exception.toString());
187: }
188:
189: return object;
190: }
191:
192: /**
193: * Compute a hash value for the CacheKey dependent upon the values of the primary key
194: * instead of the identity of the receiver.
195: * This method is intended for use by constructors only.
196: */
197: protected int computeHash(Vector primaryKey) {
198: int computedHashValue = 0;
199:
200: for (int index = 0; index < primaryKey.size(); index++) {
201: Object value = primaryKey.elementAt(index);
202: if (value != null) {
203: //gf bug 1193: fix to handle array hashcodes properly
204: if (value.getClass().isArray()) {
205: computedHashValue = computedHashValue
206: ^ computeArrayHashCode(value);
207: } else {
208: computedHashValue = computedHashValue
209: ^ (value.hashCode());
210: }
211: }
212: }
213: return computedHashValue;
214: }
215:
216: /**
217: * Compute the hashcode for supported array types
218: * @param obj - an array
219: * @return hashCode value
220: */
221: private int computeArrayHashCode(Object obj) {
222: if (obj.getClass() == ClassConstants.APBYTE) {
223: return Arrays.hashCode((byte[]) obj);
224: } else if (obj.getClass() == ClassConstants.APCHAR) {
225: return Arrays.hashCode((char[]) obj);
226: } else {
227: return Arrays.hashCode((Object[]) obj);
228: }
229: }
230:
231: /**
232: * Determine if the receiver is equal to anObject.
233: * If anObject is a CacheKey, do further comparison, otherwise, return false.
234: * @see CacheKey#equals(CacheKey)
235: */
236: public boolean equals(Object object) {
237: if (object instanceof CacheKey) {
238: return equals((CacheKey) object);
239: }
240:
241: return false;
242: }
243:
244: /**
245: * Determine if the receiver is equal to key.
246: * Use an index compare, because it is much faster than enumerations.
247: */
248: public boolean equals(CacheKey key) {
249: if (this == key) {
250: return true;
251: }
252: if (getKey().size() == key.getKey().size()) {
253: for (int index = 0; index < getKey().size(); index++) {
254: Object myValue = getKey().elementAt(index);
255: Object comparisionValue = key.getKey().elementAt(index);
256:
257: if (myValue == null) {
258: if (comparisionValue != null) {
259: return false;
260: }
261: } else if (myValue.getClass().isArray()) {
262: //gf bug 1193: fix array comparison logic to exit if they don't match, and continue the loop if they do
263: if (((myValue.getClass() == ClassConstants.APBYTE) && (comparisionValue
264: .getClass() == ClassConstants.APBYTE))) {
265: if (!Helper.compareByteArrays((byte[]) myValue,
266: (byte[]) comparisionValue)) {
267: return false;
268: }
269: } else if (((myValue.getClass() == ClassConstants.APCHAR) && (comparisionValue
270: .getClass() == ClassConstants.APCHAR))) {
271: if (!Helper.compareCharArrays((char[]) myValue,
272: (char[]) comparisionValue)) {
273: return false;
274: }
275: } else {
276: if (!Helper.compareArrays((Object[]) myValue,
277: (Object[]) comparisionValue)) {
278: return false;
279: }
280: }
281: } else {
282: if (!(myValue.equals(comparisionValue))) {
283: return false;
284: }
285: }
286: }
287: return true;
288: }
289: return false;
290: }
291:
292: /**
293: * INTERNAL:
294: * This method returns the system time in millis seconds at which this object was last refreshed
295: * CR #4365
296: * CR #2698903 ... instead of using millis we will now use id's instead. Method
297: * renamed appropriately.
298: */
299: public long getLastUpdatedQueryId() {
300: return this .lastUpdatedQueryId;
301: }
302:
303: public Vector getKey() {
304: return key;
305: }
306:
307: /**
308: * Return the concurrency manager.
309: */
310: public synchronized ConcurrencyManager getMutex() {
311: if (mutex == null) {
312: mutex = new ConcurrencyManager(this );
313: }
314: return mutex;
315: }
316:
317: public Object getObject() {
318: return object;
319: }
320:
321: public IdentityMap getOwningMap() {
322: return this .mapOwner;
323: }
324:
325: /**
326: * INTERNAL:
327: * Return the current value of the Read Time variable
328: */
329: public long getReadTime() {
330: return readTime;
331: }
332:
333: public Record getRecord() {
334: return record;
335: }
336:
337: public Object getWrapper() {
338: return wrapper;
339: }
340:
341: public Object getWriteLockValue() {
342: return writeLockValue;
343: }
344:
345: /**
346: * Overrides hashCode() in Object to use the primaryKey's hashCode for storage in data structures.
347: */
348: public int hashCode() {
349: return hash;
350: }
351:
352: /**
353: * Return if the lock is acquired
354: */
355: public boolean isAcquired() {
356: return getMutex().isAcquired();
357: }
358:
359: /**
360: * INTERNAL:
361: * Return the value of the invalidationState Variable
362: * The return value will be a constant
363: * CHECK_INVALIDATION_POLICY - The Invalidation policy is must be checked for this cache key's sate
364: * CACHE_KEY_INVALID - This cache key has been labeled invalid.
365: */
366: public int getInvalidationState() {
367: return invalidationState;
368: }
369:
370: /**
371: * Release the lock on the cachek key object.
372: */
373: public void release() {
374: getMutex().release();
375: }
376:
377: /**
378: * Release the deferred lock
379: */
380: public void releaseDeferredLock() {
381: getMutex().releaseDeferredLock();
382: }
383:
384: /**
385: * Release the read lock on the cachek key object.
386: */
387: public void releaseReadLock() {
388: getMutex().releaseReadLock();
389: }
390:
391: /**
392: * INTERNAL:
393: * Set the value of the invalidationState Variable
394: * The possible values are from an enumeration of constants
395: * CHECK_INVALIDATION_POLICY - The invalidation policy is must be checked for this cache key's sate
396: * CACHE_KEY_INVALID - This cache key has been labeled invalid.
397: */
398: public void setInvalidationState(int invalidationState) {
399: this .invalidationState = invalidationState;
400: }
401:
402: /**
403: * INTERNAL:
404: * This method sets the system time in millis seconds at which this object was last refreshed
405: * CR #4365
406: * CR #2698903 ... instead of using millis we will now use ids instead. Method
407: * renamed appropriately.
408: */
409: public void setLastUpdatedQueryId(long id) {
410: this .lastUpdatedQueryId = id;
411: }
412:
413: public void setKey(Vector key) {
414: this .key = key;
415: this .hash = computeHash(key);
416: }
417:
418: /**
419: * Set the concurrency manager.
420: */
421: public void setMutex(ConcurrencyManager mutex) {
422: this .mutex = mutex;
423: }
424:
425: public void setObject(Object object) {
426: this .object = object;
427: }
428:
429: public void setOwningMap(IdentityMap map) {
430: this .mapOwner = map;
431: }
432:
433: /**
434: * INTERNAL:
435: * Set the read time of this cache key
436: */
437: public void setReadTime(long readTime) {
438: this .readTime = readTime;
439: invalidationState = CHECK_INVALIDATION_POLICY;
440: }
441:
442: public void setRecord(Record newRecord) {
443: this .record = newRecord;
444: }
445:
446: public void setWrapper(Object wrapper) {
447: this .wrapper = wrapper;
448: }
449:
450: public void setWriteLockValue(Object writeLockValue) {
451: this .writeLockValue = writeLockValue;
452: }
453:
454: public String toString() {
455: int hashCode = 0;
456: if (getObject() != null) {
457: hashCode = getObject().hashCode();
458: }
459:
460: return "[" + getKey() + ": " + hashCode + ": "
461: + getWriteLockValue() + ": " + getReadTime() + ": "
462: + getObject() + "]";
463: }
464:
465: /**
466: * Notifies that cache key that it has been accessed.
467: * Allows the LRU sub-cache to be maintained.
468: */
469: public void updateAccess() {
470: // Nothing required by default.
471: }
472: }
|