KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > inside > marshall > ObjectMarshaller


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.inside.marshall;
22
23 import com.db4o.*;
24 import com.db4o.inside.slots.*;
25
26 public abstract class ObjectMarshaller {
27     
28     public MarshallerFamily _family;
29     
30     protected abstract static class TraverseFieldCommand {
31         private boolean _cancelled=false;
32         
33         public int fieldCount(YapClass yapClass,YapReader reader) {
34             return (Debug.atHome ? yapClass.readFieldCountSodaAtHome(reader) : yapClass.readFieldCount(reader));
35         }
36
37         public boolean cancelled() {
38             return _cancelled;
39         }
40         
41         protected void cancel() {
42             _cancelled=true;
43         }
44
45         public abstract void processField(YapField field,boolean isNull, YapClass containingClass);
46     }
47
48     protected void traverseFields(YapClass yc,YapReader reader,ObjectHeaderAttributes attributes,TraverseFieldCommand command) {
49         int fieldIndex=0;
50         while(yc!=null&&!command.cancelled()) {
51             int fieldCount=command.fieldCount(yc, reader);
52             for (int i = 0; i < fieldCount && !command.cancelled(); i++) {
53                 command.processField(yc.i_fields[i],isNull(attributes,fieldIndex),yc);
54                 fieldIndex ++;
55             }
56             yc=yc.i_ancestor;
57         }
58     }
59
60     protected abstract boolean isNull(ObjectHeaderAttributes attributes,int fieldIndex);
61
62     public abstract void addFieldIndices(
63             YapClass yc,
64             ObjectHeaderAttributes attributes,
65             YapWriter writer,
66             Slot oldSlot) ;
67     
68     public abstract TreeInt collectFieldIDs(
69         TreeInt tree,
70         YapClass yc,
71         ObjectHeaderAttributes attributes,
72         YapWriter reader, String JavaDoc name);
73     
74     protected YapWriter createWriterForNew(
75             Transaction trans,
76             YapObject yo,
77             int updateDepth,
78             int length) {
79         
80         int id = yo.getID();
81         int address = -1;
82         
83         if(! trans.stream().isClient()){
84             address = trans.i_file.getSlot(length);
85         }
86         trans.setPointer(id, address, length);
87         return createWriterForUpdate(trans, updateDepth, id, address, length);
88     }
89
90     protected YapWriter createWriterForUpdate(
91             Transaction a_trans,
92             int updateDepth,
93             int id,
94             int address,
95             int length) {
96         
97         YapWriter writer = new YapWriter(a_trans, length);
98         writer.useSlot(id, address, length);
99         if (Deploy.debug) {
100             writer.writeBegin(YapConst.YAPOBJECT);
101         }
102         writer.setUpdateDepth(updateDepth);
103         return writer;
104     }
105     
106     public abstract void deleteMembers(
107             YapClass yc,
108             ObjectHeaderAttributes attributes,
109             YapWriter writer,
110             int a_type,
111             boolean isUpdate);
112     
113     public abstract boolean findOffset(
114             YapClass yc,
115             ObjectHeaderAttributes attributes,
116             YapReader reader,
117             YapField field);
118     
119     public abstract void instantiateFields(
120             YapClass yc,
121             ObjectHeaderAttributes attributes,
122             YapObject yo,
123             Object JavaDoc obj,
124             YapWriter reader);
125     
126     public abstract YapWriter marshallNew(Transaction a_trans, YapObject yo, int a_updateDepth);
127     
128     public abstract void marshallUpdate(
129         Transaction a_trans,
130         int a_updateDepth,
131         YapObject a_yapObject,
132         Object JavaDoc a_object
133         );
134     
135     protected void marshallUpdateWrite(
136             Transaction trans,
137             YapObject yo,
138             Object JavaDoc obj,
139             YapWriter writer) {
140         
141         YapClass yc = yo.getYapClass();
142         
143         YapStream stream = trans.stream();
144         stream.writeUpdate(yc, writer);
145         if (yo.isActive()) {
146             yo.setStateClean();
147         }
148         yo.endProcessing();
149         objectOnUpdate(yc, stream, obj);
150     }
151
152     private void objectOnUpdate(YapClass yc, YapStream stream, Object JavaDoc obj) {
153         stream.callbacks().objectOnUpdate(obj);
154         yc.dispatchEvent(stream, obj, EventDispatcher.UPDATE);
155     }
156     
157     public abstract Object JavaDoc readIndexEntry(
158             YapClass yc,
159             ObjectHeaderAttributes attributes,
160             YapField yf,
161             YapWriter reader);
162
163     public abstract ObjectHeaderAttributes readHeaderAttributes(YapReader reader);
164     
165     public abstract void readVirtualAttributes(
166             Transaction trans,
167             YapClass yc,
168             YapObject yo,
169             ObjectHeaderAttributes attributes,
170             YapReader reader);
171
172     public abstract void defragFields(YapClass yapClass,ObjectHeader header, ReaderPair readers);
173  
174     public abstract void writeObjectClassID(YapReader reader,int id);
175     
176     public abstract void skipMarshallerInfo(YapReader reader);
177 }
Popular Tags