001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.sql.rowset.serial;
019:
020: import java.io.Serializable;
021: import java.sql.SQLData;
022: import java.sql.SQLException;
023: import java.sql.SQLOutput;
024: import java.sql.Struct;
025: import java.util.Map;
026: import java.util.Vector;
027:
028: /**
029: * A struct class for serialization.
030: */
031: public class SerialStruct implements Struct, Serializable, Cloneable {
032: // required by serialized form
033: @SuppressWarnings("unused")
034: private static final long serialVersionUID = -8322445504027483372L;
035:
036: // required by serialized form
037: private String SQLTypeName;
038:
039: // required by serialized form
040: private Object[] attribs;
041:
042: /**
043: * Constructs this serializable struct from an instance of SQLData. Use the
044: * mapping defined in the map for UDT.
045: *
046: * @param in
047: * an instance of SQLData.
048: * @param map
049: * an user defined mapping for UDT.
050: * @throws SerialException
051: * if there is something wrong.
052: */
053: public SerialStruct(SQLData in, Map<String, Class<?>> map)
054: throws SerialException {
055: try {
056: SQLTypeName = in.getSQLTypeName();
057: } catch (SQLException e) {
058: throw new SerialException(e.getMessage());
059: }
060: Vector v = new Vector();
061: try {
062: SQLOutput out = new SQLOutputImpl(v, map);
063: in.writeSQL(out);
064: } catch (SQLException e) {
065: throw new SerialException(e.getMessage());
066: }
067: attribs = v.toArray();
068: }
069:
070: /**
071: * Constructs this serializable struct from an instance of Struct. Use the
072: * mapping defined in the map for UDT.
073: *
074: * @param in
075: * an instance of SQLData.
076: * @param map
077: * an user defined mapping for UDT.
078: * @throws SerialException
079: * if there is something wrong.
080: */
081: public SerialStruct(Struct in, Map<String, Class<?>> map)
082: throws SerialException {
083: try {
084: SQLTypeName = in.getSQLTypeName();
085: attribs = in.getAttributes(map);
086: } catch (SQLException e) {
087: throw new SerialException(e.getMessage());
088: }
089: }
090:
091: /**
092: * Returns all the attributes as an array of Object.
093: */
094: public Object[] getAttributes() throws SerialException {
095: return attribs;
096: }
097:
098: /**
099: * Returns all the attributes as an array of Object, with the mapping
100: * defined by user.
101: */
102: public Object[] getAttributes(Map<String, Class<?>> map)
103: throws SerialException {
104: // TODO handle the map.
105: return attribs;
106: }
107:
108: public String getSQLTypeName() throws SerialException {
109: return SQLTypeName;
110: }
111:
112: }
|