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 java.util.Collections;
019    import java.util.HashMap;
020    import java.util.Iterator;
021    import java.util.Map;
022    import java.util.Properties;
023    
024    import org.apache.ojb.broker.Identity;
025    import org.apache.ojb.broker.PersistenceBroker;
026    
027    /**
028     * Global {@link ObjectCache} implementation.
029     *
030     * @author matthew.baird
031     * @version $Id: ObjectCachePerClassImpl.java,v 1.1 2007-08-24 22:17:29 ewestfal Exp $
032     */
033    public class ObjectCachePerClassImpl extends AbstractMetaCache
034    {
035        private static Map cachesByClass = Collections.synchronizedMap(new HashMap());
036    
037        /**
038         * Constructor for the ObjectCachePerClassImpl object
039         */
040        public ObjectCachePerClassImpl(PersistenceBroker broker, Properties prop)
041        {
042            setClassCache(Object.class, new ObjectCacheDefaultImpl(broker, null));
043        }
044    
045        public ObjectCache getCache(Identity oid, Object obj, int methodCall)
046        {
047            if(oid.getObjectsRealClass() == null)
048            {
049                return null;
050            }
051            else
052            {
053                return getCachePerClass(oid.getObjectsRealClass(), methodCall);
054            }
055        }
056    
057        /**
058         * Clears the cache
059         */
060        public void clear()
061        {
062            Iterator it = cachesByClass.values().iterator();
063            while (it.hasNext())
064            {
065                ObjectCache cache = (ObjectCache) it.next();
066                if (cache != null)
067                {
068                    cache.clear();
069                }
070            }
071        }
072    
073        /**
074         * Sets the ObjectCache implementation to use for objects with the given
075         * type and subclasses
076         *
077         * @param objectClass The object's class, use java.lang.Object to alter
078         *                    default caching for all objects which have no special
079         *                    caching defined
080         * @param cache       The new ObjectCache implementation to use for this
081         *                    class and subclasses, null to switch off caching
082         *                    for the given class
083         */
084        public void setClassCache(Class objectClass, ObjectCache cache)
085    
086        {
087            setClassCache(objectClass.getName(), cache);
088        }
089    
090        /**
091         * Sets the ObjectCache implementation for the given class name
092         *
093         * @param className The name of the class to cache
094         * @param cache     The ObjectCache to use for this class and subclasses
095         */
096        private void setClassCache(String className, ObjectCache cache)
097        {
098            cachesByClass.put(className, cache);
099        }
100    
101        /**
102         * Gets the cache for the given class
103         *
104         * @param objectClass The class to look up the cache for
105         * @return The cache
106         */
107        private ObjectCache getCachePerClass(Class objectClass, int methodCall)
108        {
109            ObjectCache cache = (ObjectCache) cachesByClass.get(objectClass.getName());
110            if (cache == null && AbstractMetaCache.METHOD_CACHE == methodCall
111                    && !cachesByClass.containsKey(objectClass.getName()))
112            {
113                cache = new ObjectCacheDefaultImpl(null, null);
114                setClassCache(objectClass.getName(), cache);
115            }
116            return cache;
117        }
118    }