Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / test / java / compat / src / com / sleepycat / bind / serial / test / TestClassCatalog.java
1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002, 2012 Oracle and/or its affiliates.  All rights reserved.
5  *
6  */
7
8 package com.sleepycat.bind.serial.test;
9
10 import java.io.ObjectInputStream;
11 import java.io.ObjectOutputStream;
12 import java.io.ObjectStreamClass;
13 import java.util.HashMap;
14 import java.util.Map;
15
16 import com.sleepycat.bind.serial.ClassCatalog;
17 import com.sleepycat.bind.tuple.IntegerBinding;
18 import com.sleepycat.compat.DbCompat;
19 import com.sleepycat.db.DatabaseEntry;
20 import com.sleepycat.db.DatabaseException;
21 import com.sleepycat.util.FastInputStream;
22 import com.sleepycat.util.FastOutputStream;
23 import com.sleepycat.util.RuntimeExceptionWrapper;
24
25 /**
26  * @author Mark Hayes
27  */
28 public class TestClassCatalog implements ClassCatalog {
29
30     private final Map<Integer, ObjectStreamClass> idToDescMap =
31         new HashMap<Integer, ObjectStreamClass>();
32     private final Map<String, Integer> nameToIdMap =
33         new HashMap<String, Integer>();
34     private int nextId = 1;
35
36     public TestClassCatalog() {
37     }
38
39     public void close() {
40     }
41
42     public synchronized byte[] getClassID(ObjectStreamClass desc) {
43         String className = desc.getName();
44         Integer intId = nameToIdMap.get(className);
45         if (intId == null) {
46             intId = nextId;
47             nextId += 1;
48
49             idToDescMap.put(intId, desc);
50             nameToIdMap.put(className, intId);
51         }
52         DatabaseEntry entry = new DatabaseEntry();
53         IntegerBinding.intToEntry(intId, entry);
54         return entry.getData();
55     }
56
57     public synchronized ObjectStreamClass getClassFormat(byte[] byteId)
58         throws DatabaseException {
59
60         DatabaseEntry entry = new DatabaseEntry();
61         entry.setData(byteId);
62         int intId = IntegerBinding.entryToInt(entry);
63
64         ObjectStreamClass desc = (ObjectStreamClass) idToDescMap.get(intId);
65         if (desc == null) {
66             throw new RuntimeException("classID not found");
67         }
68         
69         /*
70          * Workaround for a Harmony bug that appears on Android.  The
71          * ObjectStreamClass is not properly initialized, and using it later
72          * will cause NullPointerException.  Serializing it and then
73          * deserializing it causes is to be initialized properly.  [#18163]
74          */
75         if (DbCompat.isDalvik()) {
76             try {
77                 /* Serialize desc first. */
78                 FastOutputStream fo = new FastOutputStream();
79                 ObjectOutputStream oos = new ObjectOutputStream(fo);
80                 oos.writeObject(desc);
81                 byte[] bytes = fo.toByteArray();
82                 /* Then deserialize classFormat. */
83                 FastInputStream fi = new FastInputStream(bytes);
84                 ObjectInputStream ois = new ObjectInputStream(fi);
85                 desc = (ObjectStreamClass) ois.readObject();
86             } catch (Exception e) {
87                 throw RuntimeExceptionWrapper.wrapIfNeeded(e);
88             }
89         }
90         return desc;
91     }
92
93     public ClassLoader getClassLoader() {
94         return null;
95     }
96 }