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.1 2007-08-24 22:17:29 ewestfal Exp $
029     */
030    public abstract class AbstractMetaCache implements ObjectCache
031    {
032        public static final int METHOD_CACHE = 1;
033        public static final int METHOD_LOOKUP = 2;
034        public static final int METHOD_REMOVE = 3;
035    
036        /**
037         * This method handle all calls against the {@link ObjectCache} interface.
038         * Note: The parameter <code>obj</code> can be <code>null</code> - e.g. when
039         * lookup or remove method was called.
040         *
041         * @param oid Identity of the target object.
042         * @param obj The target object itself or <code>null</code> if not available.
043         * @param callingMethod Specifies the type of method call against the {@link ObjectCache}
044         * interface. {@link #METHOD_CACHE}, {@link #METHOD_LOOKUP}, {@link #METHOD_REMOVE}.
045         * @return The {@link ObjectCache} implementation.
046         */
047        public abstract ObjectCache getCache(Identity oid, Object obj, 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        {
057            if (oid != null && obj != null)
058            {
059                ObjectCache cache = getCache(oid, obj, METHOD_CACHE);
060                if (cache != null)
061                {
062                    cache.cache(oid, obj);
063                }
064            }
065        }
066    
067        /**
068         * We delegate this method to the standard cache method.
069         * <br/>
070         * ++ Override if needed ++
071         */
072        public boolean cacheIfNew(Identity oid, Object obj)
073        {
074            cache(oid, obj);
075            return true;
076        }
077    
078        /**
079         * Looks up the object from the cache
080         *
081         * @param oid  The Identity to look up the object for
082         * @return     The object if found, otherwise null
083         */
084        public Object lookup(Identity oid)
085        {
086            Object ret = null;
087            if (oid != null)
088            {
089                ObjectCache cache = getCache(oid, null, METHOD_LOOKUP);
090                if (cache != null)
091                {
092                    ret = cache.lookup(oid);
093                }
094            }
095            return ret;
096        }
097    
098        /**
099         * Removes the given object from the cache
100         *
101         * @param oid  oid of the object to remove
102         */
103        public void remove(Identity oid)
104        {
105            if (oid == null) return;
106    
107            ObjectCache cache = getCache(oid, null, METHOD_REMOVE);
108            if (cache != null)
109            {
110                cache.remove(oid);
111            }
112        }
113    }