ClassLoaderObjectInputStream.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.io.input;

  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.ObjectInputStream;
  21. import java.io.ObjectStreamClass;
  22. import java.io.StreamCorruptedException;
  23. import java.lang.reflect.Proxy;

  24. /**
  25.  * A special ObjectInputStream that loads a class based on a specified
  26.  * {@link ClassLoader} rather than the system default.
  27.  * <p>
  28.  * This is useful in dynamic container environments.
  29.  * </p>
  30.  *
  31.  * @since 1.1
  32.  */
  33. public class ClassLoaderObjectInputStream extends ObjectInputStream {

  34.     /** The class loader to use. */
  35.     private final ClassLoader classLoader;

  36.     /**
  37.      * Constructs a new ClassLoaderObjectInputStream.
  38.      *
  39.      * @param classLoader  the ClassLoader from which classes should be loaded
  40.      * @param inputStream  the InputStream to work on
  41.      * @throws IOException in case of an I/O error
  42.      * @throws StreamCorruptedException if the stream is corrupted
  43.      */
  44.     public ClassLoaderObjectInputStream(
  45.             final ClassLoader classLoader, final InputStream inputStream)
  46.             throws IOException, StreamCorruptedException {
  47.         super(inputStream);
  48.         this.classLoader = classLoader;
  49.     }

  50.     /**
  51.      * Resolve a class specified by the descriptor using the
  52.      * specified ClassLoader or the super ClassLoader.
  53.      *
  54.      * @param objectStreamClass  descriptor of the class
  55.      * @return the Class object described by the ObjectStreamClass
  56.      * @throws IOException in case of an I/O error
  57.      * @throws ClassNotFoundException if the Class cannot be found
  58.      */
  59.     @Override
  60.     protected Class<?> resolveClass(final ObjectStreamClass objectStreamClass)
  61.             throws IOException, ClassNotFoundException {
  62.         try {
  63.             return Class.forName(objectStreamClass.getName(), false, classLoader);
  64.         } catch (final ClassNotFoundException cnfe) {
  65.             // delegate to super class loader which can resolve primitives
  66.             return super.resolveClass(objectStreamClass);
  67.         }
  68.     }

  69.     /**
  70.      * Create a proxy class that implements the specified interfaces using
  71.      * the specified ClassLoader or the super ClassLoader.
  72.      *
  73.      * @param interfaces the interfaces to implement
  74.      * @return a proxy class implementing the interfaces
  75.      * @throws IOException in case of an I/O error
  76.      * @throws ClassNotFoundException if the Class cannot be found
  77.      * @see java.io.ObjectInputStream#resolveProxyClass(String[])
  78.      * @since 2.1
  79.      */
  80.     @Override
  81.     protected Class<?> resolveProxyClass(final String[] interfaces) throws IOException,
  82.             ClassNotFoundException {
  83.         final Class<?>[] interfaceClasses = new Class[interfaces.length];
  84.         for (int i = 0; i < interfaces.length; i++) {
  85.             interfaceClasses[i] = Class.forName(interfaces[i], false, classLoader);
  86.         }
  87.         try {
  88.             return Proxy.getProxyClass(classLoader, interfaceClasses);
  89.         } catch (final IllegalArgumentException e) {
  90.             return super.resolveProxyClass(interfaces);
  91.         }
  92.     }

  93. }