View Javadoc
1   package org.apache.fulcrum.factory.utils;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.InputStream;
23  import java.io.IOException;
24  import java.io.ObjectInputStream;
25  import java.io.ObjectStreamClass;
26  
27  /**
28   * A deserialization stream for a specific class loader context.
29   *
30   * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
31   * @version $Id$
32   */
33  public class ObjectInputStreamForContext extends ObjectInputStream
34  {
35      /**
36       * The class loader of the context.
37       */
38      private ClassLoader classLoader;
39  
40      /**
41       * Required to make satisfy the proxy methods
42       * 
43       * @throws IOException Generic exception
44       */
45      public ObjectInputStreamForContext()
46          throws IOException
47      {
48      }
49  
50      /**
51       * This method will construct a new object stream for a context.
52       *
53       * @param in the serialized input stream.
54       * @param loader the class loader of the context.
55       * @throws IOException on errors.
56       */
57      public  ObjectInputStreamForContext(InputStream in,
58                                          ClassLoader loader)
59                                          throws IOException
60      {
61          super(in);
62          classLoader = loader;
63      }
64  
65      /**
66       * {@link java.io.ObjectInputStream#resolveClass(ObjectStreamClass)}
67       * 
68       * @param v ObjectStreamClass to resolve
69       * @return {@inheritDoc} class to resolve
70       * @throws IOException if object stream not found
71       * @throws ClassNotFoundException if class not found
72       */
73      protected Class<?> resolveClass(ObjectStreamClass v)
74                                   throws IOException,
75                                   ClassNotFoundException
76      {
77          return classLoader == null ?
78              super.resolveClass(v) : classLoader.loadClass(v.getName());
79      }
80  }