001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.io.input;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.io.ObjectInputStream;
022import java.io.ObjectStreamClass;
023import java.io.StreamCorruptedException;
024import java.lang.reflect.Proxy;
025
026/**
027 * A special ObjectInputStream that loads a class based on a specified
028 * {@link ClassLoader} rather than the system default.
029 * <p>
030 * This is useful in dynamic container environments.
031 * </p>
032 *
033 * @since 1.1
034 */
035public class ClassLoaderObjectInputStream extends ObjectInputStream {
036
037    /** The class loader to use. */
038    private final ClassLoader classLoader;
039
040    /**
041     * Constructs a new ClassLoaderObjectInputStream.
042     *
043     * @param classLoader  the ClassLoader from which classes should be loaded
044     * @param inputStream  the InputStream to work on
045     * @throws IOException in case of an I/O error
046     * @throws StreamCorruptedException if the stream is corrupted
047     */
048    public ClassLoaderObjectInputStream(
049            final ClassLoader classLoader, final InputStream inputStream)
050            throws IOException, StreamCorruptedException {
051        super(inputStream);
052        this.classLoader = classLoader;
053    }
054
055    /**
056     * Resolve a class specified by the descriptor using the
057     * specified ClassLoader or the super ClassLoader.
058     *
059     * @param objectStreamClass  descriptor of the class
060     * @return the Class object described by the ObjectStreamClass
061     * @throws IOException in case of an I/O error
062     * @throws ClassNotFoundException if the Class cannot be found
063     */
064    @Override
065    protected Class<?> resolveClass(final ObjectStreamClass objectStreamClass)
066            throws IOException, ClassNotFoundException {
067
068        try {
069            return Class.forName(objectStreamClass.getName(), false, classLoader);
070        } catch (final ClassNotFoundException cnfe) {
071            // delegate to super class loader which can resolve primitives
072            return super.resolveClass(objectStreamClass);
073        }
074    }
075
076    /**
077     * Create a proxy class that implements the specified interfaces using
078     * the specified ClassLoader or the super ClassLoader.
079     *
080     * @param interfaces the interfaces to implement
081     * @return a proxy class implementing the interfaces
082     * @throws IOException in case of an I/O error
083     * @throws ClassNotFoundException if the Class cannot be found
084     * @see java.io.ObjectInputStream#resolveProxyClass(String[])
085     * @since 2.1
086     */
087    @Override
088    protected Class<?> resolveProxyClass(final String[] interfaces) throws IOException,
089            ClassNotFoundException {
090        final Class<?>[] interfaceClasses = new Class[interfaces.length];
091        for (int i = 0; i < interfaces.length; i++) {
092            interfaceClasses[i] = Class.forName(interfaces[i], false, classLoader);
093        }
094        try {
095            return Proxy.getProxyClass(classLoader, interfaceClasses);
096        } catch (final IllegalArgumentException e) {
097            return super.resolveProxyClass(interfaces);
098        }
099    }
100
101}