Coverage Report - org.apache.ojb.broker.cache.ObjectCacheJCSImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectCacheJCSImpl
N/A
N/A
2.444
 
 1  
 package org.apache.ojb.broker.cache;
 2  
 
 3  
 /* Copyright 2004-2005 The Apache Software Foundation
 4  
  *
 5  
  * Licensed under the Apache License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 import java.util.Properties;
 19  
 
 20  
 import org.apache.commons.lang.builder.ToStringBuilder;
 21  
 import org.apache.commons.lang.builder.ToStringStyle;
 22  
 import org.apache.jcs.JCS;
 23  
 import org.apache.jcs.access.exception.CacheException;
 24  
 import org.apache.jcs.access.exception.ObjectExistsException;
 25  
 import org.apache.ojb.broker.Identity;
 26  
 import org.apache.ojb.broker.PersistenceBroker;
 27  
 
 28  
 /**
 29  
  * This local {@link ObjectCache} implementation using
 30  
  * <a href="http://jakarta.apache.org/turbine/jcs/index.html">
 31  
  * turbine-JCS</a> to cache objects is primarily for intern use in
 32  
  * conjunction with {@link ObjectCacheJCSPerClassImpl} implementation. If
 33  
  * used as main <code>ObjectCache</code> all cached objects will be cached
 34  
  * under the same JCS region name (see {@link #DEFAULT_REGION}).
 35  
  * <p/>
 36  
  * <p/>
 37  
  * Implementation configuration properties:
 38  
  * </p>
 39  
  * <p/>
 40  
  * <table cellspacing="2" cellpadding="2" border="3" frame="box">
 41  
  * <tr>
 42  
  * <td><strong>Property Key</strong></td>
 43  
  * <td><strong>Property Values</strong></td>
 44  
  * </tr>
 45  
  * <tr>
 46  
  * <td> - </td>
 47  
  * <td>
 48  
  * -
 49  
  * </td>
 50  
  * </tr>
 51  
  * </table>
 52  
  *
 53  
  * @author Matthew Baird (mattbaird@yahoo.com);
 54  
  * @version $Id: ObjectCacheJCSImpl.java,v 1.1 2007-08-24 22:17:29 ewestfal Exp $
 55  
  */
 56  
 public class ObjectCacheJCSImpl implements ObjectCache
 57  
 {
 58  
     /**
 59  
      * The used default region name.
 60  
      */
 61  
     public static final String DEFAULT_REGION = "ojbDefaultJCSRegion";
 62  
 
 63  
     private JCS jcsCache;
 64  
     /**
 65  
      * if no regionname is passed in, we use the default region.
 66  
      */
 67  
     private String regionName = DEFAULT_REGION;
 68  
 
 69  
     public ObjectCacheJCSImpl(PersistenceBroker broker, Properties prop)
 70  
     {
 71  
         this(null);
 72  
     }
 73  
 
 74  
     /**
 75  
      * Constructor used by the {@link ObjectCacheJCSPerClassImpl}
 76  
      */
 77  
     public ObjectCacheJCSImpl(String name)
 78  
     {
 79  
         regionName = (name != null ? name : DEFAULT_REGION);
 80  
         try
 81  
         {
 82  
             jcsCache = JCS.getInstance(regionName);
 83  
         }
 84  
         catch(Exception e)
 85  
         {
 86  
             throw new RuntimeCacheException("Can't instantiate JCS ObjectCacheImplementation", e);
 87  
         }
 88  
     }
 89  
 
 90  
     public String getRegionName()
 91  
     {
 92  
         return regionName;
 93  
     }
 94  
 
 95  
     /**
 96  
      * makes object obj persistent to the Objectcache under the key oid.
 97  
      */
 98  
     public void cache(Identity oid, Object obj)
 99  
     {
 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