001    package org.apache.ojb.broker.cache;
002    
003    /* Copyright 2002-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.PersistenceBroker;
019    import org.apache.ojb.broker.util.logging.Logger;
020    import org.apache.ojb.broker.util.logging.LoggerFactory;
021    
022    /**
023     * Factory for {@link ObjectCache} implementation classes.
024     * Each given {@link org.apache.ojb.broker.PersistenceBroker}
025     * was associated with its own <code>ObjectCache</code> instance
026     * and vice versa.
027     *
028     * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
029     * @version $Id: ObjectCacheFactory.java,v 1.1 2007-08-24 22:17:29 ewestfal Exp $
030     */
031    
032    public class ObjectCacheFactory
033    {
034        private Logger log = LoggerFactory.getLogger(ObjectCacheFactory.class);
035    
036        private static ObjectCacheFactory singleton = new ObjectCacheFactory();
037    
038        /**
039         * Get the ObjectCacheFactory instance.
040         */
041        public static ObjectCacheFactory getInstance()
042        {
043            return singleton;
044        }
045    
046        private ObjectCacheFactory()
047        {
048        }
049    
050        /**
051         * Creates a new {@link ObjectCacheInternal} instance. Each <tt>ObjectCache</tt>
052         * implementation was wrapped by a {@link CacheDistributor} and the distributor
053         * was wrapped by {@link MaterializationCache}.
054         *
055         * @param broker The PB instance to associate with the cache instance
056         */
057        public MaterializationCache createObjectCache(PersistenceBroker broker)
058        {
059            CacheDistributor cache = null;
060            try
061            {
062                log.info("Start creating new ObjectCache instance");
063                /*
064                1.
065                if default cache was not found,
066                create an new instance of the default cache specified in the
067                configuration.
068                2.
069                Then instantiate AllocatorObjectCache to handle
070                per connection/ per class caching instances.
071                3.
072                To support intern operations we wrap ObjectCache with an
073                InternalObjectCache implementation
074                */
075                cache = new CacheDistributor(broker);
076                log.info("Instantiate new " + cache.getClass().getName() + " for PB instance " + broker);
077            }
078            catch(Exception e)
079            {
080                log.error("Error while initiation, please check your configuration" +
081                        " files and the used implementation class", e);
082            }
083            log.info("New ObjectCache instance was created");
084            return new MaterializationCache(cache);
085        }
086    }