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.11.2.2 2005/12/21 22:24:15 tomdz Exp $
055: */
056: public class ObjectCacheJCSImpl implements ObjectCache {
057: /**
058: * The used default region name.
059: */
060: public static final String DEFAULT_REGION = "ojbDefaultJCSRegion";
061:
062: private JCS jcsCache;
063: /**
064: * if no regionname is passed in, we use the default region.
065: */
066: private String regionName = DEFAULT_REGION;
067:
068: public ObjectCacheJCSImpl(PersistenceBroker broker, Properties prop) {
069: this (null);
070: }
071:
072: /**
073: * Constructor used by the {@link ObjectCacheJCSPerClassImpl}
074: */
075: public ObjectCacheJCSImpl(String name) {
076: regionName = (name != null ? name : DEFAULT_REGION);
077: try {
078: jcsCache = JCS.getInstance(regionName);
079: } catch (Exception e) {
080: throw new RuntimeCacheException(
081: "Can't instantiate JCS ObjectCacheImplementation",
082: e);
083: }
084: }
085:
086: public String getRegionName() {
087: return regionName;
088: }
089:
090: /**
091: * makes object obj persistent to the Objectcache under the key oid.
092: */
093: public void cache(Identity oid, Object obj) {
094: try {
095: jcsCache.put(oid.toString(), obj);
096: } catch (CacheException e) {
097: throw new RuntimeCacheException(e);
098: }
099: }
100:
101: public boolean cacheIfNew(Identity oid, Object obj) {
102: boolean result = false;
103: try {
104: jcsCache.putSafe(oid.toString(), obj);
105: result = true;
106: } catch (ObjectExistsException e) {
107: // do nothing, object already in cache
108: } catch (CacheException e) {
109: throw new RuntimeCacheException(e);
110: }
111: return result;
112: }
113:
114: /**
115: * Lookup object with Identity oid in objectTable.
116: * returns null if no matching id is found
117: */
118: public Object lookup(Identity oid) {
119: return jcsCache.get(oid.toString());
120: }
121:
122: /**
123: * removes an Object from the cache.
124: *
125: * @param oid the Identity of the object to be removed.
126: */
127: public void remove(Identity oid) {
128: try {
129: jcsCache.remove(oid.toString());
130: } catch (CacheException e) {
131: throw new RuntimeCacheException(e.getMessage());
132: }
133: }
134:
135: /**
136: * clear the ObjectCache.
137: */
138: public void clear() {
139: if (jcsCache != null) {
140: try {
141: jcsCache.remove();
142: } catch (CacheException e) {
143: throw new RuntimeCacheException(e);
144: }
145: }
146: }
147:
148: public String toString() {
149: ToStringBuilder buf = new ToStringBuilder(this ,
150: ToStringStyle.DEFAULT_STYLE);
151: buf.append("JCS region name", regionName);
152: buf.append("JCS region", jcsCache);
153: return buf.toString();
154: }
155: }
|