KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > io > NbObjectInputStream


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.util.io;
20
21 import org.openide.util.Lookup;
22 import org.openide.util.Utilities;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.ObjectInput JavaDoc;
28 import java.io.ObjectInputStream JavaDoc;
29 import java.io.ObjectStreamClass JavaDoc;
30
31 import java.lang.reflect.InvocationTargetException JavaDoc;
32 import org.openide.util.Exceptions;
33
34
35 // note: keep method resolveObject consistent with NbObjectOutputStream.replaceObject
36

37 /** Controlled deserialization stream using the system class loader.
38 * Employs the classloader available from lookup (currently that used for modules).
39 * Also contains static methods to safely read objects that might have problems
40 * during deserialization that should not corrupt the stream. The stream also provides
41 * support for changing name of stored classes.
42 *
43 * @see #readClassDescriptor
44 */

45 public class NbObjectInputStream extends ObjectInputStream JavaDoc {
46     /** Create a new object input.
47     * @param is underlying input stream
48     * @throws IOException for the usual reasons
49     */

50     public NbObjectInputStream(InputStream JavaDoc is) throws IOException JavaDoc {
51         super(is);
52
53         try {
54             enableResolveObject(true);
55         } catch (SecurityException JavaDoc ex) {
56             throw new IOException JavaDoc(ex.toString());
57         }
58     }
59
60     /* Uses NetBeans module classloader to load the class.
61      * @param v description of the class to load
62      */

63     protected Class JavaDoc resolveClass(ObjectStreamClass JavaDoc v) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
64         ClassLoader JavaDoc cl = getNBClassLoader();
65
66         try {
67             return Class.forName(v.getName(), false, cl);
68         } catch (ClassNotFoundException JavaDoc cnfe) {
69             String JavaDoc msg = "Offending classloader: " + cl; // NOI18N
70
Exceptions.attachMessage(cnfe, msg);
71             throw cnfe;
72         }
73     }
74
75     /** Lazy create default NB classloader for use during deserialization. */
76     private static ClassLoader JavaDoc getNBClassLoader() {
77         ClassLoader JavaDoc c = Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
78
79         return (c != null) ? c : ClassLoader.getSystemClassLoader();
80     }
81
82     /** Provides a special handling for renaming of serialized classes.
83      * <P>
84      * Often, as the time goes the serialized classes evolve. They can be moved
85      * to new packages, renamed or changed (by a mistake) to not reflect the
86      * version of class stored in previous sessions.
87      * <P>
88      * This method deals with some of this incompatibilites and provides the
89      * module owners a way how to fix some of them.
90      * <P>
91      * When a class is read, the <link>Utilities.translate</link> is consulted
92      * to find out what whether the name of the class is listed there and
93      * what new value is assigned to it. This allows complete rename of the
94      * serialized class. For example:
95      * <code>org.netbeans.core.NbMainExplorer</code>
96      * can be renamed to
97      * <code>org.netbeans.core.ui.NbExp</code> - of course supposing that
98      * the new class is able to read the serialized fields of the old one.
99      * <P>
100      * Another useful feature of this method is the ability to supress wrong
101      * <code>serialVersionUID</code>. This was causing us a lot of problems,
102      * because people were forgetting to specify the <code>serialVersionUID</code>
103      * field in their sources and then it was hard to recover from it. Right
104      * now we have a solution: Just use <link>Utilities.translate</link> framework
105      * to assing your class <code>org.yourpackage.YourClass</code> the same
106      * name as it had e.g. <code>org.yourpackage.YourClass</code>. This will
107      * be interpreted by this method as a hit to suppress <code>serialVersionUID</code>
108      * and the <code>NbObjectInputStream</code> will ignore its value.
109      * <P>
110      * Please see <link>Utilities.translate</link> to learn how your module
111      * can provide list of classes that changed name or want to suppress <code>serialVersionUID</code>.
112      *
113      */

114     protected ObjectStreamClass JavaDoc readClassDescriptor() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
115         ObjectStreamClass JavaDoc ose = super.readClassDescriptor();
116
117         String JavaDoc name = ose.getName();
118         String JavaDoc newN = Utilities.translate(name);
119
120         if (name == newN) {
121             // no translation
122
return ose;
123         }
124
125         // otherwise reload the ObjectStreamClass to contain the local copy
126
ClassLoader JavaDoc cl = getNBClassLoader();
127         Class JavaDoc clazz = Class.forName(newN, false, cl);
128
129         ObjectStreamClass JavaDoc newOse = ObjectStreamClass.lookup(clazz);
130
131         // #28021 - it is possible that lookup return null. In that case the conversion
132
// table contains class which is not Serializable or Externalizable.
133
if (newOse == null) {
134             throw new java.io.NotSerializableException JavaDoc(newN);
135         }
136
137         return newOse;
138     }
139
140     /** Reads an object from the given object input.
141     * The object had to be saved by the {@link NbObjectOutputStream#writeSafely} method.
142     *
143     * @param oi object input
144     * @return the read object
145     * @exception IOException if IO error occured
146     * @exception SafeException if the operation failed but the stream is ok
147     * for further reading
148     */

149     public static Object JavaDoc readSafely(ObjectInput JavaDoc oi) throws IOException JavaDoc {
150         int size = oi.readInt();
151         byte[] byteArray = new byte[size];
152         oi.readFully(byteArray, 0, size);
153
154         try {
155             ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(byteArray);
156             NbObjectInputStream ois = new NbObjectInputStream(bis);
157             Object JavaDoc obj = ois.readObject();
158             bis.close();
159
160             return obj;
161         } catch (Exception JavaDoc exc) {
162             // encapsulate all exceptions into safe exception
163
throw new SafeException(exc);
164         } catch (LinkageError JavaDoc le) {
165             throw new SafeException(new InvocationTargetException JavaDoc(le));
166         }
167     }
168
169     /** Skips an object from the given object input without loading it.
170     * The object had to be saved by the {@link NbObjectOutputStream#writeSafely} method.
171     *
172     * @param oi object input
173     * @exception IOException if an I/O error occurred
174     */

175     public static void skipSafely(ObjectInput JavaDoc oi) throws IOException JavaDoc {
176         int size = oi.readInt();
177         oi.skip(size);
178     }
179 }
180
Popular Tags