1 //****************************************************************************** 2 // 3 // File: MPObjectInputStream.java 4 // Package: edu.rit.mp 5 // Unit: Class edu.rit.mp.MPObjectInputStream 6 // 7 // This Java source file is copyright (C) 2006 by Alan Kaminsky. All rights 8 // reserved. For further information, contact the author, Alan Kaminsky, at 9 // ark@cs.rit.edu. 10 // 11 // This Java source file is part of the Parallel Java Library ("PJ"). PJ is free 12 // software; you can redistribute it and/or modify it under the terms of the GNU 13 // General Public License as published by the Free Software Foundation; either 14 // version 3 of the License, or (at your option) any later version. 15 // 16 // PJ is distributed in the hope that it will be useful, but WITHOUT ANY 17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 18 // A PARTICULAR PURPOSE. See the GNU General Public License for more details. 19 // 20 // Linking this library statically or dynamically with other modules is making a 21 // combined work based on this library. Thus, the terms and conditions of the GNU 22 // General Public License cover the whole combination. 23 // 24 // As a special exception, the copyright holders of this library give you 25 // permission to link this library with independent modules to produce an 26 // executable, regardless of the license terms of these independent modules, and 27 // to copy and distribute the resulting executable under terms of your choice, 28 // provided that you also meet, for each linked independent module, the terms 29 // and conditions of the license of that module. An independent module is a module 30 // which is not derived from or based on this library. If you modify this library, 31 // you may extend this exception to your version of the library, but you are not 32 // obligated to do so. If you do not wish to do so, delete this exception 33 // statement from your version. 34 // 35 // A copy of the GNU General Public License is provided in the file gpl.txt. You 36 // may also obtain a copy of the GNU General Public License on the World Wide 37 // Web at http://www.gnu.org/licenses/gpl.html. 38 // 39 //****************************************************************************** 40 package edu.rit.mp; 41 42 import java.io.IOException; 43 import java.io.InputStream; 44 import java.io.ObjectInputStream; 45 import java.io.ObjectStreamClass; 46 47 /** 48 * Class MPObjectInputStream provides an object input stream that can load 49 * classes from an alternate class loader. 50 * 51 * @author Alan Kaminsky 52 * @version 31-Jan-2006 53 */ 54 class MPObjectInputStream 55 extends ObjectInputStream { 56 57 // Hidden data members. 58 private ClassLoader myClassLoader; 59 60 // Exported constructors. 61 /** 62 * Create a new MP object input stream. An alternate class loader will not 63 * be used. 64 * 65 * @param in Underlying input stream. 66 * @exception IOException Thrown if an I/O error occurred. 67 * @throws java.io.IOException if any. 68 */ 69 public MPObjectInputStream(InputStream in) 70 throws IOException { 71 super(in); 72 } 73 74 /** 75 * Create a new MP object input stream. The given class loader will be used 76 * to load classes; if null, an alternate class loader will not be used. 77 * 78 * @param in Underlying input stream. 79 * @param cl Alternate class loader, or null. 80 * @exception IOException Thrown if an I/O error occurred. 81 * @throws java.io.IOException if any. 82 */ 83 public MPObjectInputStream(InputStream in, 84 ClassLoader cl) 85 throws IOException { 86 super(in); 87 myClassLoader = cl; 88 } 89 90 // Hidden operations. 91 /** 92 * {@inheritDoc} 93 * 94 * Load the local class equivalent of the specified stream class 95 * description. 96 * @exception IOException Thrown if an I/O error occurred. 97 * @exception ClassNotFoundException Thrown if the local class could not be 98 * found. 99 */ 100 protected Class<?> resolveClass(ObjectStreamClass desc) 101 throws IOException, ClassNotFoundException { 102 try { 103 return super.resolveClass(desc); 104 } catch (ClassNotFoundException exc) { 105 if (myClassLoader != null) { 106 return Class.forName(desc.getName(), false, myClassLoader); 107 } else { 108 throw exc; 109 } 110 } 111 } 112 113 }