001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.cache;
011:
012: import java.io.Serializable;
013: import java.util.Map;
014: import java.util.HashMap;
015:
016: /**
017: * A CachePolicy object determines for a given object whether it should be cached or not, and how.
018: * Code that makes use of a cache should use a CachePolicy object, when available, to determine if the
019: * object should be cached or not.
020: *
021: * @since MMBase 1.8
022: * @author Pierre van Rooden
023: * @version $Id: CachePolicy.java,v 1.4 2007/02/11 19:21:11 nklasens Exp $
024: */
025: abstract public class CachePolicy implements Serializable {
026:
027: // map with all known policies
028: static private Map<Object, CachePolicy> policies = new HashMap<Object, CachePolicy>();
029:
030: /**
031: * Standard cache policy that advises to never cache a passed object.
032: * Accessible with the key "never".
033: */
034: static public CachePolicy NEVER = new CachePolicy("never") {
035: public boolean checkPolicy(Object o) {
036: return false;
037: }
038:
039: public String getDescription() {
040: return "CACHE NEVER";
041: }
042: };
043:
044: /**
045: * Standard cache policy that advises to always cache a passed object.
046: * Accessible with the key "always".
047: */
048: static public CachePolicy ALWAYS = new CachePolicy("always") {
049: public boolean checkPolicy(Object o) {
050: return true;
051: }
052:
053: public String getDescription() {
054: return "CACHE ALWAYS";
055: }
056: };
057:
058: /**
059: * Obtains a cache policy given a policy key.
060: * @param policyKey the key of the cache policy
061: * @return the policy key
062: * @throws IllegalArgumentException if the policy does not exist
063: */
064: static public CachePolicy getPolicy(Object policyKey) {
065: CachePolicy policy = policies.get(policyKey);
066: if (policy == null) {
067: throw new IllegalArgumentException(
068: "There is no cache policy known with key '"
069: + policyKey + "'");
070: }
071: return policy;
072: }
073:
074: static public void putPolicy(Object policyKey, CachePolicy policy) {
075: policies.put(policyKey, policy);
076: }
077:
078: /**
079: * Instantiates a new cache policy, and registers it with the given policy key.
080: */
081: protected CachePolicy(Object policyKey) {
082: CachePolicy.putPolicy(policyKey, this );
083: }
084:
085: /**
086: * Instantiates a new cache policy without registering it
087: */
088: protected CachePolicy() {
089: }
090:
091: /**
092: * Checks whether the policy advises to cache the passed object.
093: * @param o the object to check the cache for
094: * @return <code>true</code> if the policy advises to cache this object, <code>false</code> otherwise.
095: */
096: abstract public boolean checkPolicy(Object o);
097:
098: /**
099: * Returns a description of the policy.
100: */
101: public String getDescription() {
102: return getClass().getName();
103: }
104:
105: }
|