1 /*
   2  * Copyright (c) 2003, 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 package javax.xml.bind;
  27 
  28 /**
  29  * This exception indicates that a violation of a dynamically checked type
  30  * constraint was detected.
  31  *
  32  * <p>
  33  * This exception can be thrown by the generated setter methods of the schema
  34  * derived Java content classes.  However, since fail-fast validation is
  35  * an optional feature for JAXB Providers to support, not all setter methods
  36  * will throw this exception when a type constraint is violated.
  37  *
  38  * <p>
  39  * If this exception is throw while invoking a fail-fast setter, the value of
  40  * the property is guaranteed to remain unchanged, as if the setter were never
  41  * called.
  42  *
  43  * @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li><li>Joe Fialli, Sun Microsystems, Inc.</li></ul>
  44  * @see ValidationEvent
  45  * @since JAXB1.0
  46  */
  47 
  48 public class TypeConstraintException extends java.lang.RuntimeException {
  49 
  50     /**
  51      * Vendor specific error code
  52      *
  53      */
  54     private String errorCode;
  55 
  56     /**
  57      * Exception reference
  58      *
  59      */
  60     private volatile Throwable linkedException;
  61 
  62     static final long serialVersionUID = -3059799699420143848L;
  63 
  64     /**
  65      * Construct a TypeConstraintException with the specified detail message.  The
  66      * errorCode and linkedException will default to null.
  67      *
  68      * @param message a description of the exception
  69      */
  70     public TypeConstraintException(String message) {
  71         this( message, null, null );
  72     }
  73 
  74     /**
  75      * Construct a TypeConstraintException with the specified detail message and vendor
  76      * specific errorCode.  The linkedException will default to null.
  77      *
  78      * @param message a description of the exception
  79      * @param errorCode a string specifying the vendor specific error code
  80      */
  81     public TypeConstraintException(String message, String errorCode) {
  82         this( message, errorCode, null );
  83     }
  84 
  85     /**
  86      * Construct a TypeConstraintException with a linkedException.  The detail message and
  87      * vendor specific errorCode will default to null.
  88      *
  89      * @param exception the linked exception
  90      */
  91     public TypeConstraintException(Throwable exception) {
  92         this( null, null, exception );
  93     }
  94 
  95     /**
  96      * Construct a TypeConstraintException with the specified detail message and
  97      * linkedException.  The errorCode will default to null.
  98      *
  99      * @param message a description of the exception
 100      * @param exception the linked exception
 101      */
 102     public TypeConstraintException(String message, Throwable exception) {
 103         this( message, null, exception );
 104     }
 105 
 106     /**
 107      * Construct a TypeConstraintException with the specified detail message,
 108      * vendor specific errorCode, and linkedException.
 109      *
 110      * @param message a description of the exception
 111      * @param errorCode a string specifying the vendor specific error code
 112      * @param exception the linked exception
 113      */
 114     public TypeConstraintException(String message, String errorCode, Throwable exception) {
 115         super( message );
 116         this.errorCode = errorCode;
 117         this.linkedException = exception;
 118     }
 119 
 120     /**
 121      * Get the vendor specific error code
 122      *
 123      * @return a string specifying the vendor specific error code
 124      */
 125     public String getErrorCode() {
 126         return this.errorCode;
 127     }
 128 
 129     /**
 130      * Get the linked exception
 131      *
 132      * @return the linked Exception, null if none exists
 133      */
 134     public Throwable getLinkedException() {
 135         return linkedException;
 136     }
 137 
 138     /**
 139      * Add a linked Exception.
 140      *
 141      * @param exception the linked Exception (A null value is permitted and
 142      *                  indicates that the linked exception does not exist or
 143      *                  is unknown).
 144      */
 145     public void setLinkedException( Throwable exception ) {
 146         this.linkedException = exception;
 147     }
 148 
 149     /**
 150      * Returns a short description of this TypeConstraintException.
 151      *
 152      */
 153     public String toString() {
 154         return linkedException == null ?
 155             super.toString() :
 156             super.toString() + "\n - with linked exception:\n[" +
 157                                 linkedException.toString()+ "]";
 158     }
 159 
 160     /**
 161      * Prints this TypeConstraintException and its stack trace (including the stack trace
 162      * of the linkedException if it is non-null) to the PrintStream.
 163      *
 164      * @param s PrintStream to use for output
 165      */
 166     public void printStackTrace( java.io.PrintStream s ) {
 167         if( linkedException != null ) {
 168           linkedException.printStackTrace(s);
 169           s.println("--------------- linked to ------------------");
 170         }
 171 
 172         super.printStackTrace(s);
 173     }
 174 
 175     /**
 176      * Prints this TypeConstraintException and its stack trace (including the stack trace
 177      * of the linkedException if it is non-null) to <tt>System.err</tt>.
 178      *
 179      */
 180     public void printStackTrace() {
 181         printStackTrace(System.err);
 182     }
 183 
 184 }