001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.io.serializer;
006:
007: import com.tc.io.serializer.api.Serializer;
008: import com.tc.io.serializer.api.SerializerPolicy;
009: import com.tc.io.serializer.api.StringIndex;
010: import com.tc.io.serializer.impl.BooleanSerializer;
011: import com.tc.io.serializer.impl.ByteSerializer;
012: import com.tc.io.serializer.impl.CharacterSerializer;
013: import com.tc.io.serializer.impl.DoubleSerializer;
014: import com.tc.io.serializer.impl.FloatSerializer;
015: import com.tc.io.serializer.impl.IntegerSerializer;
016: import com.tc.io.serializer.impl.LongSerializer;
017: import com.tc.io.serializer.impl.ObjectIDSerializer;
018: import com.tc.io.serializer.impl.ObjectSerializer;
019: import com.tc.io.serializer.impl.ShortSerializer;
020: import com.tc.io.serializer.impl.StringSerializer;
021: import com.tc.io.serializer.impl.StringUTFSerializer;
022: import com.tc.object.ObjectID;
023:
024: import gnu.trove.TIntObjectHashMap;
025:
026: import java.io.DataOutput;
027: import java.io.IOException;
028: import java.io.ObjectInput;
029: import java.io.ObjectOutput;
030: import java.util.HashMap;
031: import java.util.Map;
032:
033: public final class DSOSerializerPolicy implements SerializerPolicy {
034:
035: private final Map class2SerializerDescriptor;
036: private final TIntObjectHashMap id2Serializer;
037: private final DSOSerializerPolicy.SDesc objectSerializer;
038:
039: public DSOSerializerPolicy(StringIndex stringIndex) {
040: this (stringIndex, false);
041: }
042:
043: public DSOSerializerPolicy() {
044: this (null, true);
045: }
046:
047: private DSOSerializerPolicy(StringIndex stringIndex,
048: boolean useUTFStrings) {
049: class2SerializerDescriptor = new HashMap();
050: id2Serializer = new TIntObjectHashMap();
051:
052: addSerializerMapping(Object.class, new ObjectSerializer());
053: addSerializerMapping(ObjectID.class, new ObjectIDSerializer());
054: addSerializerMapping(Boolean.class, new BooleanSerializer());
055: addSerializerMapping(Byte.class, new ByteSerializer());
056: addSerializerMapping(Character.class, new CharacterSerializer());
057: if (useUTFStrings) {
058: addSerializerMapping(String.class,
059: new StringUTFSerializer());
060: } else {
061: addSerializerMapping(String.class, new StringSerializer(
062: stringIndex));
063: }
064: addSerializerMapping(Double.class, new DoubleSerializer());
065: addSerializerMapping(Float.class, new FloatSerializer());
066: addSerializerMapping(Integer.class, new IntegerSerializer());
067: addSerializerMapping(Long.class, new LongSerializer());
068: addSerializerMapping(Short.class, new ShortSerializer());
069:
070: objectSerializer = (SDesc) class2SerializerDescriptor
071: .get(Object.class);
072: }
073:
074: private void addSerializerMapping(Class clazz, Serializer serializer) {
075: SDesc desc = new SDesc(clazz, serializer);
076: if (id2Serializer.containsKey(desc.getID()))
077: throw new AssertionError("Duplicate desc ids: "
078: + desc.getID());
079: id2Serializer.put(desc.getID(), desc.serializer);
080: class2SerializerDescriptor.put(desc.clazz, desc);
081: }
082:
083: public Serializer getSerializerFor(Object o, ObjectOutput out)
084: throws IOException {
085: DSOSerializerPolicy.SDesc desc = null;
086: if (o != null) {
087: desc = (DSOSerializerPolicy.SDesc) class2SerializerDescriptor
088: .get(o.getClass());
089: }
090: desc = (desc == null) ? objectSerializer : desc;
091: return extractSerializer(desc, out);
092: }
093:
094: private Serializer extractSerializer(
095: DSOSerializerPolicy.SDesc desc, DataOutput out)
096: throws IOException {
097: out.writeByte(desc.getID());
098: return desc.serializer;
099: }
100:
101: public Serializer getSerializerFor(ObjectInput in)
102: throws IOException {
103: Serializer rv = (Serializer) id2Serializer.get(in.readByte());
104: return rv == null ? objectSerializer.serializer : rv;
105: }
106:
107: static final class SDesc {
108: Class clazz;
109: Serializer serializer;
110:
111: SDesc(Class clazz, Serializer serializer) {
112: this .clazz = clazz;
113: this .serializer = serializer;
114: }
115:
116: int getID() {
117: return serializer.getSerializerID();
118: }
119: }
120: }
|