source: trunk/gsdl3/src/packages/javagdbm/java/au/com/pharos/meta/SerializationDumper.java@ 10737

Last change on this file since 10737 was 10737, checked in by kjdon, 19 years ago

Java Wrapper for GDBM, from Martin Pool. Original website gone, so added it all in here. I have modified the Makefiles to work in greenstone, and on macs, and added windows makefiles

  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1/*
2 * Copyright (C) 1997 Pharos IP Pty Ltd
3 * $Id: SerializationDumper.java 10737 2005-10-19 03:06:40Z kjdon $
4 * Confidential. All rights reserved.
5 */
6
7package au.com.pharos.meta;
8
9import java.lang.reflect.Field;
10
11import java.io.EOFException;
12import java.io.IOException;
13import java.io.InvalidClassException;
14import java.io.InvalidObjectException;
15import java.io.ObjectInput;
16import java.io.ObjectInputStream;
17import java.io.ObjectStreamException;
18import java.io.OptionalDataException;
19import java.io.PrintWriter;
20
21/*
22 * TODO: Allow people to specify an input file?
23 * TODO: Allow people to customize the display format?
24 *
25 * FIXME: We should use a custom classloader to load the untrusted
26 * classes under examination and to allow for flexible handling or
27 * tracing as classes are loaded.
28 */
29
30
31/** Java utility to display in a debugging format the contents of a
32 * serialized object stream.
33 *
34 * <P>Usage:
35 * <PRE>
36 * java au.com.pharos.meta.SerializationDumper <SERFILE
37 * </PRE>
38 *
39 * where SERFILE is the name of the serialization file.
40 *
41 * <P><B>Limitations:</B>
42 *
43 * <OL>
44 * <P><LI>Values are displayed only for public fields, due to Java
45 * security restrictions.
46 * <P><LI>Only top-level objects are dumped.
47 * </OL>
48 *
49 * <P>These are fairly serious restrictions, but SerializationDumper
50 * at least checks that the stream is well-formed, and gives some
51 * indication of what it contains.
52 *
53 * @author Martin Pool
54 * @version $Revision: 10737 $
55 **/
56public class SerializationDumper {
57 public static void main(String argv[])
58 {
59 try {
60 ObjectInput in = new ObjectInputStream(System.in);
61 PrintWriter writer = new PrintWriter(System.out);
62 SerializationDumper dumper = new SerializationDumper();
63 dumper.dump(in, writer);
64 writer.flush();
65 } catch (IOException e) {
66 e.printStackTrace();
67 }
68 }
69
70
71 /** Dump all objects read from <EM>in</EM> to <EM>out</EM>
72 * in a human-readable debugging form.
73 **/
74 public void dump(ObjectInput in, PrintWriter out)
75 throws IOException
76 {
77 while (true) { // exception on eof
78 Object obj = null;
79 try {
80 obj = in.readObject();
81 displayObject(obj, out);
82 }
83 catch (ClassNotFoundException e) {
84 warning(e, out);
85 }
86 catch (OptionalDataException e) {
87 error(e, out);
88 return;
89 }
90 catch (ObjectStreamException e) {
91 warning(e, out);
92 }
93 catch (EOFException e) {
94 return;
95 }
96 }
97 }
98
99
100 /** Display an object in human-readable form. **/
101 private void displayObject(Object obj, PrintWriter out)
102 throws IOException
103 {
104 if (obj == null) {
105 out.println("(null)");
106 return;
107 }
108
109 Class cls = obj.getClass();
110
111 // Print the header: the name, hashcode, and string
112 // representation
113 out.println(cls.getName() + "@0x" +
114 Integer.toHexString(obj.hashCode()) + "{");
115 out.println("\t\"" + obj.toString() + "\"");
116
117 // TODO: Recursive descent of the graph, allowing for circular
118 // structures.
119
120 // Print each field of the object
121 Field[] fields = cls.getDeclaredFields();
122 for (int i = 0; i < fields.length; i++)
123 displayField(obj, fields[i], out);
124
125 out.println("}");
126 }
127
128
129 /** Display a particular field from an object in human-readable
130 * form. Fields which are not accessible for security reasons
131 * are noted as such, and the name and type of the field is
132 * still printed. **/
133 private void displayField(Object obj, Field fld, PrintWriter out)
134 throws IOException
135 {
136 int flags = fld.getModifiers();
137 out.print("\t");
138 out.print(fld.toString() + ": ");
139 try {
140 Object value = fld.get(obj);
141 if (value == null) {
142 out.println("(null)");
143 }
144 else {
145 out.println(value.getClass().getName() + "@0x" +
146 Integer.toHexString(value.hashCode()) + ":");
147 out.println("\t\t\"" + value.toString() + "\"");
148 }
149 } catch (IllegalAccessException e) {
150 out.println("(not accessible)");
151 }
152 }
153
154
155 /** Display a warning that an exception occurred while dumping
156 * information. **/
157 private void warning(Exception e, PrintWriter out)
158 throws IOException
159 {
160 out.println("warning: " + e);
161 }
162
163 /** Display an exception that occurred while dumping
164 * information. **/
165 private void error(Exception e, PrintWriter out)
166 throws IOException
167 {
168 out.println("error: " + e.toString());
169 }
170}
171
Note: See TracBrowser for help on using the repository browser.