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.internal.marshall;
022:
023: import com.db4o.*;
024: import com.db4o.internal.*;
025:
026: /**
027: * @exclude
028: */
029: public final class ObjectHeader {
030:
031: private final ClassMetadata _classMetadata;
032:
033: public final MarshallerFamily _marshallerFamily;
034:
035: public final ObjectHeaderAttributes _headerAttributes;
036:
037: private int _handlerVersion;
038:
039: public ObjectHeader(ObjectContainerBase stream, Buffer reader) {
040: this (stream, null, reader);
041: }
042:
043: public ObjectHeader(ClassMetadata yapClass, Buffer reader) {
044: this (null, yapClass, reader);
045: }
046:
047: public ObjectHeader(StatefulBuffer writer) {
048: this (writer.getStream(), writer);
049: }
050:
051: public ObjectHeader(ObjectContainerBase stream, ClassMetadata yc,
052: Buffer reader) {
053: if (Deploy.debug) {
054: reader.readBegin(Const4.YAPOBJECT);
055: }
056: int classID = reader.readInt();
057: _marshallerFamily = readMarshallerFamily(reader, classID);
058:
059: classID = normalizeID(classID);
060:
061: _classMetadata = (yc != null ? yc : stream
062: .classMetadataForId(classID));
063:
064: if (Deploy.debug) {
065: // This check has been added to cope with defragment in debug mode: SlotDefragment#setIdentity()
066: // will trigger calling this constructor with a source db yap class and a target db stream,
067: // thus _yapClass==null. There may be a better solution, since this call is just meant to
068: // skip the object header.
069: if (_classMetadata != null) {
070: int ycID = _classMetadata.getID();
071: if (classID != ycID) {
072: System.out
073: .println("ObjectHeader::init YapClass does not match. Expected ID: "
074: + ycID + " Read ID: " + classID);
075: }
076: }
077: }
078: _headerAttributes = readAttributes(_marshallerFamily, reader);
079: }
080:
081: public static ObjectHeader defrag(BufferPair readers) {
082: Buffer source = readers.source();
083: Buffer target = readers.target();
084: ObjectHeader header = new ObjectHeader(readers.context()
085: .systemTrans().container(), null, source);
086: int newID = readers.mapping().mappedID(
087: header.classMetadata().getID());
088: if (Deploy.debug) {
089: target.readBegin(Const4.YAPOBJECT);
090: }
091: header._marshallerFamily._object.writeObjectClassID(target,
092: newID);
093: header._marshallerFamily._object.skipMarshallerInfo(target);
094: readAttributes(header._marshallerFamily, target);
095: return header;
096: }
097:
098: public ObjectMarshaller objectMarshaller() {
099: return _marshallerFamily._object;
100: }
101:
102: private MarshallerFamily readMarshallerFamily(Buffer reader,
103: int classID) {
104: boolean marshallerAware = marshallerAware(classID);
105: _handlerVersion = 0;
106: if (marshallerAware) {
107: _handlerVersion = reader.readByte();
108: }
109: MarshallerFamily marshallerFamily = MarshallerFamily
110: .version(_handlerVersion);
111: return marshallerFamily;
112: }
113:
114: private static ObjectHeaderAttributes readAttributes(
115: MarshallerFamily marshallerFamily, Buffer reader) {
116: return marshallerFamily._object.readHeaderAttributes(reader);
117: }
118:
119: private boolean marshallerAware(int id) {
120: return id < 0;
121: }
122:
123: private int normalizeID(int id) {
124: return (id < 0 ? -id : id);
125: }
126:
127: public ClassMetadata classMetadata() {
128: return _classMetadata;
129: }
130:
131: public int handlerVersion() {
132: return _handlerVersion;
133: }
134:
135: }
|