1. /*
  2. * @(#)SaslException.java 1.12 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.security.sasl;
  8. import java.io.IOException;
  9. /**
  10. * This class represents an error that has occurred when using SASL.
  11. *
  12. * @since 1.5
  13. *
  14. * @author Rosanna Lee
  15. * @author Rob Weltman
  16. */
  17. public class SaslException extends IOException {
  18. /**
  19. * The possibly null root cause exception.
  20. * @serial
  21. */
  22. // Required for serialization interoperability with JSR 28
  23. private Throwable _exception;
  24. /**
  25. * Constructs a new instance of <tt>SaslException</tt>.
  26. * The root exception and the detailed message are null.
  27. */
  28. public SaslException () {
  29. super();
  30. }
  31. /**
  32. * Constructs a new instance of <tt>SaslException</tt> with a detailed message.
  33. * The root exception is null.
  34. * @param detail A possibly null string containing details of the exception.
  35. *
  36. * @see java.lang.Throwable#getMessage
  37. */
  38. public SaslException (String detail) {
  39. super(detail);
  40. }
  41. /**
  42. * Constructs a new instance of <tt>SaslException</tt> with a detailed message
  43. * and a root exception.
  44. * For example, a SaslException might result from a problem with
  45. * the callback handler, which might throw a NoSuchCallbackException if
  46. * it does not support the requested callback, or throw an IOException
  47. * if it had problems obtaining data for the callback. The
  48. * SaslException's root exception would be then be the exception thrown
  49. * by the callback handler.
  50. *
  51. * @param detail A possibly null string containing details of the exception.
  52. * @param ex A possibly null root exception that caused this exception.
  53. *
  54. * @see java.lang.Throwable#getMessage
  55. * @see #getCause
  56. */
  57. public SaslException (String detail, Throwable ex) {
  58. super(detail);
  59. if (ex != null) {
  60. initCause(ex);
  61. }
  62. }
  63. /*
  64. * Override Throwable.getCause() to ensure deserialized object from
  65. * JSR 28 would return same value for getCause() (i.e., _exception).
  66. */
  67. public Throwable getCause() {
  68. return _exception;
  69. }
  70. /*
  71. * Override Throwable.initCause() to match getCause() by updating
  72. * _exception as well.
  73. */
  74. public Throwable initCause(Throwable cause) {
  75. super.initCause(cause);
  76. _exception = cause;
  77. return this;
  78. }
  79. /**
  80. * Returns the string representation of this exception.
  81. * The string representation contains
  82. * this exception's class name, its detailed messsage, and if
  83. * it has a root exception, the string representation of the root
  84. * exception. This string representation
  85. * is meant for debugging and not meant to be interpreted
  86. * programmatically.
  87. * @return The non-null string representation of this exception.
  88. * @see java.lang.Throwable#getMessage
  89. */
  90. // Override Throwable.toString() to conform to JSR 28
  91. public String toString() {
  92. String answer = super.toString();
  93. if (_exception != null && _exception != this) {
  94. answer += " [Caused by " + _exception.toString() + "]";
  95. }
  96. return answer;
  97. }
  98. /** Use serialVersionUID from JSR 28 RI for interoperability */
  99. private static final long serialVersionUID = 4579784287983423626L;
  100. }