KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > util > ContextObjectInputStream


1 package org.jgroups.util;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5
6 import java.io.IOException JavaDoc;
7 import java.io.ObjectStreamClass JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.io.ObjectInputStream JavaDoc;
10 import java.util.HashMap JavaDoc;
11
12 /**
13  * ObjectInputStream which sets a contact classloader for reading bytes into objects. Copied from
14  * MarshalledValueInputStream of JBoss
15  * @author Bela Ban
16  * @version $Id: ContextObjectInputStream.java,v 1.4 2004/09/24 09:00:30 belaban Exp $
17  */

18 public class ContextObjectInputStream extends ObjectInputStream JavaDoc {
19
20
21     /**
22      * A class wide cache of proxy classes populated by resolveProxyClass
23      */

24     private static final HashMap JavaDoc classCache=new HashMap JavaDoc();
25
26     private static final HashMap JavaDoc primClasses = new HashMap JavaDoc(8, 1.0F);
27     static {
28         primClasses.put("boolean", boolean.class);
29         primClasses.put("byte", byte.class);
30         primClasses.put("char", char.class);
31         primClasses.put("short", short.class);
32         primClasses.put("int", int.class);
33         primClasses.put("long", long.class);
34         primClasses.put("float", float.class);
35         primClasses.put("double", double.class);
36         primClasses.put("void", void.class);
37     }
38
39     /**
40      * Creates a new instance of MarshalledValueOutputStream
41      */

42     public ContextObjectInputStream(InputStream JavaDoc is) throws IOException JavaDoc {
43         super(is);
44     }
45
46
47     protected Class JavaDoc resolveClass(ObjectStreamClass JavaDoc v) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
48         String JavaDoc className=v.getName();
49         Class JavaDoc resolvedClass=null;
50         // Check the class cache first if it exists
51
if(classCache != null) {
52             synchronized(classCache) {
53                 resolvedClass=(Class JavaDoc)classCache.get(className);
54             }
55         }
56
57         if(resolvedClass == null) {
58             ClassLoader JavaDoc loader=Thread.currentThread().getContextClassLoader();
59             try {
60                 resolvedClass=loader.loadClass(className);
61             }
62             catch(ClassNotFoundException JavaDoc e) {
63                 /* This is a backport von JDK 1.4's java.io.ObjectInputstream to support
64                  * retrieval of primitive classes (e.g. Boolean.TYPE) in JDK 1.3.
65                  * This is required for org.jgroups.blocks.MethodCall to support primitive
66                  * Argument types in JDK1.3:
67                  */

68                 resolvedClass = (Class JavaDoc) primClasses.get(className);
69                 if (resolvedClass == null) {
70                     
71                     /* Use the super.resolveClass() call which will resolve array
72                        classes and primitives. We do not use this by default as this can
73                        result in caching of stale values across redeployments.
74                     */

75                     resolvedClass=super.resolveClass(v);
76                 }
77             }
78             if(classCache != null) {
79                 synchronized(classCache) {
80                     classCache.put(className, resolvedClass);
81                 }
82             }
83         }
84         return resolvedClass;
85     }
86 }
87
88
Popular Tags