1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @(#)MessagingException.java        1.10 02/06/13
  28  */
  29 
  30 
  31 
  32 package com.sun.xml.internal.messaging.saaj.packaging.mime;
  33 
  34 
  35 /**
  36  * The base class for all exceptions thrown by the Messaging classes
  37  *
  38  * @author John Mani
  39  * @author Bill Shannon
  40  */
  41 
  42 public class MessagingException extends Exception {
  43 
  44     /**
  45      * The next exception in the chain.
  46      *
  47      * @serial
  48      */
  49     private Exception next;
  50 
  51     /**
  52      * Constructs a MessagingException with no detail message.
  53      */
  54     public MessagingException() {
  55         super();
  56     }
  57 
  58     /**
  59      * Constructs a MessagingException with the specified detail message.
  60      * @param s         the detail message
  61      */
  62     public MessagingException(String s) {
  63         super(s);
  64     }
  65 
  66     /**
  67      * Constructs a MessagingException with the specified
  68      * Exception and detail message. The specified exception is chained
  69      * to this exception.
  70      * @param s         the detail message
  71      * @param e         the embedded exception
  72      * @see     #getNextException
  73      * @see     #setNextException
  74      */
  75     public MessagingException(String s, Exception e) {
  76         super(s);
  77         next = e;
  78     }
  79 
  80     /**
  81      * Get the next exception chained to this one. If the
  82      * next exception is a MessagingException, the chain
  83      * may extend further.
  84      *
  85      * @return  next Exception, null if none.
  86      */
  87     public synchronized Exception getNextException() {
  88         return next;
  89     }
  90 
  91     /**
  92      * Add an exception to the end of the chain. If the end
  93      * is <strong>not</strong> a MessagingException, this
  94      * exception cannot be added to the end.
  95      *
  96      * @param   ex      the new end of the Exception chain
  97      * @return          <code>true</code> if the this Exception
  98      *                  was added, <code>false</code> otherwise.
  99      */
 100     public synchronized boolean setNextException(Exception ex) {
 101         Exception theEnd = this;
 102         while (theEnd instanceof MessagingException &&
 103                ((MessagingException)theEnd).next != null) {
 104             theEnd = ((MessagingException)theEnd).next;
 105         }
 106         // If the end is a MessagingException, we can add this
 107         // exception to the chain.
 108         if (theEnd instanceof MessagingException) {
 109             ((MessagingException)theEnd).next = ex;
 110             return true;
 111         } else
 112             return false;
 113     }
 114 
 115     /**
 116      * Produce the message, include the message from the nested
 117      * exception if there is one.
 118      */
 119     public String getMessage() {
 120         if (next == null)
 121             return super.getMessage();
 122         Exception n = next;
 123         String s = super.getMessage();
 124         StringBuilder sb = new StringBuilder(s == null ? "" : s);
 125         while (n != null) {
 126             sb.append(";\n  nested exception is:\n\t");
 127             if (n instanceof MessagingException) {
 128                 MessagingException mex = (MessagingException)n;
 129                 sb.append(n.getClass().toString());
 130                 String msg = mex.getSuperMessage();
 131                 if (msg != null) {
 132                     sb.append(": ");
 133                     sb.append(msg);
 134                 }
 135                 n = mex.next;
 136             } else {
 137                 sb.append(n.toString());
 138                 n = null;
 139             }
 140         }
 141         return sb.toString();
 142     }
 143 
 144     private String getSuperMessage() {
 145         return super.getMessage();
 146     }
 147 }