View Javadoc
1   /*
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 2002 (C) Intalio Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  package org.exolab.castor.builder.binding;
36  
37  /**
38   * The base exception for the <tt>binding</tt> package. This exception is nested in order to keep a
39   * correct stack trace while nesting the exception that causes the call to BindingException.
40   *
41   * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
42   * @version $Version:$ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $
43   */
44  public final class BindingException extends Exception {
45  
46    /** We add this field because an Exception is serializable. */
47    private static final long serialVersionUID = 1726983206974247107L;
48    /** The embedded exception if tunnelling, or null. */
49    private Exception _exception;
50  
51    /**
52     * Creates a new BindingException.
53     *
54     * @param message The error or warning message.
55     */
56    public BindingException(final String message) {
57      super(message);
58      _exception = null;
59    }
60  
61    /**
62     * Creates a new BindingException wrapping an existing Exception.
63     * <p>
64     * The existing Exception will be embedded in the new one, and its message will become the default
65     * message for the BindingException.
66     *
67     * @param exception The Exception to be wrapped in a BindingException.
68     */
69    public BindingException(final Exception exception) {
70      super();
71      _exception = exception;
72    }
73  
74    /**
75     * Creates a new BindingException from an existing exception.
76     * <p>
77     * The existing Exception will be embedded in the new one, but the new Exception will have its own
78     * message.
79     *
80     * @param message The detail message.
81     * @param exception The Exception to be wrapped in a BindingException.
82     */
83    public BindingException(final String message, final Exception exception) {
84      super(message);
85      _exception = exception;
86    }
87  
88    /**
89     * Returns a detailed message for this Exception.
90     * <p>
91     * If there is an embedded Exception, and if the BindingException has no detail message of its
92     * own, this method will return the detail message from the embedded Exception.
93     *
94     * @return String The error or warning message.
95     */
96    public String getMessage() {
97      String message = super.getMessage();
98  
99      if (message == null && _exception != null) {
100       return _exception.getMessage();
101     }
102     return message;
103   }
104 
105   /**
106    * Returns the embedded Exception, if any.
107    *
108    * @return Exception The embedded Exception, or null if there is none.
109    */
110   public Exception getException() {
111     return _exception;
112   }
113 
114   /**
115    * Overrides printStackTrace to keep the stack trace of the embedded Exception.
116    */
117   public void printStackTrace() {
118     if (_exception != null) {
119       System.out.println("--------------------------------");
120       System.out.println("Stack Trace for :" + _exception);
121       _exception.printStackTrace();
122       System.out.println("--------------------------------");
123     }
124     super.printStackTrace();
125   }
126 
127   /**
128    * Overrides toString to pick up any embedded Exception.
129    *
130    * @return String A string representation of this Exception.
131    */
132   public String toString() {
133     if (_exception != null) {
134       return _exception.toString();
135     }
136     return super.toString();
137   }
138 
139 }