001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: CacheObjectContainer.java,v 1.2 2002/06/08 00:49:38 mediumnet Exp $
008:
009: package org.ozoneDB;
010:
011: import java.io.*;
012: import java.util.*;
013: import java.lang.reflect.*;
014: import org.ozoneDB.*;
015: import org.ozoneDB.DxLib.*;
016: import org.ozoneDB.core.*;
017: import org.ozoneDB.util.*;
018:
019: /**
020: * An implementation of {@link ObjectContainer} that works together with {@link
021: * ClientCacheDatabase} to provide an client site cache. A client program never
022: * needs to directly deal with it.
023: *
024: *
025: * @author <a href="http://www.softwarebuero.de/">SMB</a>
026: * @version $Revision: 1.2 $Date: 2002/06/08 00:49:38 $
027: */
028: public final class CacheObjectContainer implements ObjectContainer,
029: Serializable {
030:
031: private static long touchCount;
032:
033: private transient ClientCacheDatabase db;
034:
035: protected transient AbstractTransaction tx;
036:
037: private transient long lastTouched;
038:
039: /**
040: * True if the container has to be synched with the server.
041: */
042: private transient boolean dirty;
043:
044: private int state;
045:
046: /**
047: * The time when this container was fetched from the server. This is
048: * initialized and used by the server only.
049: */
050: private long modTime;
051:
052: private OzoneCompatible target;
053:
054: private ObjectID id;
055:
056: private String name;
057:
058: private int access;
059:
060: public CacheObjectContainer(ObjectContainer rhs) {
061: this (rhs.target(), rhs.id(), rhs.name(), OzoneInterface.Public);
062: modTime = rhs.modTime();
063: }
064:
065: public CacheObjectContainer(OzoneCompatible _target, ObjectID _id,
066: String _name, int _access) {
067: target = _target;
068: target.setContainer(this );
069: id = _id;
070: name = _name;
071: access = _access;
072: state = STATE_CLEAN;
073:
074: touch();
075: }
076:
077: public int access() {
078: return access;
079: }
080:
081: public int state() {
082: return state;
083: }
084:
085: public boolean dirty() {
086: return dirty;
087: }
088:
089: public void setDirty(boolean _dirty) {
090: dirty = _dirty;
091: }
092:
093: public void raiseState(int newState) {
094: int oldState = state;
095: state = newState > state ? newState : state;
096:
097: if (state >= STATE_MODIFIED) {
098: dirty = true;
099: }
100:
101: // don't write containers back to the server that has been created and
102: // deleted in one transaction
103: if (state == STATE_DELETED && oldState == STATE_CREATED) {
104: dirty = false;
105: }
106: }
107:
108: public void clearState() {
109: state = STATE_CLEAN;
110: dirty = false;
111: }
112:
113: public void touch() {
114: touchCount++;
115: }
116:
117: public long lastTouched() {
118: return lastTouched;
119: }
120:
121: public long modTime() {
122: return modTime;
123: }
124:
125: protected void setModTime(long _modTime) {
126: modTime = _modTime;
127: }
128:
129: public Lock lock() {
130: throw new RuntimeException("Method not implemented.");
131: }
132:
133: public DxCollection allLockers() {
134: throw new RuntimeException("Method not implemented.");
135: }
136:
137: public void setTarget(OzoneCompatible _target) {
138: if (target != null) {
139: target.setContainer(null);
140: }
141:
142: target = _target;
143: target.setContainer(this );
144: }
145:
146: public OzoneCompatible target() {
147: return target;
148: }
149:
150: public Class targetClass() {
151: return target.getClass();
152: }
153:
154: public void setDatabase(ClientCacheDatabase _db) {
155: db = _db;
156: }
157:
158: public OzoneInterface database() {
159: return db;
160: }
161:
162: public ObjectID id() {
163: return id;
164: }
165:
166: public Permissions permissions() {
167: throw new RuntimeException("Method not implemented.");
168: }
169:
170: public String name() {
171: return name;
172: }
173:
174: public void setName(String _name) {
175: name = _name;
176: }
177:
178: public OzoneProxy ozoneProxy() {
179: try {
180: String implName = targetClass().getName();
181: String proxyName;
182: if (implName.endsWith(ObjectContainer.IMPLNAME_POSTFIX)) {
183: proxyName = implName
184: .substring(0, implName.length() - 5);
185: } else {
186: proxyName = implName + PROXYNAME_POSTFIX;
187: }
188:
189: Class cl = Class.forName(proxyName);
190: Class[] argTypes = { ObjectID.class, OzoneInterface.class };
191: Constructor ctor = cl.getConstructor(argTypes);
192:
193: // System.out.println ("creating proxy: " + cl.getName());
194: Object[] args = { id(), database() };
195: return (OzoneProxy) ctor.newInstance(args);
196: } catch (Exception e) {
197: throw new RuntimeException(e.toString());
198: }
199: }
200:
201: public OzoneCompatible targetClone() throws Exception {
202: ByteArrayOutputStream bout = new ByteArrayOutputStream(2048);
203: ObjectOutputStream out = new ObjectOutputStream(bout);
204: out.writeObject(target());
205: out.close();
206: ObjectInputStream in = new ObjectInputStream(
207: new ByteArrayInputStream(bout.toByteArray()));
208: Object targetClone = (OzoneCompatible) in.readObject();
209: in.close();
210: return (OzoneCompatible) targetClone;
211: }
212:
213: public Object invokeTarget(Env env, String methodName, String sig,
214: Object[] args) throws Exception {
215: throw new RuntimeException("Method not implemented.");
216: }
217:
218: public Object invokeTarget(Env env, int methodIndex, Object[] args)
219: throws Exception {
220: throw new RuntimeException("Method not implemented.");
221: }
222:
223: public void createTarget(Env env, Class cl, String sig,
224: Object[] args) throws Exception {
225: throw new RuntimeException("Method not implemented.");
226: }
227:
228: public void deleteTarget() {
229: throw new RuntimeException("Method not implemented.");
230: }
231:
232: public void nameTarget(String _name) {
233: throw new RuntimeException("Method not implemented.");
234: }
235:
236: public void finalizeTarget() throws Exception {
237: throw new RuntimeException("Method not implemented.");
238: }
239:
240: /**
241: Pins this ObjectContainer.
242: Every caller of this method must pair this call with a call to {@link #unpin}.
243: An ObjectContainer remains in main memory at least as long as it is pinned.
244: */
245: public void pin() {
246: throw new RuntimeException("Method not implemented.");
247: }
248:
249: /**
250: Unpins this ObjectContainer.
251: This method must be called exactly once for every call to {@link #pin}.
252: */
253: public void unpin() {
254: throw new RuntimeException("Method not implemented.");
255: }
256:
257: /**
258: Returns wether this ObjectContainer is pinned.
259: */
260: public boolean isPinned() {
261: throw new RuntimeException("Method not implemented.");
262: }
263:
264: /**
265: Ensures that the garbageCollectionLevel is at least the given currentGarbageCollectionLevel.
266: The return value is meaningful if the supplied newGarbageCollectionLevel is the currentGarbageCollectionLevel
267:
268: @return
269: <=0 if this object still has to be processed.
270: This is the case if it belongs to the surelyReachable set but not to the processedReachable set
271: > otherwise
272:
273: <0 if this object has been updated
274: =0 if this object has not been updated, it is surelyReachable
275: >0 if this object has not been updated, it is processedReachable
276: */
277: public int ensureGarbageCollectionLevel(
278: int newGarbageCollectionLevel) {
279: throw new RuntimeException("Method not implemented.");
280: }
281:
282: /**
283: Returns the garbageCollectionLevel this ObjectContainer has reached due to (not) calling {@link #ensureGarbageCollectionLevel}.
284: */
285: public int getGarbageCollectionLevel() {
286: throw new RuntimeException("Method not implemented.");
287: }
288: }
|