01: package org.jgroups.util;
02:
03: import java.io.IOException;
04: import java.io.InputStream;
05: import java.io.ObjectInputStream;
06: import java.io.ObjectStreamClass;
07: import java.util.HashMap;
08:
09: /**
10: * ObjectInputStream which sets a contact classloader for reading bytes into objects. Copied from
11: * MarshalledValueInputStream of JBoss
12: * @author Bela Ban
13: * @version $Id: ContextObjectInputStream.java,v 1.7 2006/05/02 08:23:25 belaban Exp $
14: */
15: public class ContextObjectInputStream extends ObjectInputStream {
16:
17: /**
18: * A class wide cache of proxy classes populated by resolveProxyClass
19: */
20: private static final HashMap classCache = new HashMap();
21:
22: private static final HashMap primClasses = new HashMap(9, 1.0F);
23: static {
24: primClasses.put("boolean", boolean.class);
25: primClasses.put("byte", byte.class);
26: primClasses.put("char", char.class);
27: primClasses.put("short", short.class);
28: primClasses.put("int", int.class);
29: primClasses.put("long", long.class);
30: primClasses.put("float", float.class);
31: primClasses.put("double", double.class);
32: primClasses.put("void", void.class);
33: }
34:
35: /**
36: * Creates a new instance of MarshalledValueOutputStream
37: */
38: public ContextObjectInputStream(InputStream is) throws IOException {
39: super (is);
40: }
41:
42: protected Class resolveClass(ObjectStreamClass v)
43: throws IOException, ClassNotFoundException {
44: String className = v.getName();
45: Class resolvedClass = null;
46: // Check the class cache first if it exists
47: synchronized (classCache) {
48: resolvedClass = (Class) classCache.get(className);
49: }
50:
51: if (resolvedClass == null) {
52: try {
53: resolvedClass = Util.loadClass(className, this
54: .getClass());
55: } catch (ClassNotFoundException e) {
56:
57: }
58: if (resolvedClass == null) {
59: /* This is a backport von JDK 1.4's java.io.ObjectInputstream to support
60: * retrieval of primitive classes (e.g. Boolean.TYPE) in JDK 1.3.
61: * This is required for org.jgroups.blocks.MethodCall to support primitive
62: * Argument types in JDK1.3:
63: */
64: resolvedClass = (Class) primClasses.get(className);
65: if (resolvedClass == null) {
66:
67: /* Use the super.resolveClass() call which will resolve array
68: classes and primitives. We do not use this by default as this can
69: result in caching of stale values across redeployments.
70: */
71: resolvedClass = super.resolveClass(v);
72: }
73: }
74: synchronized (classCache) {
75: classCache.put(className, resolvedClass);
76: }
77: }
78: return resolvedClass;
79: }
80: }
|