KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > P1Object


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;
22
23 import com.db4o.inside.*;
24 import com.db4o.inside.replication.*;
25
26 /**
27  * base class for all database aware objects
28  * @exclude
29  * @persistent
30  */

31 public class P1Object implements Db4oTypeImpl{
32     
33     private transient Transaction i_trans;
34     private transient YapObject i_yapObject;
35     
36     public P1Object(){
37     }
38     
39     P1Object(Transaction a_trans){
40         i_trans = a_trans;
41     }
42     
43     public void activate (Object JavaDoc a_obj, int a_depth){
44         if(i_trans == null){
45             return;
46         }
47         if(a_depth < 0){
48             stream().activate1(i_trans, a_obj);
49         }else{
50             stream().activate1(i_trans, a_obj, a_depth);
51         }
52     }
53     
54     public int activationDepth(){
55         return 1;
56     }
57     
58     public int adjustReadDepth(int a_depth) {
59         return a_depth;
60     }
61     
62     public boolean canBind() {
63         return false;
64     }
65     
66     public void checkActive(){
67         if(i_trans == null){
68             return;
69         }
70         if(i_yapObject == null){
71             i_yapObject = stream().getYapObject(this);
72             if(i_yapObject == null){
73                 stream().set(this);
74                 i_yapObject = stream().getYapObject(this);
75             }
76         }
77         if(validYapObject()){
78             i_yapObject.activate(i_trans, this, activationDepth(), false);
79         }
80     }
81
82     public Object JavaDoc createDefault(Transaction a_trans) {
83         throw Exceptions4.virtualException();
84     }
85     
86     void deactivate(){
87         if(validYapObject()){
88             i_yapObject.deactivate(i_trans, activationDepth());
89         }
90     }
91     
92     void delete(){
93         if(i_trans == null){
94             return;
95         }
96         if(i_yapObject == null){
97             i_yapObject = stream().getYapObject(this);
98         }
99         if(validYapObject()){
100             stream().delete2(i_trans,i_yapObject,this, 0, false);
101         }
102     }
103     
104     protected void delete(Object JavaDoc a_obj){
105         if(i_trans != null){
106             stream().delete(a_obj);
107         }
108     }
109     
110     protected long getIDOf(Object JavaDoc a_obj){
111         if(i_trans == null){
112             return 0;
113         }
114         return stream().getID(a_obj);
115     }
116     
117     protected Transaction getTrans(){
118         return i_trans;
119     }
120     
121     public boolean hasClassIndex() {
122         return false;
123     }
124     
125     public void preDeactivate(){
126         // virtual, do nothing
127
}
128     
129     protected Object JavaDoc replicate(Transaction fromTrans, Transaction toTrans) {
130         
131         YapStream fromStream = fromTrans.stream();
132         YapStream toStream = toTrans.stream();
133         
134         MigrationConnection mgc = fromStream.i_handlers.i_migration;
135         
136         synchronized(fromStream.lock()){
137             
138             int id = toStream.oldReplicationHandles(this);
139             
140             if(id == -1){
141                 // no action to be taken, already handled
142
return this;
143             }
144             
145             if(id > 0) {
146                 // replication has taken care, we need that object
147
return toStream.getByID(id);
148             }
149             
150             if(mgc != null){
151                 Object JavaDoc otherObj = mgc.identityFor(this);
152                 if(otherObj != null){
153                     return otherObj;
154                 }
155             }
156             
157             P1Object replica = (P1Object)createDefault(toTrans);
158             
159             if(mgc != null){
160                 mgc.mapReference(replica, i_yapObject);
161                 mgc.mapIdentity(this, replica);
162             }
163             
164             replica.store(0);
165             
166             return replica;
167         }
168     }
169     
170     public void replicateFrom(Object JavaDoc obj) {
171         // do nothing
172
}
173
174     public void setTrans(Transaction a_trans){
175         i_trans = a_trans;
176     }
177
178     public void setYapObject(YapObject a_yapObject) {
179         i_yapObject = a_yapObject;
180     }
181     
182     protected void store(Object JavaDoc a_obj){
183         if(i_trans != null){
184             stream().setInternal(i_trans, a_obj, true);
185         }
186     }
187     
188     public Object JavaDoc storedTo(Transaction a_trans){
189         i_trans = a_trans;
190         return this;
191     }
192     
193     Object JavaDoc streamLock(){
194         if(i_trans != null){
195             stream().checkClosed();
196             return stream().lock();
197         }
198         return this;
199     }
200     
201     public void store(int a_depth){
202         if(i_trans == null){
203             return;
204         }
205         if(i_yapObject == null){
206             i_yapObject = i_trans.stream().getYapObject(this);
207             if(i_yapObject == null){
208                 i_trans.stream().setInternal(i_trans, this, true);
209                 i_yapObject = i_trans.stream().getYapObject(this);
210                 return;
211             }
212         }
213         update(a_depth);
214     }
215     
216     void update(){
217         update(activationDepth());
218     }
219     
220     void update(int depth){
221         if(validYapObject()){
222             stream().beginTopLevelSet();
223             try{
224                 i_yapObject.writeUpdate(i_trans, depth);
225                 stream().checkStillToSet();
226             } finally{
227                 stream().endTopLevelSet(i_trans);
228             }
229         }
230     }
231     
232     void updateInternal(){
233         updateInternal(activationDepth());
234     }
235     
236     void updateInternal(int depth){
237         if(validYapObject()){
238             i_yapObject.writeUpdate(i_trans, depth);
239             stream().flagAsHandled(i_yapObject);
240             stream().checkStillToSet();
241         }
242     }
243     
244     private boolean validYapObject(){
245         return (i_trans != null) && (i_yapObject != null) && (i_yapObject.getID() > 0);
246     }
247     
248     private YapStream stream(){
249         return i_trans.stream();
250     }
251     
252 }
253
Popular Tags