01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.objectserver.persistence.sleepycat;
05:
06: import com.sleepycat.bind.EntryBinding;
07: import com.sleepycat.bind.serial.ClassCatalog;
08: import com.sleepycat.bind.serial.SerialBinding;
09: import com.sleepycat.je.DatabaseEntry;
10: import com.tc.objectserver.core.api.ManagedObject;
11:
12: import java.util.HashMap;
13: import java.util.Map;
14:
15: public class SleepycatSerializationAdapter implements
16: SerializationAdapter {
17:
18: private final ClassCatalog classCatalog;
19: private final Map entryBindings;
20:
21: public SleepycatSerializationAdapter(ClassCatalog classCatalog) {
22: this .classCatalog = classCatalog;
23: this .entryBindings = new HashMap();
24: }
25:
26: public void serializeManagedObject(DatabaseEntry entry,
27: ManagedObject mo) {
28: serialize(entry, mo, ManagedObject.class);
29: }
30:
31: public void serializeString(DatabaseEntry entry, String string) {
32: serialize(entry, string, String.class);
33: }
34:
35: public ManagedObject deserializeManagedObject(DatabaseEntry entry) {
36: return (ManagedObject) deserialize(entry, ManagedObject.class);
37: }
38:
39: public String deserializeString(DatabaseEntry entry) {
40: return (String) deserialize(entry, String.class);
41: }
42:
43: public void reset() {
44: return;
45: }
46:
47: private void serialize(DatabaseEntry entry, Object o, Class clazz) {
48: getEntryBindingFor(clazz).objectToEntry(o, entry);
49: }
50:
51: private Object deserialize(DatabaseEntry data, Class clazz) {
52: return getEntryBindingFor(clazz).entryToObject(data);
53: }
54:
55: private EntryBinding getEntryBindingFor(Class c) {
56: EntryBinding rv = (EntryBinding) this .entryBindings.get(c);
57: if (rv == null) {
58: rv = new SerialBinding(this.classCatalog, c);
59: this.entryBindings.put(c, rv);
60: }
61: return rv;
62: }
63:
64: }
|