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     * Nested Exception to hold wrapped exception.
38     *
39     * @serial
40     */
41    private Throwable detail;
42  
43    /**
44     * Constructs a
45     * <code>CpoException</code> with no specified detail message.
46     */
47    public CpoException() {
48    }
49  
50    /**
51     * Constructs a
52     * <code>CpoException</code> with the specified detail message.
53     *
54     * @param s the detail message
55     */
56    public CpoException(String s) {
57      super(s);
58    }
59  
60    /**
61     * Constructs a
62     * <code>CpoException</code> with the specified detail message and nested exception.
63     *
64     * @param s the detail message
65     * @param ex the nested exception
66     */
67    public CpoException(String s, Throwable ex) {
68      super(s);
69      detail = ex;
70    }
71  
72    /**
73     * Constructs a
74     * <code>CpoException</code> with the specified detail message and nested exception.
75     *
76     * @param ex the nested exception
77     */
78    public CpoException(Throwable ex) {
79      super();
80      detail = ex;
81    }
82  
83    /**
84     * Returns the detail message, including the message from the nested exception if there is one.
85     */
86    @Override
87    public String getMessage() {
88      StringBuilder msg = new StringBuilder("\n");
89  
90      msg.append(super.getMessage());
91  
92      if (detail != null) {
93        msg.append("\n");
94        msg.append(detail.getMessage());
95        if (detail.getCause() != null) {
96          msg.append(detail.getCause().getMessage());
97        }
98      }
99      return msg.toString();
100   }
101 
102   /**
103    * Returns the detail message, including the message from the nested exception if there is one.
104    */
105   @Override
106   public String getLocalizedMessage() {
107     StringBuilder msg = new StringBuilder("\n");
108 
109     msg.append(super.getLocalizedMessage());
110 
111     if (detail != null) {
112       msg.append("\n");
113       msg.append(detail.getLocalizedMessage());
114       if (detail.getCause() != null) {
115         msg.append(detail.getCause().getLocalizedMessage());
116       }
117     }
118     return msg.toString();
119   }
120 
121   /**
122    * Prints the composite message and the embedded stack trace to the specified stream
123    * <code>ps</code>.
124    *
125    * @param ps the print stream
126    */
127   @Override
128   public void printStackTrace(java.io.PrintStream ps) {
129     synchronized (ps) {
130       if (detail != null) {
131         detail.printStackTrace(ps);
132       }
133       super.printStackTrace(ps);
134     }
135   }
136 
137   /**
138    * Prints the composite message to
139    * <code>System.err</code>.
140    */
141   @Override
142   public void printStackTrace() {
143     printStackTrace(System.err);
144   }
145 
146   /**
147    * Prints the composite message and the embedded stack trace to the specified print writer
148    * <code>pw</code>.
149    *
150    * @param pw the print writer
151    */
152   @Override
153   public void printStackTrace(java.io.PrintWriter pw) {
154     synchronized (pw) {
155       if (detail != null) {
156         detail.printStackTrace(pw);
157       }
158       super.printStackTrace(pw);
159     }
160   }
161 }