KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 package org.openide.util.io;
21
22 import java.awt.Image JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectOutput JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.WeakHashMap JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36 import org.openide.util.WeakSet;
37
38 // note: keep method NbObjectInputStream.resolveObject
39
// consistent with replaceObject method
40

41 /** Object output stream that could in the future be smart about saving certain objects.
42 * Also static methods to safely write an object that could cause problems during later deserialization.
43 */

44 public class NbObjectOutputStream extends ObjectOutputStream JavaDoc {
45     private static final String JavaDoc SVUID = "serialVersionUID"; // NOI18N
46
private static final Set JavaDoc<String JavaDoc> alreadyReported = new WeakSet<String JavaDoc>();
47
48     static {
49         // See below.
50
alreadyReported.add("java.lang.Exception"); // NOI18N
51
alreadyReported.add("java.io.IOException"); // NOI18N
52
alreadyReported.add("java.util.TreeSet"); // NOI18N
53
alreadyReported.add("java.awt.geom.AffineTransform"); // NOI18N
54
}
55
56     private static Map JavaDoc<String JavaDoc,Boolean JavaDoc> examinedClasses = new WeakHashMap JavaDoc<String JavaDoc,Boolean JavaDoc>(250);
57     private final List JavaDoc<Class JavaDoc> serializing = new ArrayList JavaDoc<Class JavaDoc>(50);
58
59     /** Create a new object output.
60     * @param os the underlying output stream
61     * @throws IOException for the usual reasons
62     */

63     public NbObjectOutputStream(OutputStream JavaDoc os) throws IOException JavaDoc {
64         super(os);
65
66         try {
67             enableReplaceObject(true);
68         } catch (SecurityException JavaDoc ex) {
69             throw (IOException JavaDoc) new IOException JavaDoc(ex.toString()).initCause(ex);
70         }
71     }
72
73     /*
74     * @param obj is an Object to be checked for replace
75     */

76     public Object JavaDoc replaceObject(Object JavaDoc obj) throws IOException JavaDoc {
77         if (obj instanceof Image JavaDoc) {
78             return null;
79
80             // [LIGHT]
81
// additional code needed for full version
82
}
83
84         return super.replaceObject(obj);
85     }
86
87     /** Writes an object safely to the object output.
88      * Can be read by {@link NbObjectInputStream#readSafely}.
89     * @param oo object output to write to
90     * @param obj the object to write
91     * @exception SafeException if the object simply fails to be serialized
92     * @exception IOException if something more serious fails
93     */

94     public static void writeSafely(ObjectOutput JavaDoc oo, Object JavaDoc obj)
95     throws IOException JavaDoc {
96         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc(200);
97
98         try {
99             NbObjectOutputStream oos = new NbObjectOutputStream(bos);
100             oos.writeObject(obj);
101             oos.flush();
102             bos.close();
103         } catch (Exception JavaDoc exc) {
104             // exception during safe of the object
105
// encapsulate all exceptions into safe exception
106
oo.writeInt(0);
107             throw new SafeException(exc);
108         }
109
110         oo.writeInt(bos.size());
111         oo.write(bos.toByteArray());
112     }
113
114     protected void annotateClass(Class JavaDoc cl) throws IOException JavaDoc {
115         super.annotateClass(cl);
116
117         if (cl.isArray()) {
118             return;
119         }
120
121         if (cl.isInterface()) {
122             // TheInterface.class is being serialized, not an instance;
123
// no need for svuid.
124
return;
125         }
126
127         serializing.add(cl);
128
129         if (isSerialVersionUIDDeclared(cl)) {
130             return;
131         }
132
133         if (IOException JavaDoc.class.isAssignableFrom(cl)) {
134             // ObjectOutputStream for some reason stores IOException's that are
135
// thrown during serialization (they are rethrown later maybe?).
136
// It's no problem, just ignore them here.
137
return;
138         }
139
140         String JavaDoc classname = cl.getName();
141
142         if (alreadyReported.add(classname)) {
143             Set JavaDoc<Class JavaDoc> serializingUniq = new HashSet JavaDoc<Class JavaDoc>();
144             StringBuffer JavaDoc b = new StringBuffer JavaDoc("Serializable class "); // NOI18N
145
b.append(classname);
146             b.append(" does not declare serialVersionUID field. Encountered while storing: ["); // NOI18N
147

148             Iterator JavaDoc it = serializing.iterator();
149             boolean first = true;
150
151             while (it.hasNext()) {
152                 Class JavaDoc c = (Class JavaDoc) it.next();
153
154                 if ((c != cl) && serializingUniq.add(c)) {
155                     if (first) {
156                         first = false;
157                     } else {
158                         b.append(", "); // NOI18N
159
}
160
161                     b.append(c.getName());
162                 }
163             }
164
165             b.append("] See also http://www.netbeans.org/issues/show_bug.cgi?id=19915"); // NOI18N
166

167             String JavaDoc file = System.getProperty("InstanceDataObject.current.file"); // NOI18N
168

169             if ((file != null) && (file.length() > 0)) {
170                 b.append(" [may have been writing "); // NOI18N
171
b.append(file);
172                 b.append("]"); // NOI18N
173
}
174
175             Logger.getAnonymousLogger().warning(b.toString());
176         }
177     }
178
179     private static boolean isSerialVersionUIDDeclared(Class JavaDoc clazz) {
180         String JavaDoc classname = clazz.getName();
181         Boolean JavaDoc okay = examinedClasses.get(classname);
182
183         if (okay == null) {
184             if (classname.equals("java.util.HashSet") || classname.equals("java.util.ArrayList")) { // NOI18N
185
okay = Boolean.TRUE;
186             } else {
187                 okay = Boolean.FALSE;
188
189                 java.lang.reflect.Field JavaDoc[] flds = clazz.getDeclaredFields();
190
191                 for (int i = 0; i < flds.length; i++) {
192                     if (flds[i].getName().equals(SVUID)) {
193                         okay = Boolean.TRUE;
194
195                         break;
196                     }
197                 }
198             }
199
200             examinedClasses.put(clazz.getName(), okay);
201         }
202
203         return okay.booleanValue();
204     }
205 }
206
Popular Tags