View Javadoc

1   package org.apache.turbine.util.pool;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.ObjectInputStream;
22  import java.io.ObjectStreamClass;
23  
24  /***
25   * A deserialization stream for a specific class loader context.
26   *
27   * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
28   * @version $Id: ObjectInputStreamForContext.java 264148 2005-08-29 14:21:04Z henning $
29   */
30  public class ObjectInputStreamForContext extends ObjectInputStream
31  {
32      /***
33       * The class loader of the context.
34       */
35      private ClassLoader classLoader;
36  
37      /***
38       * Contructs a new object stream for a context.
39       *
40       * @param in the serialized input stream.
41       * @param loader the class loader of the context.
42       * @throws IOException on errors.
43       */
44      public ObjectInputStreamForContext(InputStream in,
45                                         ClassLoader loader)
46              throws IOException
47      {
48          super(in);
49          classLoader = loader;
50      }
51  
52      protected Class resolveClass(ObjectStreamClass v)
53              throws IOException,
54              ClassNotFoundException
55      {
56          return classLoader == null ?
57                  super.resolveClass(v) : classLoader.loadClass(v.getName());
58      }
59  }