001/* SaslException.java
002   Copyright (C) 2003, 2005, Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package javax.security.sasl;
040
041import gnu.java.lang.CPStringBuilder;
042
043import java.io.IOException;
044import java.io.PrintStream;
045import java.io.PrintWriter;
046import java.io.Serializable;
047
048/**
049 * This class represents an error that has occurred when using SASL.
050 *
051 * @since 1.5
052 */
053public class SaslException extends IOException implements Serializable
054{
055
056  // Constants and variables
057  // -------------------------------------------------------------------------
058
059  private static final long serialVersionUID = 4579784287983423626L;
060
061  /**
062   * @serial The possibly null root cause exception.
063   */
064  private Throwable _exception = null;
065
066  // Constructor(s)
067  // -------------------------------------------------------------------------
068
069  /**
070   * Constructs a new instance of <code>SaslException</code>. The root
071   * exception and the detailed message are null.
072   */
073  public SaslException()
074  {
075    super();
076  }
077
078  /**
079   * Constructs a new instance of <code>SaslException</code> with a detailed
080   * message. The <code>root</code> exception is <code>null</code>.
081   *
082   * @param detail a possibly null string containing details of the exception.
083   * @see Throwable#getMessage()
084   */
085  public SaslException(String detail)
086  {
087    super(detail);
088  }
089
090  /**
091   * Constructs a new instance of <code>SaslException</code> with a detailed
092   * message and a root exception. For example, a <code>SaslException</code>
093   * might result from a problem with the callback handler, which might throw a
094   * {@link javax.security.auth.callback.UnsupportedCallbackException} if it
095   * does not support the requested callback, or throw an {@link IOException}
096   * if it had problems obtaining data for the callback. The
097   * <code>SaslException</code>'s root exception would be then be the exception
098   * thrown by the callback handler.
099   *
100   * @param detail a possibly <code>null</code> string containing details of
101   * the exception.
102   * @param ex a possibly <code>null</code> root exception that caused this
103   * exception.
104   * @see Throwable#getMessage()
105   * @see #getCause()
106   */
107  public SaslException(String detail, Throwable ex)
108  {
109    super(detail);
110    _exception = ex;
111  }
112
113  // Class methods
114  // -------------------------------------------------------------------------
115
116  // Instance methods
117  // -------------------------------------------------------------------------
118
119  /**
120   * Returns the cause of this throwable or <code>null</code> if the cause is
121   * nonexistent or unknown. The cause is the throwable that caused this
122   * exception to be thrown.
123   *
124   * @return the possibly <code>null</code> exception that caused this exception.
125   */
126  public Throwable getCause()
127  {
128    return _exception;
129  }
130
131  /**
132   * Prints this exception's stack trace to <code>System.err</code>. If this
133   * exception has a root exception; the stack trace of the root exception is
134   * also printed to <code>System.err</code>.
135   */
136  public void printStackTrace()
137  {
138    super.printStackTrace();
139    if (_exception != null)
140      _exception.printStackTrace();
141  }
142
143  /**
144   * Prints this exception's stack trace to a print stream. If this exception
145   * has a root exception; the stack trace of the root exception is also
146   * printed to the print stream.
147   *
148   * @param ps the non-null print stream to which to print.
149   */
150  public void printStackTrace(PrintStream ps)
151  {
152    super.printStackTrace(ps);
153    if (_exception != null)
154      _exception.printStackTrace(ps);
155  }
156
157  /**
158   * Prints this exception's stack trace to a print writer. If this exception
159   * has a root exception; the stack trace of the root exception is also
160   * printed to the print writer.
161   *
162   * @param pw the non-null print writer to use for output.
163   */
164  public void printStackTrace(PrintWriter pw)
165  {
166    super.printStackTrace(pw);
167    if (_exception != null)
168      _exception.printStackTrace(pw);
169  }
170
171  /**
172   * Returns the string representation of this exception. The string
173   * representation contains this exception's class name, its detailed
174   * messsage, and if it has a root exception, the string representation of the
175   * root exception. This string representation is meant for debugging and not
176   * meant to be interpreted programmatically.
177   *
178   * @return the non-null string representation of this exception.
179   * @see Throwable#getMessage()
180   */
181  public String toString()
182  {
183    CPStringBuilder sb = new CPStringBuilder(this.getClass().getName())
184      .append(": ").append(super.toString());
185    if (_exception != null)
186      sb.append("; caused by: ").append(_exception.toString());
187    return sb.toString();
188  }
189}