001    package org.apache.ojb.broker.cache;
002    
003    /* Copyright 2004-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.Properties;
019    
020    import org.apache.commons.lang.builder.ToStringBuilder;
021    import org.apache.commons.lang.builder.ToStringStyle;
022    import org.apache.jcs.JCS;
023    import org.apache.jcs.access.exception.CacheException;
024    import org.apache.jcs.access.exception.ObjectExistsException;
025    import org.apache.ojb.broker.Identity;
026    import org.apache.ojb.broker.PersistenceBroker;
027    
028    /**
029     * This local {@link ObjectCache} implementation using
030     * <a href="http://jakarta.apache.org/turbine/jcs/index.html">
031     * turbine-JCS</a> to cache objects is primarily for intern use in
032     * conjunction with {@link ObjectCacheJCSPerClassImpl} implementation. If
033     * used as main <code>ObjectCache</code> all cached objects will be cached
034     * under the same JCS region name (see {@link #DEFAULT_REGION}).
035     * <p/>
036     * <p/>
037     * Implementation configuration properties:
038     * </p>
039     * <p/>
040     * <table cellspacing="2" cellpadding="2" border="3" frame="box">
041     * <tr>
042     * <td><strong>Property Key</strong></td>
043     * <td><strong>Property Values</strong></td>
044     * </tr>
045     * <tr>
046     * <td> - </td>
047     * <td>
048     * -
049     * </td>
050     * </tr>
051     * </table>
052     *
053     * @author Matthew Baird (mattbaird@yahoo.com);
054     * @version $Id: ObjectCacheJCSImpl.java,v 1.1 2007-08-24 22:17:29 ewestfal Exp $
055     */
056    public class ObjectCacheJCSImpl implements ObjectCache
057    {
058        /**
059         * The used default region name.
060         */
061        public static final String DEFAULT_REGION = "ojbDefaultJCSRegion";
062    
063        private JCS jcsCache;
064        /**
065         * if no regionname is passed in, we use the default region.
066         */
067        private String regionName = DEFAULT_REGION;
068    
069        public ObjectCacheJCSImpl(PersistenceBroker broker, Properties prop)
070        {
071            this(null);
072        }
073    
074        /**
075         * Constructor used by the {@link ObjectCacheJCSPerClassImpl}
076         */
077        public ObjectCacheJCSImpl(String name)
078        {
079            regionName = (name != null ? name : DEFAULT_REGION);
080            try
081            {
082                jcsCache = JCS.getInstance(regionName);
083            }
084            catch(Exception e)
085            {
086                throw new RuntimeCacheException("Can't instantiate JCS ObjectCacheImplementation", e);
087            }
088        }
089    
090        public String getRegionName()
091        {
092            return regionName;
093        }
094    
095        /**
096         * makes object obj persistent to the Objectcache under the key oid.
097         */
098        public void cache(Identity oid, Object obj)
099        {
100            try
101            {
102                jcsCache.put(oid.toString(), obj);
103            }
104            catch (CacheException e)
105            {
106                throw new RuntimeCacheException(e);
107            }
108        }
109    
110        public boolean cacheIfNew(Identity oid, Object obj)
111        {
112            boolean result = false;
113            try
114            {
115                jcsCache.putSafe(oid.toString(), obj);
116                result = true;
117            }
118            catch(ObjectExistsException e)
119            {
120                // do nothing, object already in cache
121            }
122            catch(CacheException e)
123            {
124                throw new RuntimeCacheException(e);
125            }
126            return result;
127        }
128    
129        /**
130         * Lookup object with Identity oid in objectTable.
131         * returns null if no matching id is found
132         */
133        public Object lookup(Identity oid)
134        {
135            return jcsCache.get(oid.toString());
136        }
137    
138        /**
139         * removes an Object from the cache.
140         *
141         * @param oid the Identity of the object to be removed.
142         */
143        public void remove(Identity oid)
144        {
145            try
146            {
147                jcsCache.remove(oid.toString());
148            }
149            catch (CacheException e)
150            {
151                throw new RuntimeCacheException(e.getMessage());
152            }
153        }
154    
155        /**
156         * clear the ObjectCache.
157         */
158        public void clear()
159        {
160            if (jcsCache != null)
161            {
162                try
163                {
164                    jcsCache.remove();
165                }
166                catch (CacheException e)
167                {
168                    throw new RuntimeCacheException(e);
169                }
170            }
171        }
172    
173        public String toString()
174        {
175            ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE);
176            buf.append("JCS region name", regionName);
177            buf.append("JCS region", jcsCache);
178            return buf.toString();
179        }
180    }
181