KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > DbRemote > DbCacheChunk


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: DbCacheChunk.java,v 1.6 2003/03/12 12:30:23 per_nyfelt Exp $
8

9 package org.ozoneDB.core.DbRemote;
10
11 import java.io.*;
12 import java.lang.reflect.*;
13 import org.ozoneDB.DxLib.*;
14 import org.ozoneDB.*;
15 import org.ozoneDB.io.stream.ResolvingObjectInputStream;
16 import org.ozoneDB.core.*;
17 import org.ozoneDB.util.*;
18
19
20 /**
21  * Read/write a chunk of objects from/to the server. This is used by the
22  * {@link ClientCacheDatabase} to fetch/sync objects from/to the server.
23  *
24  *
25  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
26  * @version $Revision: 1.6 $Date: 2003/03/12 12:30:23 $
27  */

28 public final class DbCacheChunk extends DbCommand implements Externalizable {
29
30     private final static byte MODE_READ = 1;
31     private final static byte MODE_WRITE = 2;
32
33     private byte mode;
34
35     private int size;
36
37     private ObjectID rootID;
38
39     private byte[] chunk;
40
41
42     public DbCacheChunk() {
43     }
44
45
46     /**
47      * Fetch objects from the server.
48      */

49     public DbCacheChunk( ObjectID _rootID, int _size ) {
50         mode = MODE_READ;
51         rootID = _rootID;
52         size = _size;
53     }
54
55
56     /**
57      * Store objects in the server.
58      */

59     public DbCacheChunk( byte[] _chunk ) {
60         mode = MODE_WRITE;
61         chunk = _chunk;
62     }
63
64
65     public void perform( Transaction ta ) throws Exception JavaDoc {
66         env.logWriter.newEntry( this, "DbCacheChunk.perform(): mode:" + mode, LogWriter.DEBUG );
67
68         switch (mode) {
69         case MODE_READ:
70             readChunk( ta );
71             break;
72         case MODE_WRITE:
73             writeChunk( ta );
74             break;
75         default:
76             throw new RuntimeException JavaDoc( "Unknown mode." );
77         }
78     }
79
80
81     protected void readChunk( Transaction ta ) throws Exception JavaDoc {
82         env.logWriter.newEntry( this, " readChunk()", LogWriter.DEBUG3 );
83
84         DxBag bag = env.storeManager.clusterOfID( rootID );
85
86         result = new DxArrayBag();
87
88         DxIterator it = bag.iterator();
89         ObjectID id = null;
90         while ((id = (ObjectID)it.next()) != null) {
91             try {
92                 env.logWriter.newEntry( this, "readChunk(): " + id, LogWriter.DEBUG3 );
93                 ObjectContainer container = ta.acquireObject( id, Lock.LEVEL_READ );
94
95                 try {
96                 // since the container link in the target is transient we can
97
// copy the target by just serializing it
98
CacheObjectContainer cc = new CacheObjectContainer( container );
99
100                 ((DxArrayBag)result).add( cc );
101                 } finally {
102                     container.unpin();
103                 }
104             } catch (OzoneRemoteException e) {
105                 env.logWriter.newEntry( this, " readChunk(): " + e.toString(), LogWriter.DEBUG3 );
106                 // we don't handle ObjectNotFoundException and such things here
107
continue;
108             }
109         }
110     }
111
112
113     protected void writeChunk( Transaction ta ) throws Exception JavaDoc {
114         env.logWriter.newEntry( this, "writeChunk(): size:" + chunk.length, LogWriter.DEBUG3 );
115
116         // writeChunk can use byte[] chunks because proxies always assume
117
// that they are inside the server and so seting their link
118
// correctly, this differs from readChunk()
119

120         ByteArrayInputStream bin = new ByteArrayInputStream( chunk );
121         ObjectInputStream in = new ResolvingObjectInputStream( bin );
122
123         env.logWriter.newEntry( this, " available:" + bin.available(), LogWriter.DEBUG3 );
124         while (bin.available() > 0) {
125             CacheObjectContainer cacheContainer = (CacheObjectContainer)in.readObject();
126             env.logWriter.newEntry( this,
127                     " container: id:" + cacheContainer.id() + " state:" + cacheContainer.state(), LogWriter.DEBUG3 );
128
129             if (cacheContainer.state() == ObjectContainer.STATE_CREATED) {
130                 ObjectContainer container =
131                         ta.createObject( null, cacheContainer.access(), cacheContainer.name(), null, null,
132                         cacheContainer.id() );
133                 try {
134                     container.setTarget( cacheContainer.target() );
135                 } finally {
136                     container.unpin();
137                 }
138             } else {
139                 // this throws an exception if the container has been deleted already
140
ObjectContainer container = ta.acquireObject( cacheContainer.id(), Lock.LEVEL_WRITE );
141
142                 // check the optimistic lock
143
if (container.modTime() > cacheContainer.modTime()) {
144                     throw new TransactionException( "Object has been changed by another transaction.",
145                             TransactionException.OPTIMISTIC );
146                 }
147
148                 if (cacheContainer.state() == ObjectContainer.STATE_DELETED) {
149                     ta.deleteObject( container.id() );
150                 } else if (cacheContainer.state() == ObjectContainer.STATE_MODIFIED) {
151                     container.setTarget( cacheContainer.target() );
152
153                     // do nothing if the names are the same
154
String JavaDoc name = cacheContainer.name();
155                     // if (!(container.name() == null && name == null || container.name().equals (name)))
156
container.nameTarget( name );
157                 } else {
158                     throw new RuntimeException JavaDoc( "Wrong container state: " + container.state() );
159                 }
160             }
161         }
162     }
163
164
165     /**
166      * Recursivly search the specified object and put all found OzoneProxies
167      * in the specified deque.
168      */

169     protected void findProxies( Object JavaDoc obj, DxDeque deque ) {
170         String JavaDoc name = obj != null ? obj.getClass().getName() : "(null)";
171         System.out.println( "*** findProxies(): " + name );
172
173         if (obj == null) {
174         // do nothing
175
} else if (obj instanceof OzoneProxy) {
176             deque.pushTop( ((OzoneProxy)obj).remoteID() );
177         } else {
178             Class JavaDoc cl = obj.getClass();
179             int mdf = cl.getModifiers();
180             if (Modifier.isTransient( mdf ) || Modifier.isStatic( mdf ) || Modifier.isFinal( mdf )) {
181                 // do nothing
182
System.out.println( "*** transient/static/final" + name );
183             } else if (cl.isPrimitive()) {
184                 // do nothing
185
System.out.println( "*** primitive" + name );
186             } else if (cl.isArray()) {
187                 System.out.println( "*** array" + name );
188                 int len = Array.getLength( obj );
189                 for (int j = 0; j < len; j++) {
190                     Object JavaDoc member = Array.get( obj, j );
191                     findProxies( member, deque );
192                 }
193             } else {
194                 Field[] fields = cl.getFields();
195                 for (int i = 0; i < fields.length; i++) {
196                     try {
197                         System.out.println( "*** " + fields[i].toString() );
198                         Object JavaDoc member = fields[i].get( obj );
199                         findProxies( member, deque );
200                     } catch (Exception JavaDoc e) {
201                         // should never happen
202
System.out.println( "*** exception" + name );
203                     }
204                 }
205             }
206         }
207     }
208
209
210     public void writeExternal( ObjectOutput out ) throws IOException {
211         out.writeByte( mode );
212         switch (mode) {
213         case MODE_WRITE: {
214             out.writeObject( chunk );
215             break;
216             }
217         case MODE_READ: {
218             out.writeInt( size );
219             out.writeObject( rootID );
220             break;
221             }
222         default:
223             throw new RuntimeException JavaDoc( "Unknown mode." );
224         }
225     }
226
227
228     public synchronized void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException JavaDoc {
229         mode = in.readByte();
230         switch (mode) {
231         case MODE_WRITE: {
232             chunk = (byte[])in.readObject();
233             break;
234             }
235         case MODE_READ: {
236             size = in.readInt();
237             rootID = (ObjectID)in.readObject();
238             break;
239             }
240         default:
241             throw new RuntimeException JavaDoc( "Unknown mode." );
242         }
243     }
244
245 }
246
247
248 /**
249  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
250  * @version $Revision: 1.6 $Date: 2003/03/12 12:30:23 $
251  */

252 final class RObjectOutputStream extends ObjectOutputStream {
253
254     private DxDeque deque;
255
256
257     public RObjectOutputStream( OutputStream out, DxDeque _deque ) throws IOException{
258         super( out );
259         deque = _deque;
260
261         System.out.println( getClass().getClassLoader() );
262         enableReplaceObject( true );
263     }
264
265
266     protected Object JavaDoc replaceObject( Object JavaDoc obj ) throws IOException {
267         String JavaDoc name = obj != null ? obj.getClass().getName() : "(null)";
268         System.out.println( "*** replaceObject():" + name );
269
270         if (obj instanceof OzoneProxy) {
271             System.out.println( "*** proxy!" );
272             deque.pushTop( ((OzoneProxy)obj).remoteID() );
273         }
274
275         return obj;
276     }
277
278 }
279
Popular Tags