KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > cluster > session > ReplicationStream


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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
18 package org.apache.catalina.cluster.session;
19
20 import java.io.InputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.ObjectInputStream JavaDoc;
23 import java.io.ObjectStreamClass JavaDoc;
24
25 /**
26  * Custom subclass of <code>ObjectInputStream</code> that loads from the
27  * class loader for this web application. This allows classes defined only
28  * with the web application to be found correctly.
29  *
30  * @@author Craig R. McClanahan
31  * @@author Bip Thelin
32  * @@version $Revision: 1.3 $, $Date: 2004/02/27 14:58:56 $
33  */

34
35 public final class ReplicationStream
36     extends ObjectInputStream JavaDoc {
37
38
39     /**
40      * The class loader we will use to resolve classes.
41      */

42     private ClassLoader JavaDoc classLoader = null;
43
44     /**
45      * Construct a new instance of CustomObjectInputStream
46      *
47      * @@param stream The input stream we will read from
48      * @@param classLoader The class loader used to instantiate objects
49      *
50      * @@exception IOException if an input/output error occurs
51      */

52     public ReplicationStream(InputStream JavaDoc stream,
53                              ClassLoader JavaDoc classLoader)
54         throws IOException JavaDoc {
55
56         super(stream);
57         this.classLoader = classLoader;
58     }
59
60     /**
61      * Load the local class equivalent of the specified stream class
62      * description, by using the class loader assigned to this Context.
63      *
64      * @@param classDesc Class description from the input stream
65      *
66      * @@exception ClassNotFoundException if this class cannot be found
67      * @@exception IOException if an input/output error occurs
68      */

69     public Class JavaDoc resolveClass(ObjectStreamClass JavaDoc classDesc)
70         throws ClassNotFoundException JavaDoc, IOException JavaDoc {
71         String JavaDoc name = classDesc.getName();
72         boolean tryRepFirst = name.startsWith("org.apache.catalina.cluster");
73         try
74         {
75             if ( tryRepFirst ) return findReplicationClass(name);
76             else return findWebappClass(name);
77         }
78         catch ( Exception JavaDoc x )
79         {
80             if ( tryRepFirst ) return findWebappClass(name);
81             else return findReplicationClass(name);
82         }
83     }
84     
85     public Class JavaDoc findReplicationClass(String JavaDoc name)
86         throws ClassNotFoundException JavaDoc, IOException JavaDoc {
87         return Class.forName(name, false, getClass().getClassLoader());
88     }
89
90     public Class JavaDoc findWebappClass(String JavaDoc name)
91         throws ClassNotFoundException JavaDoc, IOException JavaDoc {
92         return Class.forName(name, false, classLoader);
93     }
94
95
96 }
97
Popular Tags