1. /* ====================================================================
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowledgement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowledgement may appear in the software itself,
  24. * if and wherever such third-party acknowledgements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.commons.lang.exception;
  55. import java.io.PrintStream;
  56. import java.io.PrintWriter;
  57. /**
  58. * An interface to be implemented by {@link java.lang.Throwable}
  59. * extensions which would like to be able to nest root exceptions
  60. * inside themselves.
  61. *
  62. * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
  63. * @author <a href="mailto:knielsen@apache.org">Kasper Nielsen</a>
  64. * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
  65. * @author Pete Gieser
  66. * @since 1.0
  67. * @version $Id: Nestable.java,v 1.9 2003/08/18 02:22:24 bayard Exp $
  68. */
  69. public interface Nestable {
  70. /**
  71. * Returns the reference to the exception or error that caused the
  72. * exception implementing the <code>Nestable</code> to be thrown.
  73. *
  74. * @return throwable that caused the original exception
  75. */
  76. public Throwable getCause();
  77. /**
  78. * Returns the error message of this and any nested
  79. * <code>Throwable</code>.
  80. *
  81. * @return the error message
  82. */
  83. public String getMessage();
  84. /**
  85. * Returns the error message of the <code>Throwable</code> in the chain
  86. * of <code>Throwable</code>s at the specified index, numbererd from 0.
  87. *
  88. * @param index the index of the <code>Throwable</code> in the chain of
  89. * <code>Throwable</code>s
  90. * @return the error message, or null if the <code>Throwable</code> at the
  91. * specified index in the chain does not contain a message
  92. * @throws IndexOutOfBoundsException if the <code>index</code> argument is
  93. * negative or not less than the count of <code>Throwable</code>s in the
  94. * chain
  95. */
  96. public String getMessage(int index);
  97. /**
  98. * Returns the error message of this and any nested <code>Throwable</code>s
  99. * in an array of Strings, one element for each message. Any
  100. * <code>Throwable</code> not containing a message is represented in the
  101. * array by a null. This has the effect of cause the length of the returned
  102. * array to be equal to the result of the {@link #getThrowableCount()}
  103. * operation.
  104. *
  105. * @return the error messages
  106. */
  107. public String[] getMessages();
  108. /**
  109. * Returns the <code>Throwable</code> in the chain of
  110. * <code>Throwable</code>s at the specified index, numbererd from 0.
  111. *
  112. * @param index the index, numbered from 0, of the <code>Throwable</code> in
  113. * the chain of <code>Throwable</code>s
  114. * @return the <code>Throwable</code>
  115. * @throws IndexOutOfBoundsException if the <code>index</code> argument is
  116. * negative or not less than the count of <code>Throwable</code>s in the
  117. * chain
  118. */
  119. public Throwable getThrowable(int index);
  120. /**
  121. * Returns the number of nested <code>Throwable</code>s represented by
  122. * this <code>Nestable</code>, including this <code>Nestable</code>.
  123. *
  124. * @return the throwable count
  125. */
  126. public int getThrowableCount();
  127. /**
  128. * Returns this <code>Nestable</code> and any nested <code>Throwable</code>s
  129. * in an array of <code>Throwable</code>s, one element for each
  130. * <code>Throwable</code>.
  131. *
  132. * @return the <code>Throwable</code>s
  133. */
  134. public Throwable[] getThrowables();
  135. /**
  136. * Returns the index, numbered from 0, of the first occurrence of the
  137. * specified type in the chain of <code>Throwable</code>s, or -1 if the
  138. * specified type is not found in the chain.
  139. *
  140. * @param type <code>Class</code> to be found
  141. * @return index of the first occurrence of the type in the chain, or -1 if
  142. * the type is not found
  143. */
  144. public int indexOfThrowable(Class type);
  145. /**
  146. * Returns the index, numbered from 0, of the first <code>Throwable</code>
  147. * that matches the specified type in the chain of <code>Throwable</code>s
  148. * with an index greater than or equal to the specified index, or -1 if
  149. * the type is not found.
  150. *
  151. * @param type <code>Class</code> to be found
  152. * @param fromIndex the index, numbered from 0, of the starting position in
  153. * the chain to be searched
  154. * @return index of the first occurrence of the type in the chain, or -1 if
  155. * the type is not found
  156. * @throws IndexOutOfBoundsException if the <code>fromIndex</code> argument
  157. * is negative or not less than the count of <code>Throwable</code>s in the
  158. * chain
  159. */
  160. public int indexOfThrowable(Class type, int fromIndex);
  161. /**
  162. * Prints the stack trace of this exception to the specified print
  163. * writer. Includes information from the exception, if any,
  164. * which caused this exception.
  165. *
  166. * @param out <code>PrintWriter</code> to use for output.
  167. */
  168. public void printStackTrace(PrintWriter out);
  169. /**
  170. * Prints the stack trace of this exception to the specified print
  171. * stream. Includes inforamation from the exception, if any,
  172. * which caused this exception.
  173. *
  174. * @param out <code>PrintStream</code> to use for output.
  175. */
  176. public void printStackTrace(PrintStream out);
  177. /**
  178. * Prints the stack trace for this exception only--root cause not
  179. * included--using the provided writer. Used by {@link
  180. * org.apache.commons.lang.exception.NestableDelegate} to write
  181. * individual stack traces to a buffer. The implementation of
  182. * this method should call
  183. * <code>super.printStackTrace(out);</code> in most cases.
  184. *
  185. * @param out The writer to use.
  186. */
  187. public void printPartialStackTrace(PrintWriter out);
  188. }