KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > server > ObjectInputStreamWithClassLoader


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.mx.server;
23
24
25 import java.io.InputStream JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectStreamClass JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 import java.lang.reflect.Proxy JavaDoc;
31
32 /**
33  * This replaces the EjbossInputStream in the storage package.
34  * The input stream will take a class loader in its constructor and look
35  * into it to retrieve the class definitions.
36  * It is used throughout the server to deserialize parameters and objects
37  * whose definition are in a jar and not the global classpath
38  * It also has better comments than the previous version.
39  *
40  * @author <a HREF="rickard@dreambean.com">Rickard Oberg</a>
41  * @since Ejboss 0.9
42  */

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

52     ClassLoader JavaDoc cl;
53
54     
55 /******************************************************************************/
56 /******************************************************************************/
57 /*
58 /* CONSTRUCTORS
59 /*
60 /******************************************************************************/

61 /******************************************************************************/
62
63     /**
64     * Construct a new instance with the given classloader and input stream.
65     *
66     * @param ClassLoader classloader to use
67     * @param InputStream stream to read objects from
68     */

69     public ObjectInputStreamWithClassLoader(InputStream JavaDoc in, ClassLoader JavaDoc cl)
70          throws IOException JavaDoc {
71
72          super(in);
73
74          this.cl = cl;
75     }
76
77
78 /******************************************************************************/
79 /******************************************************************************/
80 /*
81 /* OVERWRITING <ObjectInputStream>
82 /*
83 /******************************************************************************/

84 /******************************************************************************/
85
86     /**
87     * Resolve the class described in the osc parameter. First, try the
88     * default classloader (implemented by the super class). If it cannot
89     * load the class, try the classloader given to this instance.
90     *
91     * @param ObjectStreamClass class description object
92     * @return the Class corresponding to class description
93     * @exception IOException if an I/O error occurs
94     * @exception ClassNotFoundException if the class cannot be found by the classloader
95     */

96     protected Class JavaDoc resolveClass(ObjectStreamClass JavaDoc osc)
97         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
98
99         return cl.loadClass(osc.getName());
100     }
101     
102     protected Class JavaDoc resolveProxyClass( String JavaDoc[] interfaces )
103         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
104         
105         Class JavaDoc[] interfacesClass = new Class JavaDoc[interfaces.length];
106         for( int i=0; i< interfaces.length; i++ )
107         {
108             interfacesClass[i] = Class.forName(interfaces[i], false, cl);
109         }
110         
111         return Proxy.getProxyClass(cl, interfacesClass);
112     }
113 }
114
115
116
117
118
Popular Tags