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.descriptors.invalidation;
038:
039: import oracle.toplink.essentials.internal.identitymaps.CacheKey;
040:
041: /**
042: * PUBLIC:
043: * A CacheInvalidationPolicy is used to set objects in TopLink's identity maps to be invalid
044: * following given rules. CacheInvalidationPolicy is the abstract superclass for all
045: * policies used for cache invalidation.
046: * By default in TopLink, objects do not expire in the cache. Several different policies
047: * are available to allow objects to expire. These can be set on the Descriptor.
048: * @see oracle.toplink.essentials.publicinterface.Descriptor
049: * @see oracle.toplink.essentials.descriptors.cacheinvalidation.NoExpiryCacheInvalidationPolicy
050: * @see oracle.toplink.essentials.descriptors.cacheinvalidation.DailyCacheInvalidationPolicy
051: * @see oracle.toplink.essentials.descriptors.cacheinvalidation.TimeToLiveCacheInvalidationPolicy
052: */
053: public abstract class CacheInvalidationPolicy implements
054: java.io.Serializable {
055: public static final long NO_EXPIRY = -1;
056:
057: /** this will represent objects that do not expire */
058: protected boolean shouldUpdateReadTimeOnUpdate = false;
059:
060: /**
061: * INTERNAL:
062: * Get the next time when this object will become invalid
063: */
064: public abstract long getExpiryTimeInMillis(CacheKey key);
065:
066: /**
067: * INTERNAL:
068: * Return the remaining life of this object
069: */
070: public long getRemainingValidTime(CacheKey key) {
071: long expiryTime = getExpiryTimeInMillis(key);
072: long remainingTime = expiryTime - System.currentTimeMillis();
073: if (remainingTime > 0) {
074: return remainingTime;
075: }
076: return 0;
077: }
078:
079: /**
080: * INTERNAL:
081: * return true if this object is expire, false otherwise.
082: */
083: public abstract boolean isInvalidated(CacheKey key,
084: long currentTimeMillis);
085:
086: /**
087: * PUBLIC:
088: * Set whether to update the stored time an object was read when an object is updated.
089: * When the read time is updated, it indicates to TopLink that the data in the object
090: * is up to date. This means that cache invalidation checks will occur relative to the
091: * new read time.
092: * By default, the read time will not be updated when an object is updated.
093: * Often it is possible to be confident that the object is up to date after an update
094: * because otherwise the update will fail because of the locking policies in use.
095: */
096: public void setShouldUpdateReadTimeOnUpdate(
097: boolean shouldUpdateReadTime) {
098: shouldUpdateReadTimeOnUpdate = shouldUpdateReadTime;
099: }
100:
101: /**
102: * PUBLIC:
103: * Return whether objects affected by this CacheInvalidationPolicy should have
104: * the read time on their cache keys updated when an update occurs.
105: */
106: public boolean shouldUpdateReadTimeOnUpdate() {
107: return shouldUpdateReadTimeOnUpdate;
108: }
109: }
|