Path: blob/master/src/java.sql.rowset/share/classes/javax/sql/rowset/serial/SerialStruct.java
40948 views
/*1* Copyright (c) 2003, 2020, 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* <h2> Thread safety </h2>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* @since 1.560*/61public class SerialStruct implements Struct, Serializable, Cloneable {626364/**65* The SQL type name for the structured type that this66* <code>SerialStruct</code> object represents. This is the name67* used in the SQL definition of the SQL structured type.68*69* @serial70*/71private String SQLTypeName;7273/**74* An array of <code>Object</code> instances in which each75* element is an attribute of the SQL structured type that this76* <code>SerialStruct</code> object represents. The attributes are77* ordered according to their order in the definition of the78* SQL structured type.79*80* @serial81*/82@SuppressWarnings("serial") // Not statically typed as Serializable83private Object attribs[];8485/**86* Constructs a <code>SerialStruct</code> object from the given87* <code>Struct</code> object, using the given <code>java.util.Map</code>88* object for custom mapping the SQL structured type or any of its89* attributes that are SQL structured types.90*91* @param in an instance of {@code Struct}92* @param map a <code>java.util.Map</code> object in which93* each entry consists of 1) a <code>String</code> object94* giving the fully qualified name of a UDT and 2) the95* <code>Class</code> object for the <code>SQLData</code> implementation96* that defines how the UDT is to be mapped97* @throws SerialException if an error occurs98* @see java.sql.Struct99*/100public SerialStruct(Struct in, Map<String,Class<?>> map)101throws SerialException102{103104try {105106// get the type name107SQLTypeName = in.getSQLTypeName();108System.out.println("SQLTypeName: " + SQLTypeName);109110// get the attributes of the struct111attribs = in.getAttributes(map);112113/*114* the array may contain further Structs115* and/or classes that have been mapped,116* other types that we have to serialize117*/118mapToSerial(map);119120} catch (SQLException e) {121throw new SerialException(e.getMessage());122}123}124125/**126* Constructs a <code>SerialStruct</code> object from the127* given <code>SQLData</code> object, using the given type128* map to custom map it to a class in the Java programming129* language. The type map gives the SQL type and the class130* to which it is mapped. The <code>SQLData</code> object131* defines the class to which the SQL type will be mapped.132*133* @param in an instance of the <code>SQLData</code> class134* that defines the mapping of the SQL structured135* type to one or more objects in the Java programming language136* @param map a <code>java.util.Map</code> object in which137* each entry consists of 1) a <code>String</code> object138* giving the fully qualified name of a UDT and 2) the139* <code>Class</code> object for the <code>SQLData</code> implementation140* that defines how the UDT is to be mapped141* @throws SerialException if an error occurs142*/143public SerialStruct(SQLData in, Map<String,Class<?>> map)144throws SerialException145{146147try {148149//set the type name150SQLTypeName = in.getSQLTypeName();151152Vector<Object> tmp = new Vector<>();153in.writeSQL(new SQLOutputImpl(tmp, map));154attribs = tmp.toArray();155156} catch (SQLException e) {157throw new SerialException(e.getMessage());158}159}160161162/**163* Retrieves the SQL type name for this <code>SerialStruct</code>164* object. This is the name used in the SQL definition of the165* structured type166*167* @return a <code>String</code> object representing the SQL168* type name for the SQL structured type that this169* <code>SerialStruct</code> object represents170* @throws SerialException if an error occurs171*/172public String getSQLTypeName() throws SerialException {173return SQLTypeName;174}175176/**177* Retrieves an array of <code>Object</code> values containing the178* attributes of the SQL structured type that this179* <code>SerialStruct</code> object represents.180*181* @return an array of <code>Object</code> values, with each182* element being an attribute of the SQL structured type183* that this <code>SerialStruct</code> object represents184* @throws SerialException if an error occurs185*/186public Object[] getAttributes() throws SerialException {187Object[] val = this.attribs;188return (val == null) ? null : Arrays.copyOf(val, val.length);189}190191/**192* Retrieves the attributes for the SQL structured type that193* this <code>SerialStruct</code> represents as an array of194* <code>Object</code> values, using the given type map for195* custom mapping if appropriate.196*197* @param map a <code>java.util.Map</code> object in which198* each entry consists of 1) a <code>String</code> object199* giving the fully qualified name of a UDT and 2) the200* <code>Class</code> object for the <code>SQLData</code> implementation201* that defines how the UDT is to be mapped202* @return an array of <code>Object</code> values, with each203* element being an attribute of the SQL structured204* type that this <code>SerialStruct</code> object205* represents206* @throws SerialException if an error occurs207*/208public Object[] getAttributes(Map<String,Class<?>> map)209throws SerialException210{211Object[] val = this.attribs;212return (val == null) ? null : Arrays.copyOf(val, val.length);213}214215216/**217* Maps attributes of an SQL structured type that are not218* serialized to a serialized form, using the given type map219* for custom mapping when appropriate. The following types220* in the Java programming language are mapped to their221* serialized forms: <code>Struct</code>, <code>SQLData</code>,222* <code>Ref</code>, <code>Blob</code>, <code>Clob</code>, and223* <code>Array</code>.224* <P>225* This method is called internally and is not used by an226* application programmer.227*228* @param map a <code>java.util.Map</code> object in which229* each entry consists of 1) a <code>String</code> object230* giving the fully qualified name of a UDT and 2) the231* <code>Class</code> object for the <code>SQLData</code> implementation232* that defines how the UDT is to be mapped233* @throws SerialException if an error occurs234*/235private void mapToSerial(Map<String,Class<?>> map) throws SerialException {236237try {238239for (int i = 0; i < attribs.length; i++) {240if (attribs[i] instanceof Struct) {241attribs[i] = new SerialStruct((Struct)attribs[i], map);242} else if (attribs[i] instanceof SQLData) {243attribs[i] = new SerialStruct((SQLData)attribs[i], map);244} else if (attribs[i] instanceof Blob) {245attribs[i] = new SerialBlob((Blob)attribs[i]);246} else if (attribs[i] instanceof Clob) {247attribs[i] = new SerialClob((Clob)attribs[i]);248} else if (attribs[i] instanceof Ref) {249attribs[i] = new SerialRef((Ref)attribs[i]);250} else if (attribs[i] instanceof java.sql.Array) {251attribs[i] = new SerialArray((java.sql.Array)attribs[i], map);252}253}254255} catch (SQLException e) {256throw new SerialException(e.getMessage());257}258return;259}260261/**262* Compares this SerialStruct to the specified object. The result is263* {@code true} if and only if the argument is not {@code null} and is a264* {@code SerialStruct} object whose attributes are identical to this265* object's attributes266*267* @param obj The object to compare this {@code SerialStruct} against268*269* @return {@code true} if the given object represents a {@code SerialStruct}270* equivalent to this SerialStruct, {@code false} otherwise271*272*/273public boolean equals(Object obj) {274if (this == obj) {275return true;276}277if (obj instanceof SerialStruct) {278SerialStruct ss = (SerialStruct)obj;279return SQLTypeName.equals(ss.SQLTypeName) &&280Arrays.equals(attribs, ss.attribs);281}282return false;283}284285/**286* Returns a hash code for this {@code SerialStruct}. The hash code for a287* {@code SerialStruct} object is computed using the hash codes288* of the attributes of the {@code SerialStruct} object and its289* {@code SQLTypeName}290*291* @return a hash code value for this object.292*/293public int hashCode() {294return ((31 + Arrays.hashCode(attribs)) * 31) * 31295+ SQLTypeName.hashCode();296}297298/**299* Returns a clone of this {@code SerialStruct}. The copy will contain a300* reference to a clone of the underlying attribs array, not a reference301* to the original underlying attribs array of this {@code SerialStruct} object.302*303* @return a clone of this SerialStruct304*/305public Object clone() {306try {307SerialStruct ss = (SerialStruct) super.clone();308ss.attribs = Arrays.copyOf(attribs, attribs.length);309return ss;310} catch (CloneNotSupportedException ex) {311// this shouldn't happen, since we are Cloneable312throw new InternalError();313}314315}316317/**318* readObject is called to restore the state of the {@code SerialStruct} from319* a stream.320* @param s the {@code ObjectInputStream} to read from.321*322* @throws ClassNotFoundException if the class of a serialized object323* could not be found.324* @throws IOException if an I/O error occurs.325*/326private void readObject(ObjectInputStream s)327throws IOException, ClassNotFoundException {328329ObjectInputStream.GetField fields = s.readFields();330Object[] tmp = (Object[])fields.get("attribs", null);331attribs = tmp == null ? null : tmp.clone();332SQLTypeName = (String)fields.get("SQLTypeName", null);333}334335/**336* writeObject is called to save the state of the {@code SerialStruct}337* to a stream.338* @param s the {@code ObjectOutputStream} to write to.339* @throws IOException if an I/O error occurs.340*/341private void writeObject(ObjectOutputStream s)342throws IOException {343344ObjectOutputStream.PutField fields = s.putFields();345fields.put("attribs", attribs);346fields.put("SQLTypeName", SQLTypeName);347s.writeFields();348}349350/**351* The identifier that assists in the serialization of this352* <code>SerialStruct</code> object.353*/354static final long serialVersionUID = -8322445504027483372L;355}356357358