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. package javax.sql.rowset.serial;
  8. import java.sql.*;
  9. import java.io.*;
  10. import java.util.*;
  11. /**
  12. * A serialized mapping of a <code>Ref</code> object, which is the mapping in the
  13. * Java programming language of an SQL <code>REF</code> value.
  14. * <p>
  15. * The <code>SerialRef</code> class provides a constructor for
  16. * creating a <code>SerialRef</code> instance from a <code>Ref</code>
  17. * object and provides methods for getting and setting the <code>Ref</code> object.
  18. */
  19. public class SerialRef implements Ref, Serializable, Cloneable {
  20. /**
  21. * String containing the base type name.
  22. * @serial
  23. */
  24. private String baseTypeName;
  25. /**
  26. * This will store the type <code>Ref</code> as an <code>Object</code>.
  27. */
  28. private Object object;
  29. /**
  30. * Private copy of the Ref reference.
  31. */
  32. private Ref reference;
  33. /**
  34. * Constructs a <code>SerialRef</code> object from the given <code>Ref</code>
  35. * object.
  36. *
  37. * @param ref a Ref object; cannot be <code>null</code>
  38. * @throws SQLException if a database access occurs; if <code>ref</code>
  39. * is <code>null</code> or if the <code>Ref</code> object returns a
  40. * <code>null</code> value base type name.
  41. * @throws SerialException if an error occurs serializing the <code>Ref</code>
  42. * object
  43. */
  44. public SerialRef(Ref ref) throws SerialException, SQLException {
  45. if (ref == null) {
  46. throw new SQLException("Cannot instantiate a SerialRef object " +
  47. "with a null Ref object");
  48. }
  49. reference = ref;
  50. object = ref;
  51. if (ref.getBaseTypeName() == null) {
  52. throw new SQLException("Cannot instantiate a SerialRef object " +
  53. "that returns a null base type name");
  54. } else {
  55. baseTypeName = new String(ref.getBaseTypeName());
  56. }
  57. }
  58. /**
  59. * Returns a string describing the base type name of the <code>Ref</code>.
  60. *
  61. * @return a string of the base type name of the Ref
  62. * @throws SerialException in no Ref object has been set
  63. */
  64. public String getBaseTypeName() throws SerialException {
  65. return baseTypeName;
  66. }
  67. /**
  68. * Returns an <code>Object</code> representing the SQL structured type
  69. * to which this <code>SerialRef</code> object refers. The attributes
  70. * of the structured type are mapped according to the given type map.
  71. *
  72. * @param map a <code>java.util.Map</code> object containing zero or
  73. * more entries, with each entry consisting of 1) a <code>String</code>
  74. * giving the fully qualified name of a UDT and 2) the
  75. * <code>Class</code> object for the <code>SQLData</code> implementation
  76. * that defines how the UDT is to be mapped
  77. * @return an object instance resolved from the Ref reference and mapped
  78. * according to the supplied type map
  79. * @throws SerialException if an error is encountered in the reference
  80. * resolution
  81. */
  82. public Object getObject(java.util.Map<String,Class<?>> map)
  83. throws SerialException
  84. {
  85. map = new Hashtable(map);
  86. if (!object.equals(null)) {
  87. return map.get(object);
  88. } else {
  89. throw new SerialException("The object is not set");
  90. }
  91. }
  92. /**
  93. * Returns an <code>Object</code> representing the SQL structured type
  94. * to which this <code>SerialRef</code> object refers.
  95. *
  96. * @return an object instance resolved from the Ref reference
  97. * @throws SerialException if an error is encountered in the reference
  98. * resolution
  99. */
  100. public Object getObject() throws SerialException {
  101. if (reference != null) {
  102. try {
  103. return reference.getObject();
  104. } catch (SQLException e) {
  105. throw new SerialException("SQLException: " + e.getMessage());
  106. }
  107. }
  108. if (object != null) {
  109. return object;
  110. }
  111. throw new SerialException("The object is not set");
  112. }
  113. /**
  114. * Sets the SQL structured type that this <code>SerialRef</code> object
  115. * references to the given <code>Object</code> object.
  116. *
  117. * @param obj an <code>Object</code> representing the SQL structured type
  118. * to be referenced
  119. * @throws SerialException if an error is encountered generating the
  120. * the structured type referenced by this <code>SerialRef</code> object
  121. */
  122. public void setObject(Object obj) throws SerialException {
  123. try {
  124. reference.setObject(obj);
  125. } catch (SQLException e) {
  126. throw new SerialException("SQLException: " + e.getMessage());
  127. }
  128. object = obj;
  129. }
  130. /**
  131. * The identifier that assists in the serialization of this <code>SerialRef</code>
  132. * object.
  133. */
  134. static final long serialVersionUID = -4727123500609662274L;
  135. }