001: /*
002:
003: Derby - Class org.apache.derbyTesting.unitTests.services.T_Cacheable
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.unitTests.services;
023:
024: import org.apache.derby.iapi.services.cache.*;
025:
026: import org.apache.derby.iapi.error.StandardException;
027:
028: /**
029:
030: */
031: public abstract class T_Cacheable implements Cacheable {
032:
033: protected boolean isDirty;
034:
035: protected Thread owner;
036:
037: public T_Cacheable() {
038: }
039:
040: /*
041: ** Cacheable methods
042: */
043:
044: public Cacheable setIdentity(Object key) throws StandardException {
045: // we expect a key of type Object[]
046: if (!(key instanceof T_Key)) {
047: throw T_CacheException.invalidKey();
048: }
049:
050: owner = null;
051:
052: return null; // must be overriden by super class
053: }
054:
055: public Cacheable createIdentity(Object key, Object createParameter)
056: throws StandardException {
057:
058: // we expect a key of type Object[]
059: if (!(key instanceof T_Key)) {
060: throw T_CacheException.invalidKey();
061: }
062:
063: owner = (Thread) createParameter;
064:
065: return null; // must be overriden by super class
066: }
067:
068: /**
069: Returns true of the object is dirty. Will only be called when the object is unkept.
070:
071: <BR> MT - thread safe
072:
073: */
074: public boolean isDirty() {
075: synchronized (this ) {
076: return isDirty;
077: }
078: }
079:
080: public void clean(boolean forRemove) throws StandardException {
081: synchronized (this ) {
082: isDirty = false;
083: }
084: }
085:
086: /*
087: ** Implementation specific methods
088: */
089:
090: protected Cacheable getCorrectObject(Object keyValue)
091: throws StandardException {
092:
093: Cacheable correctType;
094:
095: if (keyValue instanceof Integer) {
096:
097: correctType = new T_CachedInteger();
098: //} else if (keyValue instanceof String) {
099: //correctType = new T_CachedString();
100: } else {
101:
102: throw T_CacheException.invalidKey();
103: }
104:
105: return correctType;
106: }
107:
108: protected boolean dummySet(T_Key tkey) throws StandardException {
109:
110: // first wait
111: if (tkey.getWait() != 0) {
112: synchronized (this ) {
113:
114: try {
115: wait(tkey.getWait());
116: } catch (InterruptedException ie) {
117: // RESOLVE
118: }
119: }
120: }
121:
122: if (!tkey.canFind())
123: return false;
124:
125: if (tkey.raiseException())
126: throw T_CacheException.identityFail();
127:
128: return true;
129: }
130:
131: public void setDirty() {
132: synchronized (this ) {
133: isDirty = true;
134: }
135: }
136:
137: public boolean canRemove() {
138:
139: synchronized (this ) {
140: if (owner == null)
141: owner = Thread.currentThread();
142:
143: if (owner == Thread.currentThread())
144: return true;
145: return false;
146: }
147: }
148: }
|