001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o;
022:
023: import com.db4o.ext.*;
024: import com.db4o.internal.*;
025: import com.db4o.internal.replication.*;
026:
027: /**
028: * base class for all database aware objects
029: * @exclude
030: * @persistent
031: */
032: public class P1Object implements Db4oTypeImpl {
033:
034: private transient Transaction i_trans;
035: private transient ObjectReference i_yapObject;
036:
037: public P1Object() {
038: }
039:
040: P1Object(Transaction a_trans) {
041: i_trans = a_trans;
042: }
043:
044: public void activate(Object a_obj, int a_depth) {
045: if (i_trans == null) {
046: return;
047: }
048: if (a_depth < 0) {
049: stream().activateDefaultDepth(i_trans, a_obj);
050: } else {
051: stream().activate(i_trans, a_obj, a_depth);
052: }
053: }
054:
055: public int activationDepth() {
056: return 1;
057: }
058:
059: public int adjustReadDepth(int a_depth) {
060: return a_depth;
061: }
062:
063: public boolean canBind() {
064: return false;
065: }
066:
067: public void checkActive() {
068: if (i_trans == null) {
069: return;
070: }
071: if (i_yapObject == null) {
072:
073: i_yapObject = i_trans.referenceForObject(this );
074: if (i_yapObject == null) {
075: stream().set(i_trans, this );
076: i_yapObject = i_trans.referenceForObject(this );
077: }
078: }
079: if (validYapObject()) {
080: i_yapObject.activate(i_trans, this , activationDepth(),
081: false);
082: }
083: }
084:
085: public Object createDefault(Transaction a_trans) {
086: throw Exceptions4.virtualException();
087: }
088:
089: void deactivate() {
090: if (validYapObject()) {
091: i_yapObject.deactivate(i_trans, activationDepth());
092: }
093: }
094:
095: void delete() {
096: if (i_trans == null) {
097: return;
098: }
099: if (i_yapObject == null) {
100: i_yapObject = i_trans.referenceForObject(this );
101: }
102: if (validYapObject()) {
103: stream().delete2(i_trans, i_yapObject, this , 0, false);
104: }
105: }
106:
107: protected void delete(Object a_obj) {
108: if (i_trans != null) {
109: stream().delete(i_trans, a_obj);
110: }
111: }
112:
113: protected long getIDOf(Object a_obj) {
114: if (i_trans == null) {
115: return 0;
116: }
117: return stream().getID(i_trans, a_obj);
118: }
119:
120: protected Transaction getTrans() {
121: return i_trans;
122: }
123:
124: public boolean hasClassIndex() {
125: return false;
126: }
127:
128: public void preDeactivate() {
129: // virtual, do nothing
130: }
131:
132: /**
133: * @deprecated
134: */
135: protected Object replicate(Transaction fromTrans,
136: Transaction toTrans) {
137:
138: ObjectContainerBase fromStream = fromTrans.container();
139: ObjectContainerBase toStream = toTrans.container();
140:
141: MigrationConnection mgc = fromStream._handlers
142: .migrationConnection();
143:
144: synchronized (fromStream.lock()) {
145:
146: int id = toStream.oldReplicationHandles(toTrans, this );
147:
148: if (id == -1) {
149: // no action to be taken, already handled
150: return this ;
151: }
152:
153: if (id > 0) {
154: // replication has taken care, we need that object
155: return toStream.getByID(toTrans, id);
156: }
157:
158: if (mgc != null) {
159: Object otherObj = mgc.identityFor(this );
160: if (otherObj != null) {
161: return otherObj;
162: }
163: }
164:
165: P1Object replica = (P1Object) createDefault(toTrans);
166:
167: if (mgc != null) {
168: mgc.mapReference(replica, i_yapObject);
169: mgc.mapIdentity(this , replica);
170: }
171:
172: replica.store(0);
173:
174: return replica;
175: }
176: }
177:
178: public void replicateFrom(Object obj) {
179: // do nothing
180: }
181:
182: public void setTrans(Transaction a_trans) {
183: i_trans = a_trans;
184: }
185:
186: public void setObjectReference(ObjectReference a_yapObject) {
187: i_yapObject = a_yapObject;
188: }
189:
190: protected void store(Object a_obj) {
191: if (i_trans != null) {
192: stream().setInternal(i_trans, a_obj, true);
193: }
194: }
195:
196: public Object storedTo(Transaction a_trans) {
197: i_trans = a_trans;
198: return this ;
199: }
200:
201: Object streamLock() {
202: if (i_trans != null) {
203: stream().checkClosed();
204: return stream().lock();
205: }
206: return this ;
207: }
208:
209: public void store(int a_depth) {
210: if (i_trans == null) {
211: return;
212: }
213: if (i_yapObject == null) {
214: i_yapObject = i_trans.referenceForObject(this );
215: if (i_yapObject == null) {
216: i_trans.container().setInternal(i_trans, this , true);
217: i_yapObject = i_trans.referenceForObject(this );
218: return;
219: }
220: }
221: update(a_depth);
222: }
223:
224: void update() {
225: update(activationDepth());
226: }
227:
228: void update(int depth) {
229: if (validYapObject()) {
230: ObjectContainerBase stream = stream();
231: stream.beginTopLevelSet();
232: try {
233: i_yapObject.writeUpdate(i_trans, depth);
234: stream.checkStillToSet();
235: stream.completeTopLevelSet();
236: } catch (Db4oException e) {
237: stream.completeTopLevelSet(e);
238: } finally {
239: stream.endTopLevelSet(i_trans);
240: }
241: }
242: }
243:
244: void updateInternal() {
245: updateInternal(activationDepth());
246: }
247:
248: void updateInternal(int depth) {
249: if (validYapObject()) {
250: i_yapObject.writeUpdate(i_trans, depth);
251: stream().flagAsHandled(i_yapObject);
252: stream().checkStillToSet();
253: }
254: }
255:
256: private boolean validYapObject() {
257: return (i_trans != null) && (i_yapObject != null)
258: && (i_yapObject.getID() > 0);
259: }
260:
261: private ObjectContainerBase stream() {
262: return i_trans.container();
263: }
264:
265: }
|