2
|
1 |
/*
|
|
2 |
* Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package javax.sql.rowset.serial;
|
|
27 |
|
|
28 |
import java.sql.*;
|
|
29 |
import java.io.*;
|
|
30 |
import java.util.Map;
|
|
31 |
import java.lang.reflect.*;
|
|
32 |
import javax.sql.rowset.RowSetWarning;
|
|
33 |
|
|
34 |
/**
|
|
35 |
* A serializable mapping in the Java programming language of an SQL
|
|
36 |
* <code>JAVA_OBJECT</code> value. Assuming the Java object
|
|
37 |
* implements the <code>Serializable</code> interface, this class simply wraps the
|
|
38 |
* serialization process.
|
|
39 |
* <P>
|
|
40 |
* If however, the serialization is not possible because
|
|
41 |
* the Java object is not immediately serializable, this class will
|
|
42 |
* attempt to serialize all non-static members to permit the object
|
|
43 |
* state to be serialized.
|
|
44 |
* Static or transient fields cannot be serialized; an attempt to serialize
|
|
45 |
* them will result in a <code>SerialException</code> object being thrown.
|
|
46 |
*
|
|
47 |
* @author Jonathan Bruce
|
|
48 |
*/
|
|
49 |
public class SerialJavaObject implements Serializable, Cloneable {
|
|
50 |
|
|
51 |
/**
|
|
52 |
* Placeholder for object to be serialized.
|
|
53 |
*/
|
|
54 |
private Object obj;
|
|
55 |
|
|
56 |
|
|
57 |
/**
|
|
58 |
* Placeholder for all fields in the <code>JavaObject</code> being serialized.
|
|
59 |
*/
|
|
60 |
private transient Field[] fields;
|
|
61 |
|
|
62 |
/**
|
|
63 |
* Constructor for <code>SerialJavaObject</code> helper class.
|
|
64 |
* <p>
|
|
65 |
*
|
|
66 |
* @param obj the Java <code>Object</code> to be serialized
|
|
67 |
* @throws SerialException if the object is found
|
|
68 |
* to be unserializable
|
|
69 |
*/
|
|
70 |
public SerialJavaObject(Object obj) throws SerialException {
|
|
71 |
|
|
72 |
// if any static fields are found, an exception
|
|
73 |
// should be thrown
|
|
74 |
|
|
75 |
|
|
76 |
// get Class. Object instance should always be available
|
|
77 |
Class c = obj.getClass();
|
|
78 |
|
|
79 |
// determine if object implements Serializable i/f
|
|
80 |
boolean serializableImpl = false;
|
|
81 |
Class[] theIf = c.getInterfaces();
|
|
82 |
for (int i = 0; i < theIf.length; i++) {
|
|
83 |
String ifName = theIf[i].getName();
|
|
84 |
if (ifName == "java.io.Serializable") {
|
|
85 |
serializableImpl = true;
|
|
86 |
}
|
|
87 |
}
|
|
88 |
|
|
89 |
// can only determine public fields (obviously). If
|
|
90 |
// any of these are static, this should invalidate
|
|
91 |
// the action of attempting to persist these fields
|
|
92 |
// in a serialized form
|
|
93 |
|
|
94 |
boolean anyStaticFields = false;
|
|
95 |
fields = c.getFields();
|
|
96 |
//fields = new Object[field.length];
|
|
97 |
|
|
98 |
for (int i = 0; i < fields.length; i++ ) {
|
|
99 |
if ( fields[i].getModifiers() == Modifier.STATIC ) {
|
|
100 |
anyStaticFields = true;
|
|
101 |
}
|
|
102 |
//fields[i] = field[i].get(obj);
|
|
103 |
}
|
|
104 |
try {
|
|
105 |
if (!(serializableImpl)) {
|
|
106 |
throw new RowSetWarning("Test");
|
|
107 |
}
|
|
108 |
} catch (RowSetWarning w) {
|
|
109 |
setWarning(w);
|
|
110 |
}
|
|
111 |
|
|
112 |
if (anyStaticFields) {
|
|
113 |
throw new SerialException("Located static fields in " +
|
|
114 |
"object instance. Cannot serialize");
|
|
115 |
}
|
|
116 |
|
|
117 |
this.obj = obj;
|
|
118 |
}
|
|
119 |
|
|
120 |
/**
|
|
121 |
* Returns an <code>Object</code> that is a copy of this <code>SerialJavaObject</code>
|
|
122 |
* object.
|
|
123 |
*
|
|
124 |
* @return a copy of this <code>SerialJavaObject</code> object as an
|
|
125 |
* <code>Object</code> in the Java programming language
|
|
126 |
* @throws SerialException if the instance is corrupt
|
|
127 |
*/
|
|
128 |
public Object getObject() throws SerialException {
|
|
129 |
return this.obj;
|
|
130 |
}
|
|
131 |
|
|
132 |
/**
|
|
133 |
* Returns an array of <code>Field</code> objects that contains each
|
|
134 |
* field of the object that this helper class is serializing.
|
|
135 |
*
|
|
136 |
* @return an array of <code>Field</code> objects
|
|
137 |
* @throws SerialException if an error is encountered accessing
|
|
138 |
* the serialized object
|
|
139 |
*/
|
|
140 |
public Field[] getFields() throws SerialException {
|
|
141 |
if (fields != null) {
|
|
142 |
Class c = this.obj.getClass();
|
|
143 |
//the following has to be commented before mustang integration
|
|
144 |
//return c.getFields();
|
|
145 |
//the following has to be uncommented before mustang integration
|
|
146 |
return sun.reflect.misc.FieldUtil.getFields(c);
|
|
147 |
} else {
|
|
148 |
throw new SerialException("SerialJavaObject does not contain" +
|
|
149 |
" a serialized object instance");
|
|
150 |
}
|
|
151 |
}
|
|
152 |
|
|
153 |
/**
|
|
154 |
* The identifier that assists in the serialization of this
|
|
155 |
* <code>SerialJavaObject</code> object.
|
|
156 |
*/
|
|
157 |
static final long serialVersionUID = -1465795139032831023L;
|
|
158 |
|
|
159 |
/**
|
|
160 |
* A container for the warnings issued on this <code>SerialJavaObject</code>
|
|
161 |
* object. When there are multiple warnings, each warning is chained to the
|
|
162 |
* previous warning.
|
|
163 |
*/
|
|
164 |
java.util.Vector chain;
|
|
165 |
|
|
166 |
/**
|
|
167 |
* Registers the given warning.
|
|
168 |
*/
|
|
169 |
private void setWarning(RowSetWarning e) {
|
|
170 |
if (chain == null) {
|
|
171 |
chain = new java.util.Vector();
|
|
172 |
}
|
|
173 |
chain.add(e);
|
|
174 |
}
|
|
175 |
}
|