Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/sql/rowset/serial/SerialStruct.java
38918 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sql.rowset.serial;2627import java.sql.*;28import javax.sql.*;29import java.io.*;30import java.math.*;31import java.util.Arrays;32import java.util.Map;33import java.util.Vector;3435import javax.sql.rowset.*;3637/**38* A serialized mapping in the Java programming language of an SQL39* structured type. Each attribute that is not already serialized40* is mapped to a serialized form, and if an attribute is itself41* a structured type, each of its attributes that is not already42* serialized is mapped to a serialized form.43* <P>44* In addition, the structured type is custom mapped to a class in the45* Java programming language if there is such a mapping, as are46* its attributes, if appropriate.47* <P>48* The <code>SerialStruct</code> class provides a constructor for creating49* an instance from a <code>Struct</code> object, a method for retrieving50* the SQL type name of the SQL structured type in the database, and methods51* for retrieving its attribute values.52*53* <h3> Thread safety </h3>54*55* A SerialStruct is not safe for use by multiple concurrent threads. If a56* SerialStruct is to be used by more than one thread then access to the57* SerialStruct should be controlled by appropriate synchronization.58*59*/60public class SerialStruct implements Struct, Serializable, Cloneable {616263/**64* The SQL type name for the structured type that this65* <code>SerialStruct</code> object represents. This is the name66* used in the SQL definition of the SQL structured type.67*68* @serial69*/70private String SQLTypeName;7172/**73* An array of <code>Object</code> instances in which each74* element is an attribute of the SQL structured type that this75* <code>SerialStruct</code> object represents. The attributes are76* ordered according to their order in the definition of the77* SQL structured type.78*79* @serial80*/81private Object attribs[];8283/**84* Constructs a <code>SerialStruct</code> object from the given85* <code>Struct</code> object, using the given <code>java.util.Map</code>86* object for custom mapping the SQL structured type or any of its87* attributes that are SQL structured types.88*89* @param in an instance of {@code Struct}90* @param map a <code>java.util.Map</code> object in which91* each entry consists of 1) a <code>String</code> object92* giving the fully qualified name of a UDT and 2) the93* <code>Class</code> object for the <code>SQLData</code> implementation94* that defines how the UDT is to be mapped95* @throws SerialException if an error occurs96* @see java.sql.Struct97*/98public SerialStruct(Struct in, Map<String,Class<?>> map)99throws SerialException100{101102try {103104// get the type name105SQLTypeName = in.getSQLTypeName();106System.out.println("SQLTypeName: " + SQLTypeName);107108// get the attributes of the struct109attribs = in.getAttributes(map);110111/*112* the array may contain further Structs113* and/or classes that have been mapped,114* other types that we have to serialize115*/116mapToSerial(map);117118} catch (SQLException e) {119throw new SerialException(e.getMessage());120}121}122123/**124* Constructs a <code>SerialStruct</code> object from the125* given <code>SQLData</code> object, using the given type126* map to custom map it to a class in the Java programming127* language. The type map gives the SQL type and the class128* to which it is mapped. The <code>SQLData</code> object129* defines the class to which the SQL type will be mapped.130*131* @param in an instance of the <code>SQLData</code> class132* that defines the mapping of the SQL structured133* type to one or more objects in the Java programming language134* @param map a <code>java.util.Map</code> object in which135* each entry consists of 1) a <code>String</code> object136* giving the fully qualified name of a UDT and 2) the137* <code>Class</code> object for the <code>SQLData</code> implementation138* that defines how the UDT is to be mapped139* @throws SerialException if an error occurs140*/141public SerialStruct(SQLData in, Map<String,Class<?>> map)142throws SerialException143{144145try {146147//set the type name148SQLTypeName = in.getSQLTypeName();149150Vector<Object> tmp = new Vector<>();151in.writeSQL(new SQLOutputImpl(tmp, map));152attribs = tmp.toArray();153154} catch (SQLException e) {155throw new SerialException(e.getMessage());156}157}158159160/**161* Retrieves the SQL type name for this <code>SerialStruct</code>162* object. This is the name used in the SQL definition of the163* structured type164*165* @return a <code>String</code> object representing the SQL166* type name for the SQL structured type that this167* <code>SerialStruct</code> object represents168* @throws SerialException if an error occurs169*/170public String getSQLTypeName() throws SerialException {171return SQLTypeName;172}173174/**175* Retrieves an array of <code>Object</code> values containing the176* attributes of the SQL structured type that this177* <code>SerialStruct</code> object represents.178*179* @return an array of <code>Object</code> values, with each180* element being an attribute of the SQL structured type181* that this <code>SerialStruct</code> object represents182* @throws SerialException if an error occurs183*/184public Object[] getAttributes() throws SerialException {185Object[] val = this.attribs;186return (val == null) ? null : Arrays.copyOf(val, val.length);187}188189/**190* Retrieves the attributes for the SQL structured type that191* this <code>SerialStruct</code> represents as an array of192* <code>Object</code> values, using the given type map for193* custom mapping if appropriate.194*195* @param map a <code>java.util.Map</code> object in which196* each entry consists of 1) a <code>String</code> object197* giving the fully qualified name of a UDT and 2) the198* <code>Class</code> object for the <code>SQLData</code> implementation199* that defines how the UDT is to be mapped200* @return an array of <code>Object</code> values, with each201* element being an attribute of the SQL structured202* type that this <code>SerialStruct</code> object203* represents204* @throws SerialException if an error occurs205*/206public Object[] getAttributes(Map<String,Class<?>> map)207throws SerialException208{209Object[] val = this.attribs;210return (val == null) ? null : Arrays.copyOf(val, val.length);211}212213214/**215* Maps attributes of an SQL structured type that are not216* serialized to a serialized form, using the given type map217* for custom mapping when appropriate. The following types218* in the Java programming language are mapped to their219* serialized forms: <code>Struct</code>, <code>SQLData</code>,220* <code>Ref</code>, <code>Blob</code>, <code>Clob</code>, and221* <code>Array</code>.222* <P>223* This method is called internally and is not used by an224* application programmer.225*226* @param map a <code>java.util.Map</code> object in which227* each entry consists of 1) a <code>String</code> object228* giving the fully qualified name of a UDT and 2) the229* <code>Class</code> object for the <code>SQLData</code> implementation230* that defines how the UDT is to be mapped231* @throws SerialException if an error occurs232*/233private void mapToSerial(Map<String,Class<?>> map) throws SerialException {234235try {236237for (int i = 0; i < attribs.length; i++) {238if (attribs[i] instanceof Struct) {239attribs[i] = new SerialStruct((Struct)attribs[i], map);240} else if (attribs[i] instanceof SQLData) {241attribs[i] = new SerialStruct((SQLData)attribs[i], map);242} else if (attribs[i] instanceof Blob) {243attribs[i] = new SerialBlob((Blob)attribs[i]);244} else if (attribs[i] instanceof Clob) {245attribs[i] = new SerialClob((Clob)attribs[i]);246} else if (attribs[i] instanceof Ref) {247attribs[i] = new SerialRef((Ref)attribs[i]);248} else if (attribs[i] instanceof java.sql.Array) {249attribs[i] = new SerialArray((java.sql.Array)attribs[i], map);250}251}252253} catch (SQLException e) {254throw new SerialException(e.getMessage());255}256return;257}258259/**260* Compares this SerialStruct to the specified object. The result is261* {@code true} if and only if the argument is not {@code null} and is a262* {@code SerialStruct} object whose attributes are identical to this263* object's attributes264*265* @param obj The object to compare this {@code SerialStruct} against266*267* @return {@code true} if the given object represents a {@code SerialStruct}268* equivalent to this SerialStruct, {@code false} otherwise269*270*/271public boolean equals(Object obj) {272if (this == obj) {273return true;274}275if (obj instanceof SerialStruct) {276SerialStruct ss = (SerialStruct)obj;277return SQLTypeName.equals(ss.SQLTypeName) &&278Arrays.equals(attribs, ss.attribs);279}280return false;281}282283/**284* Returns a hash code for this {@code SerialStruct}. The hash code for a285* {@code SerialStruct} object is computed using the hash codes286* of the attributes of the {@code SerialStruct} object and its287* {@code SQLTypeName}288*289* @return a hash code value for this object.290*/291public int hashCode() {292return ((31 + Arrays.hashCode(attribs)) * 31) * 31293+ SQLTypeName.hashCode();294}295296/**297* Returns a clone of this {@code SerialStruct}. The copy will contain a298* reference to a clone of the underlying attribs array, not a reference299* to the original underlying attribs array of this {@code SerialStruct} object.300*301* @return a clone of this SerialStruct302*/303public Object clone() {304try {305SerialStruct ss = (SerialStruct) super.clone();306ss.attribs = Arrays.copyOf(attribs, attribs.length);307return ss;308} catch (CloneNotSupportedException ex) {309// this shouldn't happen, since we are Cloneable310throw new InternalError();311}312313}314315/**316* readObject is called to restore the state of the {@code SerialStruct} from317* a stream.318*/319private void readObject(ObjectInputStream s)320throws IOException, ClassNotFoundException {321322ObjectInputStream.GetField fields = s.readFields();323Object[] tmp = (Object[])fields.get("attribs", null);324attribs = tmp == null ? null : tmp.clone();325SQLTypeName = (String)fields.get("SQLTypeName", null);326}327328/**329* writeObject is called to save the state of the {@code SerialStruct}330* to a stream.331*/332private void writeObject(ObjectOutputStream s)333throws IOException, ClassNotFoundException {334335ObjectOutputStream.PutField fields = s.putFields();336fields.put("attribs", attribs);337fields.put("SQLTypeName", SQLTypeName);338s.writeFields();339}340341/**342* The identifier that assists in the serialization of this343* <code>SerialStruct</code> object.344*/345static final long serialVersionUID = -8322445504027483372L;346}347348349