KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)SerialStruct.java 1.6 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 javax.sql.*;
12 import java.io.*;
13 import java.math.*;
14 import java.util.Map JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 import javax.sql.rowset.*;
18
19 /**
20  * A serialized mapping in the Java programming language of an SQL
21  * structured type. Each attribute that is not already serialized
22  * is mapped to a serialized form, and if an attribute is itself
23  * a structured type, each of its attributes that is not already
24  * serialized is mapped to a serialized form.
25  * <P>
26  * In addition, the structured type is custom mapped to a class in the
27  * Java programming language if there is such a mapping, as are
28  * its attributes, if appropriate.
29  * <P>
30  * The <code>SerialStruct</code> class provides a constructor for creating
31  * an instance from a <code>Struct</code> object, a method for retrieving
32  * the SQL type name of the SQL structured type in the database, and methods
33  * for retrieving its attribute values.
34  */

35 public class SerialStruct implements Struct, Serializable, Cloneable JavaDoc {
36
37     
38     /**
39      * The SQL type name for the structured type that this
40      * <code>SerialStruct</code> object represents. This is the name
41      * used in the SQL definition of the SQL structured type.
42      *
43      * @serial
44      */

45     private String JavaDoc SQLTypeName;
46     
47     /**
48      * An array of <code>Object</code> instances in which each
49      * element is an attribute of the SQL structured type that this
50      * <code>SerialStruct</code> object represents. The attributes are
51      * ordered according to their order in the definition of the
52      * SQL structured type.
53      *
54      * @serial
55      */

56     private Object JavaDoc attribs[];
57
58     /**
59      * Constructs a <code>SerialStruct</code> object from the given
60      * <code>Struct</code> object, using the given <code>java.util.Map</code>
61      * object for custom mapping the SQL structured type or any of its
62      * attributes that are SQL structured types.
63      *
64      * @param map a <code>java.util.Map</code> object in which
65      * each entry consists of 1) a <code>String</code> object
66      * giving the fully qualified name of a UDT and 2) the
67      * <code>Class</code> object for the <code>SQLData</code> implementation
68      * that defines how the UDT is to be mapped
69      * @throws SerialException if an error occurs
70      * @see java.sql.Struct
71      */

72      public SerialStruct(Struct in, Map JavaDoc<String JavaDoc,Class JavaDoc<?>> map)
73          throws SerialException JavaDoc
74      {
75
76     try {
77
78         // get the type name
79
SQLTypeName = new String JavaDoc(in.getSQLTypeName());
80         System.out.println("SQLTypeName: " + SQLTypeName);
81         
82         // get the attributes of the struct
83
attribs = in.getAttributes(map);
84
85         /*
86          * the array may contain further Structs
87          * and/or classes that have been mapped,
88          * other types that we have to serialize
89          */

90         mapToSerial(map);
91
92     } catch (SQLException e) {
93         throw new SerialException JavaDoc(e.getMessage());
94     }
95     }
96
97      /**
98       * Constructs a <code>SerialStruct</code> object from the
99       * given <code>SQLData</code> object, using the given type
100       * map to custom map it to a class in the Java programming
101       * language. The type map gives the SQL type and the class
102       * to which it is mapped. The <code>SQLData</code> object
103       * defines the class to which the SQL type will be mapped.
104       *
105       * @param in an instance of the <code>SQLData</code> class
106       * that defines the mapping of the SQL structured
107       * type to one or more objects in the Java programming language
108       * @param map a <code>java.util.Map</code> object in which
109       * each entry consists of 1) a <code>String</code> object
110       * giving the fully qualified name of a UDT and 2) the
111       * <code>Class</code> object for the <code>SQLData</code> implementation
112       * that defines how the UDT is to be mapped
113       * @throws SerialException if an error occurs
114       */

115     public SerialStruct(SQLData in, Map JavaDoc<String JavaDoc,Class JavaDoc<?>> map)
116         throws SerialException JavaDoc
117     {
118
119     try {
120
121         //set the type name
122
SQLTypeName = new String JavaDoc(in.getSQLTypeName());
123
124         Vector JavaDoc tmp = new Vector JavaDoc();
125         in.writeSQL(new SQLOutputImpl JavaDoc(tmp, map));
126         attribs = tmp.toArray();
127
128     } catch (SQLException e) {
129         throw new SerialException JavaDoc(e.getMessage());
130     }
131     }
132
133     
134     /**
135      * Retrieves the SQL type name for this <code>SerialStruct</code>
136      * object. This is the name used in the SQL definition of the
137      * structured type
138      *
139      * @return a <code>String</code> object representing the SQL
140      * type name for the SQL structured type that this
141      * <code>SerialStruct</code> object represents
142      * @throws SerialException if an error occurs
143      */

144     public String JavaDoc getSQLTypeName() throws SerialException JavaDoc {
145         return SQLTypeName;
146     }
147     
148     /**
149      * Retrieves an array of <code>Object</code> values containing the
150      * attributes of the SQL structured type that this
151      * <code>SerialStruct</code> object represents.
152      *
153      * @return an array of <code>Object</code> values, with each
154      * element being an attribute of the SQL structured type
155      * that this <code>SerialStruct</code> object represents
156      * @throws SerialException if an error occurs
157      */

158     public Object JavaDoc[] getAttributes() throws SerialException JavaDoc {
159         return attribs;
160     }
161         
162     /**
163      * Retrieves the attributes for the SQL structured type that
164      * this <code>SerialStruct</code> represents as an array of
165      * <code>Object</code> values, using the given type map for
166      * custom mapping if appropriate.
167      *
168      * @param map a <code>java.util.Map</code> object in which
169      * each entry consists of 1) a <code>String</code> object
170      * giving the fully qualified name of a UDT and 2) the
171      * <code>Class</code> object for the <code>SQLData</code> implementation
172      * that defines how the UDT is to be mapped
173      * @return an array of <code>Object</code> values, with each
174      * element being an attribute of the SQL structured
175      * type that this <code>SerialStruct</code> object
176      * represents
177      * @throws SerialException if an error occurs
178      */

179     public Object JavaDoc[] getAttributes(Map JavaDoc<String JavaDoc,Class JavaDoc<?>> map)
180         throws SerialException JavaDoc
181     {
182        return attribs;
183     }
184
185     
186     /**
187      * Maps attributes of an SQL structured type that are not
188      * serialized to a serialized form, using the given type map
189      * for custom mapping when appropriate. The following types
190      * in the Java programming language are mapped to their
191      * serialized forms: <code>Struct</code>, <code>SQLData</code>,
192      * <code>Ref</code>, <code>Blob</code>, <code>Clob</code>, and
193      * <code>Array</code>.
194      * <P>
195      * This method is called internally and is not used by an
196      * application programmer.
197      *
198      * @param map a <code>java.util.Map</code> object in which
199      * each entry consists of 1) a <code>String</code> object
200      * giving the fully qualified name of a UDT and 2) the
201      * <code>Class</code> object for the <code>SQLData</code> implementation
202      * that defines how the UDT is to be mapped
203      * @throws SerialException if an error occurs
204      */

205     private void mapToSerial(Map JavaDoc map) throws SerialException JavaDoc {
206
207     try {
208
209         for (int i = 0; i < attribs.length; i++) {
210             if (attribs[i] instanceof Struct) {
211                 attribs[i] = new SerialStruct JavaDoc((Struct)attribs[i], map);
212             } else if (attribs[i] instanceof SQLData) {
213                 attribs[i] = new SerialStruct JavaDoc((SQLData)attribs[i], map);
214             } else if (attribs[i] instanceof Blob) {
215                 attribs[i] = new SerialBlob JavaDoc((Blob)attribs[i]);
216             } else if (attribs[i] instanceof Clob) {
217                 attribs[i] = new SerialClob JavaDoc((Clob)attribs[i]);
218             } else if (attribs[i] instanceof Ref) {
219                 attribs[i] = new SerialRef JavaDoc((Ref)attribs[i]);
220             } else if (attribs[i] instanceof java.sql.Array JavaDoc) {
221                 attribs[i] = new SerialArray JavaDoc((java.sql.Array JavaDoc)attribs[i], map);
222             }
223         }
224
225     } catch (SQLException e) {
226         throw new SerialException JavaDoc(e.getMessage());
227     }
228         return;
229     }
230     
231     /**
232      * The identifier that assists in the serialization of this
233      * <code>SerialStruct</code> object.
234      */

235     static final long serialVersionUID = -8322445504027483372L;
236 }
237
Popular Tags