001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.wsif.base;
059:
060: import java.io.*;
061:
062: /**
063: * Class <code>FObjectInputStream</code> overloads the class <code>ObjectInputStream</code>
064: * to solve the bug reported under BugID
065: * <a href="http://developer.java.sun.com/developer/bugParade/bugs/4171142.html">4171142</a> .
066: * <br>
067: * If an object contains primitiv data types and you try to serialize/deserialize
068: * it than you will get an ClassNotFoundException for the primitiv data type.
069: * Reason for this exception is that the ObjectInputStream try to resolve the
070: * data type from java.lang.<primitiv> which does not work.
071: * <br>
072: * The solution for the problem is to override the method resolveClass and to
073: * bypass the resolution for primitiv data types.
074: *
075: * @author <a href="mailto:adietzsch@de.ibm.com?subject=FObjectInputStream">Alexander Dietzsch</a>
076: */
077: class WSIFObjectInputStream extends ObjectInputStream {
078: /**
079: * Create an ObjectInputStream that reads from the specified InputStream.
080: * The stream header containing the magic number and version number
081: * are read from the stream and verified. This method will block
082: * until the corresponding ObjectOutputStream has written and flushed the
083: * header.
084: *
085: * @param in the underlying <code>InputStream</code> from which to read
086: * @exception StreamCorruptedException The version or magic number are
087: * incorrect.
088: * @exception IOException An exception occurred in the underlying stream.
089: */
090: WSIFObjectInputStream(InputStream in) throws IOException,
091: StreamCorruptedException {
092:
093: super (in);
094: }
095:
096: /**
097: * Load the local class equivalent of the specified stream class description.
098: *
099: * Subclasses may implement this method to allow classes to be
100: * fetched from an alternate source.
101: *
102: * The corresponding method in ObjectOutputStream is
103: * annotateClass. This method will be invoked only once for each
104: * unique class in the stream. This method can be implemented by
105: * subclasses to use an alternate loading mechanism but must
106: * return a Class object. Once returned, the serialVersionUID of the
107: * class is compared to the serialVersionUID of the serialized class.
108: * If there is a mismatch, the deserialization fails and an exception
109: * is raised. <p>
110: *
111: * By default the class name is resolved relative to the class
112: * that called readObject. <p>
113: *
114: * @param v an instance of class ObjectStreamClass
115: * @return a Class object corresponding to <code>v</code>
116: * @exception IOException Any of the usual Input/Output exceptions.
117: * @exception ClassNotFoundException If class of
118: * a serialized object cannot be found.
119: */
120: protected Class resolveClass(ObjectStreamClass v)
121: throws IOException, ClassNotFoundException {
122:
123: try {
124: // return super.resolveClass(v);
125: return Class.forName(v.getName(), true, Thread
126: .currentThread().getContextClassLoader());
127: } catch (ClassNotFoundException xcpt) {
128: String className = v.getName();
129: if (className.equals("boolean")) {
130: return boolean.class;
131: }
132: if (className.equals("char")) {
133: return char.class;
134: }
135: if (className.equals("byte")) {
136: return byte.class;
137: }
138: if (className.equals("short")) {
139: return short.class;
140: }
141: if (className.equals("int")) {
142: return int.class;
143: }
144: if (className.equals("long")) {
145: return long.class;
146: }
147: if (className.equals("float")) {
148: return float.class;
149: }
150: if (className.equals("double")) {
151: return double.class;
152: }
153:
154: throw xcpt;
155: }
156: }
157: }
|