Home | History | Annotate | Download | only in datatype
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 // $Id: DatatypeConfigurationException.java 569987 2007-08-27 04:08:46Z mrglavas $
     19 
     20 package javax.xml.datatype;
     21 
     22 import java.io.IOException;
     23 import java.io.ObjectInputStream;
     24 import java.io.PrintStream;
     25 import java.io.PrintWriter;
     26 import java.lang.reflect.Method;
     27 
     28 /**
     29  * <p>Indicates a serious configuration error.</p>
     30  *
     31  * @author <a href="mailto:Jeff.Suttor (at) Sun.com">Jeff Suttor</a>
     32  * @version $Revision: 569987 $, $Date: 2007-08-26 21:08:46 -0700 (Sun, 26 Aug 2007) $
     33  * @since 1.5
     34  */
     35 
     36 public class DatatypeConfigurationException extends Exception {
     37 
     38     /** Stream Unique Identifier. */
     39     private static final long serialVersionUID = -1699373159027047238L;
     40 
     41     /** This field is required to store the cause on JDK 1.3 and below. */
     42     private Throwable causeOnJDK13OrBelow;
     43 
     44     /** Indicates whether this class is being used in a JDK 1.4 context. */
     45     private transient boolean isJDK14OrAbove = false;
     46 
     47     /**
     48      * <p>Create a new <code>DatatypeConfigurationException</code> with
     49      * no specified detail message and cause.</p>
     50      */
     51 
     52     public DatatypeConfigurationException() {
     53         super();
     54     }
     55 
     56     /**
     57      * <p>Create a new <code>DatatypeConfigurationException</code> with
     58      * the specified detail message.</p>
     59      *
     60      * @param message The detail message.
     61      */
     62 
     63     public DatatypeConfigurationException(String message) {
     64         super(message);
     65     }
     66 
     67     /**
     68      * <p>Create a new <code>DatatypeConfigurationException</code> with
     69      * the specified detail message and cause.</p>
     70      *
     71      * @param message The detail message.
     72      * @param cause The cause.  A <code>null</code> value is permitted, and indicates that the cause is nonexistent or unknown.
     73      */
     74 
     75     public DatatypeConfigurationException(String message, Throwable cause) {
     76         super(message);
     77         initCauseByReflection(cause);
     78     }
     79 
     80     /**
     81      * <p>Create a new <code>DatatypeConfigurationException</code> with
     82      * the specified cause.</p>
     83      *
     84      * @param cause The cause.  A <code>null</code> value is permitted, and indicates that the cause is nonexistent or unknown.
     85      */
     86 
     87     public DatatypeConfigurationException(Throwable cause) {
     88         super(cause == null ? null : cause.toString());
     89         initCauseByReflection(cause);
     90     }
     91 
     92     /**
     93      * Print the the trace of methods from where the error
     94      * originated.  This will trace all nested exception
     95      * objects, as well as this object.
     96      */
     97     public void printStackTrace() {
     98         if (!isJDK14OrAbove && causeOnJDK13OrBelow != null) {
     99             printStackTrace0(new PrintWriter(System.err, true));
    100         }
    101         else {
    102             super.printStackTrace();
    103         }
    104     }
    105 
    106     /**
    107      * Print the the trace of methods from where the error
    108      * originated.  This will trace all nested exception
    109      * objects, as well as this object.
    110      * @param s The stream where the dump will be sent to.
    111      */
    112     public void printStackTrace(PrintStream s) {
    113         if (!isJDK14OrAbove && causeOnJDK13OrBelow != null) {
    114             printStackTrace0(new PrintWriter(s));
    115         }
    116         else {
    117             super.printStackTrace(s);
    118         }
    119     }
    120 
    121     /**
    122      * Print the the trace of methods from where the error
    123      * originated.  This will trace all nested exception
    124      * objects, as well as this object.
    125      * @param s The writer where the dump will be sent to.
    126      */
    127     public void printStackTrace(PrintWriter s) {
    128         if (!isJDK14OrAbove && causeOnJDK13OrBelow != null) {
    129             printStackTrace0(s);
    130         }
    131         else {
    132             super.printStackTrace(s);
    133         }
    134     }
    135 
    136     private void printStackTrace0(PrintWriter s) {
    137         causeOnJDK13OrBelow.printStackTrace(s);
    138         s.println("------------------------------------------");
    139         super.printStackTrace(s);
    140     }
    141 
    142     private void initCauseByReflection(Throwable cause) {
    143         causeOnJDK13OrBelow = cause;
    144         try {
    145             Method m = this.getClass().getMethod("initCause", new Class[] {Throwable.class});
    146             m.invoke(this, new Object[] {cause});
    147             isJDK14OrAbove = true;
    148         }
    149         // Ignore exception
    150         catch (Exception e) {}
    151     }
    152 
    153     private void readObject(ObjectInputStream in)
    154         throws IOException, ClassNotFoundException {
    155         in.defaultReadObject();
    156         try {
    157             Method m1 = this.getClass().getMethod("getCause", new Class[] {});
    158             Throwable cause = (Throwable) m1.invoke(this, new Object[] {});
    159             if (causeOnJDK13OrBelow == null) {
    160                 causeOnJDK13OrBelow = cause;
    161             }
    162             else if (cause == null) {
    163                 Method m2 = this.getClass().getMethod("initCause", new Class[] {Throwable.class});
    164                 m2.invoke(this, new Object[] {causeOnJDK13OrBelow});
    165             }
    166             isJDK14OrAbove = true;
    167         }
    168         // Ignore exception
    169         catch (Exception e) {}
    170     }
    171 }
    172