001    /*
002     * Copyright (c) 2005 Einar Pehrson <einar@pehrson.nu>.
003     *
004     * This file is part of
005     * CleanSheets - a spreadsheet application for the Java platform.
006     *
007     * CleanSheets is free software; you can redistribute it and/or modify
008     * it under the terms of the GNU General Public License as published by
009     * the Free Software Foundation; either version 2 of the License, or
010     * (at your option) any later version.
011     *
012     * CleanSheets is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with CleanSheets; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
020     */
021    package csheets.io;
022    
023    import java.io.IOException;
024    import java.io.InputStream;
025    import java.io.ObjectInputStream;
026    import java.io.ObjectStreamClass;
027    
028    /**
029     * An object input stream that resolves class descriptors through a given
030     * class loader, and thereby allowing dynamic deserialization.
031     * @author Einar Pehrson
032     */
033    public class DynamicObjectInputStream extends ObjectInputStream {
034    
035            /** The class loader queried for class descriptor resolution */
036            private ClassLoader loader;
037    
038            /**
039             * Creates a new dynamic object input stream.
040             * @param stream the input stream to read from
041             * @param loader the class loader queried for class descriptor resolution
042             * @throws IOException if an I/O error occurs while reading stream header
043             */
044            public DynamicObjectInputStream(InputStream stream, ClassLoader loader)
045                            throws IOException {
046                    super(stream);
047                    this.loader = loader;
048            }
049    
050            /**
051             * Load the local class equivalent of the specified stream class descriptor,
052             * by first querying the given class loader.
053             * @param desc the class descriptor
054             * @throws IOException if an I/O error occurs while resolving
055             * @throws ClassNotFoundException if the class of a serialized object cannot be found
056             */
057            protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException,
058                    ClassNotFoundException {
059                    try {
060                            return Class.forName(desc.getName(), false, loader);
061                    } catch (ClassNotFoundException ex) {
062                            return super.resolveClass(desc);
063                    }
064            }
065    }