01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.catalina.util;
19:
20: import java.io.InputStream;
21: import java.io.IOException;
22: import java.io.ObjectInputStream;
23: import java.io.ObjectStreamClass;
24: import java.lang.reflect.Proxy;
25:
26: /**
27: * Custom subclass of <code>ObjectInputStream</code> that loads from the
28: * class loader for this web application. This allows classes defined only
29: * with the web application to be found correctly.
30: *
31: * @author Craig R. McClanahan
32: * @author Bip Thelin
33: * @version $Revision: 467222 $, $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
34: */
35:
36: public final class CustomObjectInputStream extends ObjectInputStream {
37:
38: /**
39: * The class loader we will use to resolve classes.
40: */
41: private ClassLoader classLoader = null;
42:
43: /**
44: * Construct a new instance of CustomObjectInputStream
45: *
46: * @param stream The input stream we will read from
47: * @param classLoader The class loader used to instantiate objects
48: *
49: * @exception IOException if an input/output error occurs
50: */
51: public CustomObjectInputStream(InputStream stream,
52: ClassLoader classLoader) throws IOException {
53:
54: super (stream);
55: this .classLoader = classLoader;
56: }
57:
58: /**
59: * Load the local class equivalent of the specified stream class
60: * description, by using the class loader assigned to this Context.
61: *
62: * @param classDesc Class description from the input stream
63: *
64: * @exception ClassNotFoundException if this class cannot be found
65: * @exception IOException if an input/output error occurs
66: */
67: public Class resolveClass(ObjectStreamClass classDesc)
68: throws ClassNotFoundException, IOException {
69: try {
70: return Class.forName(classDesc.getName(), false,
71: classLoader);
72: } catch (ClassNotFoundException e) {
73: // Try also the superclass because of primitive types
74: return super .resolveClass(classDesc);
75: }
76: }
77:
78: /**
79: * Return a proxy class that implements the interfaces named in a proxy
80: * class descriptor. Do this using the class loader assigned to this
81: * Context.
82: */
83: protected Class resolveProxyClass(String[] interfaces)
84: throws IOException, ClassNotFoundException {
85:
86: Class[] cinterfaces = new Class[interfaces.length];
87: for (int i = 0; i < interfaces.length; i++)
88: cinterfaces[i] = classLoader.loadClass(interfaces[i]);
89:
90: try {
91: return Proxy.getProxyClass(classLoader, cinterfaces);
92: } catch (IllegalArgumentException e) {
93: throw new ClassNotFoundException(null, e);
94: }
95: }
96:
97: }
|