KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > ObjectInputStreamWithLoader


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.util;
24
25 import java.io.*;
26
27 import java.lang.reflect.Array JavaDoc;
28
29 /**
30   * This subclass of ObjectInputStream delegates loading of classes to
31   * an existing ClassLoader.
32   */

33 public class ObjectInputStreamWithLoader extends ObjectInputStream {
34     protected ClassLoader JavaDoc loader;
35
36     /**
37      * Loader must be non-null;
38      *
39      * @exception IOException on io error
40      * @exception StreamCorruptedException on a corrupted stream
41      */

42
43     public ObjectInputStreamWithLoader(InputStream in, ClassLoader JavaDoc loader)
44     throws IOException, StreamCorruptedException {
45
46         super(in);
47         if (loader == null) {
48         throw new IllegalArgumentException JavaDoc("Illegal null argument to ObjectInputStreamWithLoader");
49         }
50         this.loader = loader;
51     }
52
53     /**
54       * Make a primitive array class
55       */

56
57     private Class JavaDoc primitiveType(char type) {
58     switch (type) {
59     case 'B': return byte.class;
60     case 'C': return char.class;
61     case 'D': return double.class;
62     case 'F': return float.class;
63     case 'I': return int.class;
64     case 'J': return long.class;
65     case 'S': return short.class;
66     case 'Z': return boolean.class;
67     default: return null;
68     }
69     }
70
71     /**
72       * Use the given ClassLoader rather than using the system class
73       *
74       * @exception ClassNotFoundException if class can not be loaded
75       */

76     protected Class JavaDoc resolveClass(ObjectStreamClass classDesc)
77     throws IOException, ClassNotFoundException JavaDoc {
78         
79         String JavaDoc cname = classDesc.getName();
80         if (cname.startsWith("[")) {
81         // An array
82
Class JavaDoc component; // component class
83
int dcount; // dimension
84
for (dcount=1; cname.charAt(dcount)=='['; dcount++) ;
85         if (cname.charAt(dcount) == 'L') {
86             component = loader.loadClass(cname.substring(dcount+1,
87                                  cname.length()-1));
88         } else {
89             if (cname.length() != dcount+1) {
90             throw new ClassNotFoundException JavaDoc(cname);// malformed
91
}
92             component = primitiveType(cname.charAt(dcount));
93         }
94         int dim[] = new int[dcount];
95         for (int i=0; i<dcount; i++) {
96             dim[i]=0;
97         }
98         return Array.newInstance(component, dim).getClass();
99         } else {
100         return loader.loadClass(cname);
101         }
102     }
103 }
104
Popular Tags