KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > bind > serial > test > TestClassCatalog


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: TestClassCatalog.java,v 1.16 2006/10/30 21:14:36 bostic Exp $
7  */

8
9 package com.sleepycat.bind.serial.test;
10
11 import java.io.ObjectStreamClass JavaDoc;
12 import java.util.HashMap JavaDoc;
13
14 import com.sleepycat.bind.serial.ClassCatalog;
15 import com.sleepycat.je.DatabaseException;
16
17 /**
18  * @author Mark Hayes
19  */

20 public class TestClassCatalog implements ClassCatalog {
21
22     private HashMap JavaDoc idToDescMap = new HashMap JavaDoc();
23     private HashMap JavaDoc nameToIdMap = new HashMap JavaDoc();
24     private int nextId = 1;
25
26     public TestClassCatalog() {
27     }
28
29     public void close()
30         throws DatabaseException {
31     }
32
33     public synchronized byte[] getClassID(ObjectStreamClass JavaDoc desc)
34         throws DatabaseException {
35
36         String JavaDoc className = desc.getName();
37         byte[] id = (byte[]) nameToIdMap.get(className);
38         if (id == null) {
39             String JavaDoc strId = String.valueOf(nextId);
40             id = strId.getBytes();
41             nextId += 1;
42
43             idToDescMap.put(strId, desc);
44             nameToIdMap.put(className, id);
45         }
46         return id;
47     }
48
49     public synchronized ObjectStreamClass JavaDoc getClassFormat(byte[] id)
50         throws DatabaseException {
51
52         String JavaDoc strId = new String JavaDoc(id);
53         ObjectStreamClass JavaDoc desc = (ObjectStreamClass JavaDoc) idToDescMap.get(strId);
54         if (desc == null) {
55             throw new DatabaseException("classID not found");
56         }
57         return desc;
58     }
59 }
60
Popular Tags