14fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/* 24fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Licensed to the Apache Software Foundation (ASF) under one or more 34fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * contributor license agreements. See the NOTICE file distributed with 44fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * this work for additional information regarding copyright ownership. 54fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * The ASF licenses this file to You under the Apache License, Version 2.0 64fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * (the "License"); you may not use this file except in compliance with 74fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * the License. You may obtain a copy of the License at 84fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * 94fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * http://www.apache.org/licenses/LICENSE-2.0 104fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * 114fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Unless required by applicable law or agreed to in writing, software 124fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * distributed under the License is distributed on an "AS IS" BASIS, 134fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 144fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * See the License for the specific language governing permissions and 154fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * limitations under the License. 164fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */ 174fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypackage org.apache.commons.io.input; 184fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 194fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.IOException; 204fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.InputStream; 214fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.ObjectInputStream; 224fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.ObjectStreamClass; 234fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.StreamCorruptedException; 244fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 254fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/** 264fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * A special ObjectInputStream that loads a class based on a specified 274fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <code>ClassLoader</code> rather than the system default. 284fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p> 294fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * This is useful in dynamic container environments. 304fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * 314fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @author Paul Hammant 324fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @version $Id: ClassLoaderObjectInputStream.java 437567 2006-08-28 06:39:07Z bayard $ 334fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @since Commons IO 1.1 344fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */ 354fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypublic class ClassLoaderObjectInputStream extends ObjectInputStream { 364fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 374fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** The class loader to use. */ 384fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy private ClassLoader classLoader; 394fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 404fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** 414fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Constructs a new ClassLoaderObjectInputStream. 424fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * 434fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @param classLoader the ClassLoader from which classes should be loaded 444fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @param inputStream the InputStream to work on 454fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @throws IOException in case of an I/O error 464fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @throws StreamCorruptedException if the stream is corrupted 474fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */ 484fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy public ClassLoaderObjectInputStream( 494fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy ClassLoader classLoader, InputStream inputStream) 504fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy throws IOException, StreamCorruptedException { 514fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy super(inputStream); 524fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy this.classLoader = classLoader; 534fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy } 544fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 554fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** 564fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Resolve a class specified by the descriptor using the 574fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * specified ClassLoader or the super ClassLoader. 584fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * 594fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @param objectStreamClass descriptor of the class 604fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @return the Class object described by the ObjectStreamClass 614fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @throws IOException in case of an I/O error 624fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @throws ClassNotFoundException if the Class cannot be found 634fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */ 644fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy protected Class resolveClass(ObjectStreamClass objectStreamClass) 654fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy throws IOException, ClassNotFoundException { 664fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 674fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy Class clazz = Class.forName(objectStreamClass.getName(), false, classLoader); 684fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 694fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy if (clazz != null) { 704fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy // the classloader knows of the class 714fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy return clazz; 724fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy } else { 734fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy // classloader knows not of class, let the super classloader do it 744fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy return super.resolveClass(objectStreamClass); 754fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy } 764fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy } 774fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy} 78