01: /*
02: * Copyright 1999,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.catalina.cluster.session;
18:
19: import java.io.InputStream;
20: import java.io.IOException;
21: import java.io.ObjectInputStream;
22: import java.io.ObjectStreamClass;
23:
24: /**
25: * Custom subclass of <code>ObjectInputStream</code> that loads from the
26: * class loader for this web application. This allows classes defined only
27: * with the web application to be found correctly.
28: *
29: * @@author Craig R. McClanahan
30: * @@author Bip Thelin
31: * @@version $Revision: 1.3 $, $Date: 2004/02/27 14:58:56 $
32: */
33:
34: public final class ReplicationStream extends ObjectInputStream {
35:
36: /**
37: * The class loader we will use to resolve classes.
38: */
39: private ClassLoader classLoader = null;
40:
41: /**
42: * Construct a new instance of CustomObjectInputStream
43: *
44: * @@param stream The input stream we will read from
45: * @@param classLoader The class loader used to instantiate objects
46: *
47: * @@exception IOException if an input/output error occurs
48: */
49: public ReplicationStream(InputStream stream, ClassLoader classLoader)
50: throws IOException {
51:
52: super (stream);
53: this .classLoader = classLoader;
54: }
55:
56: /**
57: * Load the local class equivalent of the specified stream class
58: * description, by using the class loader assigned to this Context.
59: *
60: * @@param classDesc Class description from the input stream
61: *
62: * @@exception ClassNotFoundException if this class cannot be found
63: * @@exception IOException if an input/output error occurs
64: */
65: public Class resolveClass(ObjectStreamClass classDesc)
66: throws ClassNotFoundException, IOException {
67: String name = classDesc.getName();
68: boolean tryRepFirst = name
69: .startsWith("org.apache.catalina.cluster");
70: try {
71: if (tryRepFirst)
72: return findReplicationClass(name);
73: else
74: return findWebappClass(name);
75: } catch (Exception x) {
76: if (tryRepFirst)
77: return findWebappClass(name);
78: else
79: return findReplicationClass(name);
80: }
81: }
82:
83: public Class findReplicationClass(String name)
84: throws ClassNotFoundException, IOException {
85: return Class.forName(name, false, getClass().getClassLoader());
86: }
87:
88: public Class findWebappClass(String name)
89: throws ClassNotFoundException, IOException {
90: return Class.forName(name, false, classLoader);
91: }
92:
93: }
|