001 /*
002 * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.sql.rowset.serial;
027
028 import java.sql.*;
029 import java.io.*;
030 import java.util.*;
031
032 /**
033 * A serialized mapping of a <code>Ref</code> object, which is the mapping in the
034 * Java programming language of an SQL <code>REF</code> value.
035 * <p>
036 * The <code>SerialRef</code> class provides a constructor for
037 * creating a <code>SerialRef</code> instance from a <code>Ref</code>
038 * object and provides methods for getting and setting the <code>Ref</code> object.
039 */
040 public class SerialRef implements Ref, Serializable, Cloneable {
041
042 /**
043 * String containing the base type name.
044 * @serial
045 */
046 private String baseTypeName;
047
048 /**
049 * This will store the type <code>Ref</code> as an <code>Object</code>.
050 */
051 private Object object;
052
053 /**
054 * Private copy of the Ref reference.
055 */
056 private Ref reference;
057
058 /**
059 * Constructs a <code>SerialRef</code> object from the given <code>Ref</code>
060 * object.
061 *
062 * @param ref a Ref object; cannot be <code>null</code>
063 * @throws SQLException if a database access occurs; if <code>ref</code>
064 * is <code>null</code>; or if the <code>Ref</code> object returns a
065 * <code>null</code> value base type name.
066 * @throws SerialException if an error occurs serializing the <code>Ref</code>
067 * object
068 */
069 public SerialRef(Ref ref) throws SerialException, SQLException {
070 if (ref == null) {
071 throw new SQLException(
072 "Cannot instantiate a SerialRef object "
073 + "with a null Ref object");
074 }
075 reference = ref;
076 object = ref;
077 if (ref.getBaseTypeName() == null) {
078 throw new SQLException(
079 "Cannot instantiate a SerialRef object "
080 + "that returns a null base type name");
081 } else {
082 baseTypeName = new String(ref.getBaseTypeName());
083 }
084 }
085
086 /**
087 * Returns a string describing the base type name of the <code>Ref</code>.
088 *
089 * @return a string of the base type name of the Ref
090 * @throws SerialException in no Ref object has been set
091 */
092 public String getBaseTypeName() throws SerialException {
093 return baseTypeName;
094 }
095
096 /**
097 * Returns an <code>Object</code> representing the SQL structured type
098 * to which this <code>SerialRef</code> object refers. The attributes
099 * of the structured type are mapped according to the given type map.
100 *
101 * @param map a <code>java.util.Map</code> object containing zero or
102 * more entries, with each entry consisting of 1) a <code>String</code>
103 * giving the fully qualified name of a UDT and 2) the
104 * <code>Class</code> object for the <code>SQLData</code> implementation
105 * that defines how the UDT is to be mapped
106 * @return an object instance resolved from the Ref reference and mapped
107 * according to the supplied type map
108 * @throws SerialException if an error is encountered in the reference
109 * resolution
110 */
111 public Object getObject(java.util.Map<String, Class<?>> map)
112 throws SerialException {
113 map = new Hashtable(map);
114 if (!object.equals(null)) {
115 return map.get(object);
116 } else {
117 throw new SerialException("The object is not set");
118 }
119 }
120
121 /**
122 * Returns an <code>Object</code> representing the SQL structured type
123 * to which this <code>SerialRef</code> object refers.
124 *
125 * @return an object instance resolved from the Ref reference
126 * @throws SerialException if an error is encountered in the reference
127 * resolution
128 */
129 public Object getObject() throws SerialException {
130
131 if (reference != null) {
132 try {
133 return reference.getObject();
134 } catch (SQLException e) {
135 throw new SerialException("SQLException: "
136 + e.getMessage());
137 }
138 }
139
140 if (object != null) {
141 return object;
142 }
143
144 throw new SerialException("The object is not set");
145
146 }
147
148 /**
149 * Sets the SQL structured type that this <code>SerialRef</code> object
150 * references to the given <code>Object</code> object.
151 *
152 * @param obj an <code>Object</code> representing the SQL structured type
153 * to be referenced
154 * @throws SerialException if an error is encountered generating the
155 * the structured type referenced by this <code>SerialRef</code> object
156 */
157 public void setObject(Object obj) throws SerialException {
158 try {
159 reference.setObject(obj);
160 } catch (SQLException e) {
161 throw new SerialException("SQLException: " + e.getMessage());
162 }
163 object = obj;
164 }
165
166 /**
167 * The identifier that assists in the serialization of this <code>SerialRef</code>
168 * object.
169 */
170 static final long serialVersionUID = -4727123500609662274L;
171
172 }
|