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