1bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook/*
2bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Licensed to the Apache Software Foundation (ASF) under one or more
3bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * contributor license agreements.  See the NOTICE file distributed with
4bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * this work for additional information regarding copyright ownership.
5bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * The ASF licenses this file to You under the Apache License, Version 2.0
6bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * (the "License"); you may not use this file except in compliance with
7bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * the License.  You may obtain a copy of the License at
8bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook *
9bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook *      http://www.apache.org/licenses/LICENSE-2.0
10bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook *
11bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Unless required by applicable law or agreed to in writing, software
12bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * distributed under the License is distributed on an "AS IS" BASIS,
13bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * See the License for the specific language governing permissions and
15bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * limitations under the License.
16bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */
17bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookpackage org.apache.commons.io.input;
18bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook
19bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookimport java.io.IOException;
20bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookimport java.io.InputStream;
21bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookimport java.io.ObjectInputStream;
22bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookimport java.io.ObjectStreamClass;
23bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookimport java.io.StreamCorruptedException;
24bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook
25bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook/**
26bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * A special ObjectInputStream that loads a class based on a specified
27bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * <code>ClassLoader</code> rather than the system default.
28bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * <p>
29bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * This is useful in dynamic container environments.
30bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook *
31bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @author Paul Hammant
32bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @version $Id: ClassLoaderObjectInputStream.java 437567 2006-08-28 06:39:07Z bayard $
33bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @since Commons IO 1.1
34bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */
35bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookpublic class ClassLoaderObjectInputStream extends ObjectInputStream {
36bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook
37bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook    /** The class loader to use. */
38bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook    private ClassLoader classLoader;
39bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook
40bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook    /**
41bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook     * Constructs a new ClassLoaderObjectInputStream.
42bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook     *
43bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook     * @param classLoader  the ClassLoader from which classes should be loaded
44bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook     * @param inputStream  the InputStream to work on
45bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook     * @throws IOException in case of an I/O error
46bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook     * @throws StreamCorruptedException if the stream is corrupted
47bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook     */
48bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook    public ClassLoaderObjectInputStream(
49bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook            ClassLoader classLoader, InputStream inputStream)
50bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook            throws IOException, StreamCorruptedException {
51bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook        super(inputStream);
52bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook        this.classLoader = classLoader;
53bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook    }
54bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook
55bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook    /**
56bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook     * Resolve a class specified by the descriptor using the
57bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook     * specified ClassLoader or the super ClassLoader.
58bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook     *
59bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook     * @param objectStreamClass  descriptor of the class
60bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook     * @return the Class object described by the ObjectStreamClass
61bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook     * @throws IOException in case of an I/O error
62bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook     * @throws ClassNotFoundException if the Class cannot be found
63bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook     */
64bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook    protected Class resolveClass(ObjectStreamClass objectStreamClass)
65bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook            throws IOException, ClassNotFoundException {
66bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook
67bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook        Class clazz = Class.forName(objectStreamClass.getName(), false, classLoader);
68bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook
69bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook        if (clazz != null) {
70bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook            // the classloader knows of the class
71bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook            return clazz;
72bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook        } else {
73bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook            // classloader knows not of class, let the super classloader do it
74bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook            return super.resolveClass(objectStreamClass);
75bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook        }
76bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook    }
77bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook}
78