KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > stream > CustomObjectInputStreamWithClassloader


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.util.stream;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectStreamClass JavaDoc;
28
29 import java.lang.reflect.Proxy JavaDoc;
30
31 /**
32  * Customized object input stream that
33  * <ul>
34  * <li> redefines <code>readClassDescriptor()</code> in order to read a short
35  * class descriptor (just the class name) when deserializing an
36  * object</li>
37  * <li> takes a class loader in its constructor and uses it to retrieve
38  * the class definitions.</li>
39  * </ul>
40  *
41  * @author <a HREF="mailto:rickard@dreambean.com">Rickard Oberg</a>
42  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
43  * @version $Revision: 37459 $
44  */

45 public class CustomObjectInputStreamWithClassloader
46       extends ObjectInputStream JavaDoc
47 {
48    
49    /**
50     * The classloader to use when the default classloader cannot find
51     * the classes in the stream.
52     */

53    ClassLoader JavaDoc cl;
54    
55    /**
56     * Constructs a new instance with the given classloader and input stream.
57     *
58     * @param in stream to read objects from
59     * @param cl classloader to use
60     */

61    public CustomObjectInputStreamWithClassloader(InputStream JavaDoc in,
62                                                  ClassLoader JavaDoc cl)
63       throws IOException JavaDoc
64    {
65       super(in);
66       this.cl = cl;
67    }
68    
69    /**
70     * Reads just the class name from this input stream.
71     *
72     * @return a class description object
73     */

74    protected ObjectStreamClass JavaDoc readClassDescriptor()
75       throws IOException JavaDoc, ClassNotFoundException JavaDoc
76    {
77       return ObjectStreamClass.lookup(cl.loadClass(readUTF()));
78    }
79    
80    /**
81     * Resolves the class described in the classdesc parameter. First, try the
82     * default classloader (implemented by the super class). If it cannot
83     * load the class, try the classloader given to this instance.
84     *
85     * @param classdesc class description object
86     * @return the Class corresponding to class description
87     * @exception IOException if an I/O error occurs
88     * @exception ClassNotFoundException if the class cannot be found
89     * by the classloader
90     */

91    protected Class JavaDoc resolveClass(ObjectStreamClass JavaDoc classdesc)
92       throws IOException JavaDoc, ClassNotFoundException JavaDoc
93    {
94       return cl.loadClass(classdesc.getName());
95    }
96    
97    /**
98     * Resolves the proxy class for the specified array of interfaces.
99     *
100     * @param interfaces an array of interfaces
101     * @return the proxy class
102     * @exception IOException if an I/O error occurs
103     * @exception ClassNotFoundException if the class cannot be found
104     * by the classloader
105     */

106    protected Class JavaDoc resolveProxyClass(String JavaDoc[] interfaces)
107       throws IOException JavaDoc, ClassNotFoundException JavaDoc
108    {
109       
110       Class JavaDoc[] interfacesClass = new Class JavaDoc[interfaces.length];
111       for( int i=0; i< interfaces.length; i++ ) {
112          interfacesClass[i] = Class.forName(interfaces[i], false, cl);
113       }
114       return Proxy.getProxyClass(cl, interfacesClass);
115    }
116 }
117
Popular Tags