001: package org.apache.ojb.broker.cache;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.ojb.broker.Identity;
019:
020: /**
021: * An abstract 'meta' implementation of the {@link ObjectCache}
022: * interace.
023: * <br/>
024: * Implement the abstract {@link #getCache} method in sub-classes.
025: * All base Object/Identity validation is done by this class.
026: *
027: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
028: * @version $Id: AbstractMetaCache.java,v 1.5.2.2 2005/12/21 22:24:15 tomdz Exp $
029: */
030: public abstract class AbstractMetaCache implements ObjectCache {
031: public static final int METHOD_CACHE = 1;
032: public static final int METHOD_LOOKUP = 2;
033: public static final int METHOD_REMOVE = 3;
034:
035: /**
036: * This method handle all calls against the {@link ObjectCache} interface.
037: * Note: The parameter <code>obj</code> can be <code>null</code> - e.g. when
038: * lookup or remove method was called.
039: *
040: * @param oid Identity of the target object.
041: * @param obj The target object itself or <code>null</code> if not available.
042: * @param callingMethod Specifies the type of method call against the {@link ObjectCache}
043: * interface. {@link #METHOD_CACHE}, {@link #METHOD_LOOKUP}, {@link #METHOD_REMOVE}.
044: * @return The {@link ObjectCache} implementation.
045: */
046: public abstract ObjectCache getCache(Identity oid, Object obj,
047: int callingMethod);
048:
049: /**
050: * Caches the given object using the given Identity as key
051: *
052: * @param oid The Identity key
053: * @param obj The object o cache
054: */
055: public void cache(Identity oid, Object obj) {
056: if (oid != null && obj != null) {
057: ObjectCache cache = getCache(oid, obj, METHOD_CACHE);
058: if (cache != null) {
059: cache.cache(oid, obj);
060: }
061: }
062: }
063:
064: /**
065: * We delegate this method to the standard cache method.
066: * <br/>
067: * ++ Override if needed ++
068: */
069: public boolean cacheIfNew(Identity oid, Object obj) {
070: cache(oid, obj);
071: return true;
072: }
073:
074: /**
075: * Looks up the object from the cache
076: *
077: * @param oid The Identity to look up the object for
078: * @return The object if found, otherwise null
079: */
080: public Object lookup(Identity oid) {
081: Object ret = null;
082: if (oid != null) {
083: ObjectCache cache = getCache(oid, null, METHOD_LOOKUP);
084: if (cache != null) {
085: ret = cache.lookup(oid);
086: }
087: }
088: return ret;
089: }
090:
091: /**
092: * Removes the given object from the cache
093: *
094: * @param oid oid of the object to remove
095: */
096: public void remove(Identity oid) {
097: if (oid == null)
098: return;
099:
100: ObjectCache cache = getCache(oid, null, METHOD_REMOVE);
101: if (cache != null) {
102: cache.remove(oid);
103: }
104: }
105: }
|