KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > persistence > sleepycat > CustomSerializationAdapter


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
15 import java.io.ByteArrayOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17
18 public class CustomSerializationAdapter implements SerializationAdapter {
19
20   private final ByteArrayOutputStream JavaDoc baout;
21   private final TCObjectOutputStream out;
22   private final ManagedObjectSerializer moSerializer;
23   private final StringUTFSerializer stringSerializer;
24
25   private final Object JavaDoc serializerLock = new Object JavaDoc();
26
27   public CustomSerializationAdapter(ManagedObjectSerializer moSerializer, StringUTFSerializer stringSerializer) {
28     this.moSerializer = moSerializer;
29     this.stringSerializer = stringSerializer;
30     baout = new ByteArrayOutputStream JavaDoc(4096);
31     out = new TCObjectOutputStream(baout);
32   }
33
34   public void serializeManagedObject(DatabaseEntry entry, ManagedObject managedObject) throws IOException JavaDoc {
35     synchronized (serializerLock) {
36       serialize(entry, managedObject, moSerializer);
37     }
38   }
39
40   public synchronized void serializeString(DatabaseEntry entry, String JavaDoc string) throws IOException JavaDoc {
41     synchronized (serializerLock) {
42       serialize(entry, string, stringSerializer);
43     }
44   }
45
46   private void serialize(DatabaseEntry entry, Object JavaDoc o, Serializer serializer) throws IOException JavaDoc {
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 JavaDoc,
54       ClassNotFoundException JavaDoc {
55     return (ManagedObject) deserialize(data, moSerializer);
56   }
57
58   public String JavaDoc deserializeString(DatabaseEntry data) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
59     return (String JavaDoc) deserialize(data, stringSerializer);
60   }
61
62   private Object JavaDoc deserialize(DatabaseEntry entry, Serializer serializer) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
63     ByteArrayInputStream JavaDoc bain = new ByteArrayInputStream JavaDoc(entry.getData());
64     TCObjectInputStream in = new TCObjectInputStream(bain);
65     return serializer.deserializeFrom(in);
66   }
67 }
68
Popular Tags