1 /* 2 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.sql.rowset.serial; 27 28 import java.io.*; 29 import java.lang.reflect.*; 30 import javax.sql.rowset.RowSetWarning; 31 32 /** 33 * A serializable mapping in the Java programming language of an SQL 34 * <code>JAVA_OBJECT</code> value. Assuming the Java object 35 * implements the <code>Serializable</code> interface, this class simply wraps the 36 * serialization process. 37 * <P> 38 * If however, the serialization is not possible because 39 * the Java object is not immediately serializable, this class will 40 * attempt to serialize all non-static members to permit the object 41 * state to be serialized. 42 * Static or transient fields cannot be serialized; an attempt to serialize 43 * them will result in a <code>SerialException</code> object being thrown. 44 * 45 * @author Jonathan Bruce 46 */ 47 public class SerialJavaObject implements Serializable, Cloneable { 48 49 /** 50 * Placeholder for object to be serialized. 51 */ 52 private final Object obj; 53 54 55 /** 56 * Placeholder for all fields in the <code>JavaObject</code> being serialized. 57 */ 58 private transient Field[] fields; 59 60 /** 61 * Constructor for <code>SerialJavaObject</code> helper class. 62 * <p> 63 * 64 * @param obj the Java <code>Object</code> to be serialized 65 * @throws SerialException if the object is found not to be serializable 66 */ 67 public SerialJavaObject(Object obj) throws SerialException { 68 69 // if any static fields are found, an exception 70 // should be thrown 71 72 73 // get Class. Object instance should always be available 74 Class<?> c = obj.getClass(); 75 76 // determine if object implements Serializable i/f 77 if (!(obj instanceof java.io.Serializable)) { 78 setWarning(new RowSetWarning("Warning, the object passed to the constructor does not implement Serializable")); 79 } 80 81 // can only determine public fields (obviously). If 82 // any of these are static, this should invalidate 83 // the action of attempting to persist these fields 84 // in a serialized form 85 86 boolean anyStaticFields = false; 87 fields = c.getFields(); 88 89 for (int i = 0; i < fields.length; i++ ) { 90 if ( fields[i].getModifiers() == Modifier.STATIC ) { 91 anyStaticFields = true; 92 } 93 } 94 95 96 if (anyStaticFields) { 97 throw new SerialException("Located static fields in " + 98 "object instance. Cannot serialize"); 99 } 100 101 this.obj = obj; 102 } 103 104 /** 105 * Returns an <code>Object</code> that is a copy of this <code>SerialJavaObject</code> 106 * object. 107 * 108 * @return a copy of this <code>SerialJavaObject</code> object as an 109 * <code>Object</code> in the Java programming language 110 * @throws SerialException if the instance is corrupt 111 */ 112 public Object getObject() throws SerialException { 113 return this.obj; 114 } 115 116 /** 117 * Returns an array of <code>Field</code> objects that contains each 118 * field of the object that this helper class is serializing. 119 * 120 * @return an array of <code>Field</code> objects 121 * @throws SerialException if an error is encountered accessing 122 * the serialized object 123 */ 124 public Field[] getFields() throws SerialException { 125 if (fields != null) { 126 Class<?> c = this.obj.getClass(); 127 return c.getFields(); 128 } else { 129 throw new SerialException("SerialJavaObject does not contain" + 130 " a serialized object instance"); 131 } 132 } 133 134 /** 135 * The identifier that assists in the serialization of this 136 * <code>SerialJavaObject</code> object. 137 */ 138 static final long serialVersionUID = -1465795139032831023L; 139 140 /** 141 * A container for the warnings issued on this <code>SerialJavaObject</code> 142 * object. When there are multiple warnings, each warning is chained to the 143 * previous warning. 144 */ 145 java.util.Vector<RowSetWarning> chain; 146 147 /** 148 * Registers the given warning. 149 */ 150 private void setWarning(RowSetWarning e) { 151 if (chain == null) { 152 chain = new java.util.Vector<RowSetWarning>(); 153 } 154 chain.add(e); 155 } 156 }