KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > remoting > ObjectInputStreamEx


1 package hudson.remoting;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.io.ObjectInputStream JavaDoc;
6 import java.io.ObjectStreamClass JavaDoc;
7 import java.lang.reflect.Modifier JavaDoc;
8 import java.lang.reflect.Proxy JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.HashMap JavaDoc;
11
12 /**
13  * {@link ObjectInputStream} that uses a specific class loader.
14  */

15 final class ObjectInputStreamEx extends ObjectInputStream JavaDoc {
16     private final ClassLoader JavaDoc cl;
17
18     public ObjectInputStreamEx(InputStream JavaDoc in, ClassLoader JavaDoc cl) throws IOException JavaDoc {
19         super(in);
20         this.cl = cl;
21     }
22
23     @Override JavaDoc
24     protected Class JavaDoc<?> resolveClass(ObjectStreamClass JavaDoc desc) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
25         String JavaDoc name = desc.getName();
26         try {
27             return Class.forName(name, false, cl);
28         } catch (ClassNotFoundException JavaDoc ex) {
29             return super.resolveClass(desc);
30         }
31     }
32
33     @Override JavaDoc
34     protected Class JavaDoc<?> resolveProxyClass(String JavaDoc[] interfaces) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
35         ClassLoader JavaDoc latestLoader = cl;
36         ClassLoader JavaDoc nonPublicLoader = null;
37         boolean hasNonPublicInterface = false;
38
39         // define proxy in class loader of non-public interface(s), if any
40
Class JavaDoc[] classObjs = new Class JavaDoc[interfaces.length];
41         for (int i = 0; i < interfaces.length; i++) {
42             Class JavaDoc cl = Class.forName(interfaces[i], false, latestLoader);
43             if ((cl.getModifiers() & Modifier.PUBLIC) == 0) {
44                 if (hasNonPublicInterface) {
45                     if (nonPublicLoader != cl.getClassLoader()) {
46                         throw new IllegalAccessError JavaDoc(
47                             "conflicting non-public interface class loaders");
48                     }
49                 } else {
50                     nonPublicLoader = cl.getClassLoader();
51                     hasNonPublicInterface = true;
52                 }
53             }
54             classObjs[i] = cl;
55         }
56         try {
57             return Proxy.getProxyClass(
58                 hasNonPublicInterface ? nonPublicLoader : latestLoader,
59                 classObjs);
60         } catch (IllegalArgumentException JavaDoc e) {
61             throw new ClassNotFoundException JavaDoc(null, e);
62         }
63     }
64 }
65
Popular Tags