KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > sql > rowset > serial > SerialRef


1 /*
2  * @(#)SerialRef.java 1.7 04/05/29
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.sql.rowset.serial;
9
10 import java.sql.*;
11 import java.io.*;
12 import java.util.*;
13
14 /**
15  * A serialized mapping of a <code>Ref</code> object, which is the mapping in the
16  * Java programming language of an SQL <code>REF</code> value.
17  * <p>
18  * The <code>SerialRef</code> class provides a constructor for
19  * creating a <code>SerialRef</code> instance from a <code>Ref</code>
20  * object and provides methods for getting and setting the <code>Ref</code> object.
21  */

22 public class SerialRef implements Ref, Serializable, Cloneable JavaDoc {
23
24     /**
25      * String containing the base type name.
26      * @serial
27      */

28     private String JavaDoc baseTypeName;
29     
30     /**
31      * This will store the type <code>Ref</code> as an <code>Object</code>.
32      */

33     private Object JavaDoc object;
34
35     /**
36      * Private copy of the Ref reference.
37      */

38     private Ref reference;
39     
40     /**
41      * Constructs a <code>SerialRef</code> object from the given <code>Ref</code>
42      * object.
43      *
44      * @param ref a Ref object; cannot be <code>null</code>
45      * @throws SQLException if a database access occurs; if <code>ref</code>
46      * is <code>null</code>; or if the <code>Ref</code> object returns a
47      * <code>null</code> value base type name.
48      * @throws SerialException if an error occurs serializing the <code>Ref</code>
49      * object
50      */

51     public SerialRef(Ref ref) throws SerialException JavaDoc, SQLException {
52         if (ref == null) {
53             throw new SQLException("Cannot instantiate a SerialRef object " +
54                 "with a null Ref object");
55         }
56         reference = ref;
57         object = ref;
58         if (ref.getBaseTypeName() == null) {
59             throw new SQLException("Cannot instantiate a SerialRef object " +
60                 "that returns a null base type name");
61         } else {
62             baseTypeName = new String JavaDoc(ref.getBaseTypeName());
63         }
64     }
65
66     /**
67      * Returns a string describing the base type name of the <code>Ref</code>.
68      *
69      * @return a string of the base type name of the Ref
70      * @throws SerialException in no Ref object has been set
71      */

72     public String JavaDoc getBaseTypeName() throws SerialException JavaDoc {
73         return baseTypeName;
74     }
75
76     /**
77      * Returns an <code>Object</code> representing the SQL structured type
78      * to which this <code>SerialRef</code> object refers. The attributes
79      * of the structured type are mapped according to the given type map.
80      *
81      * @param map a <code>java.util.Map</code> object containing zero or
82      * more entries, with each entry consisting of 1) a <code>String</code>
83      * giving the fully qualified name of a UDT and 2) the
84      * <code>Class</code> object for the <code>SQLData</code> implementation
85      * that defines how the UDT is to be mapped
86      * @return an object instance resolved from the Ref reference and mapped
87      * according to the supplied type map
88      * @throws SerialException if an error is encountered in the reference
89      * resolution
90      */

91     public Object JavaDoc getObject(java.util.Map JavaDoc<String JavaDoc,Class JavaDoc<?>> map)
92         throws SerialException JavaDoc
93     {
94         map = new Hashtable(map);
95         if (!object.equals(null)) {
96             return map.get(object);
97         } else {
98             throw new SerialException JavaDoc("The object is not set");
99         }
100     }
101    
102     /**
103      * Returns an <code>Object</code> representing the SQL structured type
104      * to which this <code>SerialRef</code> object refers.
105      *
106      * @return an object instance resolved from the Ref reference
107      * @throws SerialException if an error is encountered in the reference
108      * resolution
109      */

110     public Object JavaDoc getObject() throws SerialException JavaDoc {
111         
112         if (reference != null) {
113             try {
114                 return reference.getObject();
115             } catch (SQLException e) {
116                 throw new SerialException JavaDoc("SQLException: " + e.getMessage());
117             }
118         }
119                 
120         if (object != null) {
121             return object;
122         }
123     
124         
125         throw new SerialException JavaDoc("The object is not set");
126         
127     }
128    
129     /**
130      * Sets the SQL structured type that this <code>SerialRef</code> object
131      * references to the given <code>Object</code> object.
132      *
133      * @param obj an <code>Object</code> representing the SQL structured type
134      * to be referenced
135      * @throws SerialException if an error is encountered generating the
136      * the structured type referenced by this <code>SerialRef</code> object
137      */

138     public void setObject(Object JavaDoc obj) throws SerialException JavaDoc {
139         try {
140             reference.setObject(obj);
141         } catch (SQLException e) {
142             throw new SerialException JavaDoc("SQLException: " + e.getMessage());
143         }
144         object = obj;
145     }
146     
147     /**
148      * The identifier that assists in the serialization of this <code>SerialRef</code>
149      * object.
150      */

151     static final long serialVersionUID = -4727123500609662274L;
152     
153     
154 }
155
156
157
158
Popular Tags