package org.apache.ojb.broker.cache;

/* Copyright  2004 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Properties;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.jcs.JCS;
import org.apache.jcs.access.exception.CacheException;
import org.apache.ojb.broker.Identity;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.cache.JCSHelper;
import org.apache.ojb.broker.cache.ObjectCache;
import org.apache.ojb.broker.cache.RuntimeCacheException;

/**
 * This local [EMAIL PROTECTED] ObjectCache} implementation using
 * <a href="http://jakarta.apache.org/turbine/jcs/index.html";>
 * turbine-JCS</a> to cache objects is primarily for intern use in
 * conjunction with [EMAIL PROTECTED] ObjectCacheJCSPerClassImpl} implementation. If
 * used as main <code>ObjectCache</code> all cached objects will be cached
 * under the same JCS region name (see [EMAIL PROTECTED] JCSHelper#DEFAULT_REGION}).
 *
 * <p>
 * Implementation configuration properties:
 * </p>
 *
 * <table cellspacing="2" cellpadding="2" border="3" frame="box">
 * <tr>
 *     <td><strong>Property Key</strong></td>
 *     <td><strong>Property Values</strong></td>
 * </tr>
 * <tr>
 *     <td> - </td>
 *     <td>
 *          -
 *    </td>
 * </tr>
 * </table>
 *
 * @author Matthew Baird ([EMAIL PROTECTED]);
 * @version $Id: ObjectCacheJCSImpl.java,v 1.9 2004/03/11 18:16:09 brianm
Exp $
 */
public class ObjectCacheJCSImpl implements ObjectCache {

        private JCS jcsCache;
        /**
         * if no regionname is passed in, we use the default region.
         */
        private String regionName = JCSHelper.DEFAULT_REGION;

        public ObjectCacheJCSImpl(PersistenceBroker broker, Properties prop)
        {
                this(null);
        }

        /**
         * Constructor used by the [EMAIL PROTECTED] ObjectCacheJCSPerClassImpl}
         */
        public ObjectCacheJCSImpl(String name)
        {
                regionName = name != null ? name : regionName;
                jcsCache = JCSHelper.newInstance(regionName);
        }

        public String getRegionName()
        {
                return regionName;
        }

        /**
         * makes object obj persistent to the Objectcache under the key oid.
         */
        public void cache(Identity oid, Object obj)
        {
                try
                {
                        jcsCache.put(oid.toString(), obj);
                }
                catch (CacheException e)
                {
                        throw new RuntimeCacheException(e);
                }
        }

        /**
         * Lookup object with Identity oid in objectTable.
         * returns null if no matching id is found
         */
        public Object lookup(Identity oid)
        {
                return jcsCache.get(oid.toString());
        }

        /**
         * removes an Object from the cache.
         * @param oid the Identity of the object to be removed.
         */
        public void remove(Identity oid)
        {
                try
                {
                        jcsCache.remove(oid.toString());
                }
                catch (CacheException e)
                {
                        throw new RuntimeCacheException(e.getMessage());
                }
        }

        /**
         * clear the ObjectCache.
         */
        public void clear()
        {
                if (jcsCache != null)
                {
                        try
                        {
                                jcsCache.remove();
                        }
                        catch (CacheException e)
                        {
                                throw new RuntimeCacheException(e);
                        }
                }
        }

        public String toString()
        {
                ToStringBuilder buf = new ToStringBuilder(this,
ToStringStyle.DEFAULT_STYLE);
                buf.append("JCS region name", regionName);
                buf.append("JCS region", jcsCache);
                return buf.toString();
        }

}


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to