View Javadoc
1   /*
2    * Copyright (c) 2003, 2013, 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.sql.*;
29  import java.io.*;
30  import java.util.*;
31  
32  /**
33   * A serialized mapping of a <code>Ref</code> object, which is the mapping in the
34   * Java programming language of an SQL <code>REF</code> value.
35   * <p>
36   * The <code>SerialRef</code> class provides a constructor  for
37   * creating a <code>SerialRef</code> instance from a <code>Ref</code>
38   * object and provides methods for getting and setting the <code>Ref</code> object.
39   *
40   * <h3> Thread safety </h3>
41   *
42   * A SerialRef is not safe for use by multiple concurrent threads.  If a
43   * SerialRef is to be used by more than one thread then access to the SerialRef
44   * should be controlled by appropriate synchronization.
45   *
46   */
47  public class SerialRef implements Ref, Serializable, Cloneable {
48  
49      /**
50       * String containing the base type name.
51       * @serial
52       */
53      private String baseTypeName;
54  
55      /**
56       * This will store the type <code>Ref</code> as an <code>Object</code>.
57       */
58      private Object object;
59  
60      /**
61       * Private copy of the Ref reference.
62       */
63      private Ref reference;
64  
65      /**
66       * Constructs a <code>SerialRef</code> object from the given <code>Ref</code>
67       * object.
68       *
69       * @param ref a Ref object; cannot be <code>null</code>
70       * @throws SQLException if a database access occurs; if <code>ref</code>
71       *     is <code>null</code>; or if the <code>Ref</code> object returns a
72       *     <code>null</code> value base type name.
73       * @throws SerialException if an error occurs serializing the <code>Ref</code>
74       *     object
75       */
76      public SerialRef(Ref ref) throws SerialException, SQLException {
77          if (ref == null) {
78              throw new SQLException("Cannot instantiate a SerialRef object " +
79                  "with a null Ref object");
80          }
81          reference = ref;
82          object = ref;
83          if (ref.getBaseTypeName() == null) {
84              throw new SQLException("Cannot instantiate a SerialRef object " +
85                  "that returns a null base type name");
86          } else {
87              baseTypeName = ref.getBaseTypeName();
88          }
89      }
90  
91      /**
92       * Returns a string describing the base type name of the <code>Ref</code>.
93       *
94       * @return a string of the base type name of the Ref
95       * @throws SerialException in no Ref object has been set
96       */
97      public String getBaseTypeName() throws SerialException {
98          return baseTypeName;
99      }
100 
101     /**
102      * Returns an <code>Object</code> representing the SQL structured type
103      * to which this <code>SerialRef</code> object refers.  The attributes
104      * of the structured type are mapped according to the given type map.
105      *
106      * @param map a <code>java.util.Map</code> object containing zero or
107      *        more entries, with each entry consisting of 1) a <code>String</code>
108      *        giving the fully qualified name of a UDT and 2) the
109      *        <code>Class</code> object for the <code>SQLData</code> implementation
110      *        that defines how the UDT is to be mapped
111      * @return an object instance resolved from the Ref reference and mapped
112      *        according to the supplied type map
113      * @throws SerialException if an error is encountered in the reference
114      *        resolution
115      */
116     public Object getObject(java.util.Map<String,Class<?>> map)
117         throws SerialException
118     {
119         map = new Hashtable<String, Class<?>>(map);
120         if (object != null) {
121             return map.get(object);
122         } else {
123             throw new SerialException("The object is not set");
124         }
125     }
126 
127     /**
128      * Returns an <code>Object</code> representing the SQL structured type
129      * to which this <code>SerialRef</code> object refers.
130      *
131      * @return an object instance resolved from the Ref reference
132      * @throws SerialException if an error is encountered in the reference
133      *         resolution
134      */
135     public Object getObject() throws SerialException {
136 
137         if (reference != null) {
138             try {
139                 return reference.getObject();
140             } catch (SQLException e) {
141                 throw new SerialException("SQLException: " + e.getMessage());
142             }
143         }
144 
145         if (object != null) {
146             return object;
147         }
148 
149 
150         throw new SerialException("The object is not set");
151 
152     }
153 
154     /**
155      * Sets the SQL structured type that this <code>SerialRef</code> object
156      * references to the given <code>Object</code> object.
157      *
158      * @param obj an <code>Object</code> representing the SQL structured type
159      *        to be referenced
160      * @throws SerialException if an error is encountered generating the
161      * the structured type referenced by this <code>SerialRef</code> object
162      */
163     public void setObject(Object obj) throws SerialException {
164         try {
165             reference.setObject(obj);
166         } catch (SQLException e) {
167             throw new SerialException("SQLException: " + e.getMessage());
168         }
169         object = obj;
170     }
171 
172     /**
173      * Compares this SerialRef to the specified object.  The result is {@code
174      * true} if and only if the argument is not {@code null} and is a {@code
175      * SerialRef} object that represents the same object as this
176      * object.
177      *
178      * @param  obj The object to compare this {@code SerialRef} against
179      *
180      * @return  {@code true} if the given object represents a {@code SerialRef}
181      *          equivalent to this SerialRef, {@code false} otherwise
182      *
183      */
184     public boolean equals(Object obj) {
185         if (this == obj) {
186             return true;
187         }
188         if(obj instanceof SerialRef) {
189             SerialRef ref = (SerialRef)obj;
190             return baseTypeName.equals(ref.baseTypeName) &&
191                     object.equals(ref.object);
192         }
193         return false;
194     }
195 
196     /**
197      * Returns a hash code for this {@code SerialRef}.
198      * @return  a hash code value for this object.
199      */
200     public int hashCode() {
201         return (31 + object.hashCode()) * 31 + baseTypeName.hashCode();
202     }
203 
204     /**
205      * Returns a clone of this {@code SerialRef}.
206      * The underlying {@code Ref} object will be set to null.
207      *
208      * @return  a clone of this SerialRef
209      */
210     public Object clone() {
211         try {
212             SerialRef ref = (SerialRef) super.clone();
213             ref.reference = null;
214             return ref;
215         } catch (CloneNotSupportedException ex) {
216             // this shouldn't happen, since we are Cloneable
217             throw new InternalError();
218         }
219 
220     }
221 
222     /**
223      * readObject is called to restore the state of the SerialRef from
224      * a stream.
225      */
226     private void readObject(ObjectInputStream s)
227             throws IOException, ClassNotFoundException {
228         ObjectInputStream.GetField fields = s.readFields();
229         object = fields.get("object", null);
230         baseTypeName = (String) fields.get("baseTypeName", null);
231         reference = (Ref) fields.get("reference", null);
232     }
233 
234     /**
235      * writeObject is called to save the state of the SerialRef
236      * to a stream.
237      */
238     private void writeObject(ObjectOutputStream s)
239             throws IOException, ClassNotFoundException {
240 
241         ObjectOutputStream.PutField fields = s.putFields();
242         fields.put("baseTypeName", baseTypeName);
243         fields.put("object", object);
244         // Note: this check to see if it is an instance of Serializable
245         // is for backwards compatibiity
246         fields.put("reference", reference instanceof Serializable ? reference : null);
247         s.writeFields();
248     }
249 
250     /**
251      * The identifier that assists in the serialization of this <code>SerialRef</code>
252      * object.
253      */
254     static final long serialVersionUID = -4727123500609662274L;
255 
256 
257 }