1 4 package com.tc.objectserver.persistence.sleepycat; 5 6 import com.sleepycat.je.DatabaseEntry; 7 import com.tc.io.serializer.TCObjectInputStream; 8 import com.tc.io.serializer.TCObjectOutputStream; 9 import com.tc.io.serializer.api.Serializer; 10 import com.tc.io.serializer.impl.StringUTFSerializer; 11 import com.tc.objectserver.core.api.ManagedObject; 12 import com.tc.objectserver.managedobject.ManagedObjectSerializer; 13 14 import java.io.ByteArrayInputStream ; 15 import java.io.ByteArrayOutputStream ; 16 import java.io.IOException ; 17 18 public class CustomSerializationAdapter implements SerializationAdapter { 19 20 private final ByteArrayOutputStream baout; 21 private final TCObjectOutputStream out; 22 private final ManagedObjectSerializer moSerializer; 23 private final StringUTFSerializer stringSerializer; 24 25 private final Object serializerLock = new Object (); 26 27 public CustomSerializationAdapter(ManagedObjectSerializer moSerializer, StringUTFSerializer stringSerializer) { 28 this.moSerializer = moSerializer; 29 this.stringSerializer = stringSerializer; 30 baout = new ByteArrayOutputStream (4096); 31 out = new TCObjectOutputStream(baout); 32 } 33 34 public void serializeManagedObject(DatabaseEntry entry, ManagedObject managedObject) throws IOException { 35 synchronized (serializerLock) { 36 serialize(entry, managedObject, moSerializer); 37 } 38 } 39 40 public synchronized void serializeString(DatabaseEntry entry, String string) throws IOException { 41 synchronized (serializerLock) { 42 serialize(entry, string, stringSerializer); 43 } 44 } 45 46 private void serialize(DatabaseEntry entry, Object o, Serializer serializer) throws IOException { 47 serializer.serializeTo(o, out); 48 out.flush(); 49 entry.setData(baout.toByteArray()); 50 baout.reset(); 51 } 52 53 public synchronized ManagedObject deserializeManagedObject(DatabaseEntry data) throws IOException , 54 ClassNotFoundException { 55 return (ManagedObject) deserialize(data, moSerializer); 56 } 57 58 public String deserializeString(DatabaseEntry data) throws IOException , ClassNotFoundException { 59 return (String ) deserialize(data, stringSerializer); 60 } 61 62 private Object deserialize(DatabaseEntry entry, Serializer serializer) throws IOException , ClassNotFoundException { 63 ByteArrayInputStream bain = new ByteArrayInputStream (entry.getData()); 64 TCObjectInputStream in = new TCObjectInputStream(bain); 65 return serializer.deserializeFrom(in); 66 } 67 } 68 | Popular Tags |