View Javadoc

1   /*****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.util.io;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.ObjectInputStream;
25  import java.io.ObjectStreamClass;
26  import java.io.StreamCorruptedException;
27  
28  /***
29   * A special ObjectInputStream to handle highly transient classes hosted
30   * by Avalon components that are juggling many classloaders.
31   *
32   * @author <a href="mailto:paul_hammant@yahoo.com">Paul Hammant</a>
33   * @version $Revision: 494012 $ $Date: 2007-01-08 10:23:58 +0000 (Mon, 08 Jan 2007) $
34   */
35  public class ClassLoaderObjectInputStream
36      extends ObjectInputStream
37  {
38      private ClassLoader m_classLoader;
39  
40      public ClassLoaderObjectInputStream( final ClassLoader classLoader,
41                                           final InputStream inputStream )
42          throws IOException, StreamCorruptedException
43      {
44          super( inputStream );
45          m_classLoader = classLoader;
46      }
47  
48      protected Class resolveClass( final ObjectStreamClass objectStreamClass )
49          throws IOException, ClassNotFoundException
50      {
51          final Class clazz =
52              Class.forName( objectStreamClass.getName(), false, m_classLoader );
53  
54          if( null != clazz )
55          {
56              return clazz; // the classloader knows of the class
57          }
58          else
59          {
60              // classloader knows not of class, let the super classloader do it
61              return super.resolveClass( objectStreamClass );
62          }
63      }
64  }