View Javadoc
1   /*
2    * Copyright (C) 2003-2012 David E. Berry
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   *
18   * A copy of the GNU Lesser General Public License may also be found at
19   * http://www.gnu.org/licenses/lgpl.txt
20   */
21  package org.synchronoss.cpo;
22  
23  /**
24   * A
25   * <code>CpoException</code> is the common superclass for any number of CPO related exceptions that may occur during the
26   * execution of a business task.
27   *
28   * @author David E. Berry
29   */
30  public class CpoException extends Exception {
31  
32    /**
33     * Version Id for this class.
34     */
35    private static final long serialVersionUID = 1L;
36  
37    /**
38     * Nested Exception to hold wrapped exception.
39     *
40     * @serial
41     */
42    private Throwable detail;
43  
44    /**
45     * Constructs a
46     * <code>CpoException</code> with no specified detail message.
47     */
48    public CpoException() {
49    }
50  
51    /**
52     * Constructs a
53     * <code>CpoException</code> with the specified detail message.
54     *
55     * @param s the detail message
56     */
57    public CpoException(String s) {
58      super(s);
59    }
60  
61    /**
62     * Constructs a
63     * <code>CpoException</code> with the specified detail message and nested exception.
64     *
65     * @param s the detail message
66     * @param ex the nested exception
67     */
68    public CpoException(String s, Throwable ex) {
69      super(s);
70      detail = ex;
71    }
72  
73    /**
74     * Constructs a
75     * <code>CpoException</code> with the specified detail message and nested exception.
76     *
77     * @param ex the nested exception
78     */
79    public CpoException(Throwable ex) {
80      super();
81      detail = ex;
82    }
83  
84    /**
85     * Returns the detail message, including the message from the nested exception if there is one.
86     */
87    @Override
88    public String getMessage() {
89      StringBuilder msg = new StringBuilder("\n");
90  
91      msg.append(super.getMessage());
92  
93      if (detail != null) {
94        msg.append("\n");
95        msg.append(detail.getMessage());
96        if (detail.getCause() != null) {
97          msg.append(detail.getCause().getMessage());
98        }
99      }
100     return msg.toString();
101   }
102 
103   /**
104    * Returns the detail message, including the message from the nested exception if there is one.
105    */
106   @Override
107   public String getLocalizedMessage() {
108     StringBuilder msg = new StringBuilder("\n");
109 
110     msg.append(super.getLocalizedMessage());
111 
112     if (detail != null) {
113       msg.append("\n");
114       msg.append(detail.getLocalizedMessage());
115       if (detail.getCause() != null) {
116         msg.append(detail.getCause().getLocalizedMessage());
117       }
118     }
119     return msg.toString();
120   }
121 
122   /**
123    * Prints the composite message and the embedded stack trace to the specified stream
124    * <code>ps</code>.
125    *
126    * @param ps the print stream
127    */
128   @Override
129   public void printStackTrace(java.io.PrintStream ps) {
130     synchronized (ps) {
131       if (detail != null) {
132         detail.printStackTrace(ps);
133       }
134       super.printStackTrace(ps);
135     }
136   }
137 
138   /**
139    * Prints the composite message to
140    * <code>System.err</code>.
141    */
142   @Override
143   public void printStackTrace() {
144     printStackTrace(System.err);
145   }
146 
147   /**
148    * Prints the composite message and the embedded stack trace to the specified print writer
149    * <code>pw</code>.
150    *
151    * @param pw the print writer
152    */
153   @Override
154   public void printStackTrace(java.io.PrintWriter pw) {
155     synchronized (pw) {
156       if (detail != null) {
157         detail.printStackTrace(pw);
158       }
159       super.printStackTrace(pw);
160     }
161   }
162 }