KickJava   Java API By Example, From Geeks To Geeks.

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


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.bind.EntryBinding;
7 import com.sleepycat.bind.serial.ClassCatalog;
8 import com.sleepycat.bind.serial.SerialBinding;
9 import com.sleepycat.je.DatabaseEntry;
10 import com.tc.objectserver.core.api.ManagedObject;
11
12 import java.util.HashMap JavaDoc;
13 import java.util.Map JavaDoc;
14
15 public class SleepycatSerializationAdapter implements SerializationAdapter {
16
17   private final ClassCatalog classCatalog;
18   private final Map JavaDoc entryBindings;
19
20   public SleepycatSerializationAdapter(ClassCatalog classCatalog) {
21     this.classCatalog = classCatalog;
22     this.entryBindings = new HashMap JavaDoc();
23   }
24   
25   public void serializeManagedObject(DatabaseEntry entry, ManagedObject mo) {
26     serialize(entry, mo, ManagedObject.class);
27   }
28   
29   public void serializeString(DatabaseEntry entry, String JavaDoc string) {
30     serialize(entry, string, String JavaDoc.class);
31   }
32   
33   public ManagedObject deserializeManagedObject(DatabaseEntry entry) {
34     return (ManagedObject) deserialize(entry, ManagedObject.class);
35   }
36   
37   public String JavaDoc deserializeString(DatabaseEntry entry) {
38     return (String JavaDoc)deserialize(entry, String JavaDoc.class);
39   }
40   
41   public void reset() {
42     return;
43   }
44   
45   private void serialize(DatabaseEntry entry, Object JavaDoc o, Class JavaDoc clazz) {
46     getEntryBindingFor(clazz).objectToEntry(o, entry);
47   }
48
49   private Object JavaDoc deserialize(DatabaseEntry data, Class JavaDoc clazz) {
50     return getEntryBindingFor(clazz).entryToObject(data);
51   }
52
53   private EntryBinding getEntryBindingFor(Class JavaDoc c) {
54     EntryBinding rv = (EntryBinding) this.entryBindings.get(c);
55     if (rv == null) {
56       rv = new SerialBinding(this.classCatalog, c);
57       this.entryBindings.put(c, rv);
58     }
59     return rv;
60   }
61
62 }
63
Popular Tags