Save This Page
Home » jdo2-model-2.3-ea-src » org.apache.jdo.model » [javadoc | source]
    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   package org.apache.jdo.model;
   19   
   20   import org.apache.jdo.util.I18NHelper;
   21   
   22   /**
   23    * This exception indicates a fatal Model problem. Note this is an
   24    * unchecked exception.
   25    *
   26    * @author Michael Bouschen
   27    * @since JDO 1.0.1
   28    */
   29   public class ModelFatalException 
   30       extends RuntimeException
   31   {
   32       /** The throwable that caused this model exception to be thrown. */
   33       private Throwable cause;
   34       
   35       /** Flag indicating whether printStackTrace is being executed. */
   36       private boolean inPrintStackTrace = false;
   37   
   38       /** I18N support */
   39       private static I18NHelper msg = 
   40           I18NHelper.getInstance(ModelFatalException.class);
   41   
   42       /**
   43        * Creates new <code>ModelFatalException</code> without detail
   44        * message. 
   45        */
   46       public ModelFatalException() 
   47       {
   48       }
   49       
   50       /**
   51        * Constructs a <code>ModelFatalException</code> with the specified
   52        * detail message.
   53        * @param message the detail message.
   54        */
   55       public ModelFatalException(String message)
   56       {
   57           super(message);
   58       }
   59   
   60       /** 
   61        * Constructs a new <code>ModelFatalException</code> with the specified 
   62        * detail message and cause Exception.
   63        * @param message the detail message.
   64        * @param cause the cause (which is saved for later retrieval by the
   65        * {@link #getCause()} method). (A null value is permitted, and
   66        * indicates that the cause is nonexistent or unknown.) 
   67        */
   68       public ModelFatalException(String message, Throwable cause) 
   69       {
   70           super(message);
   71           this.cause = cause;
   72       }
   73   
   74       /** 
   75        * Returns the cause of this throwable or null if the cause is
   76        * nonexistent or unknown. (The cause is the throwable that caused this 
   77        * throwable to get thrown.) 
   78        * @return the cause of this throwable or null if the cause is
   79        * nonexistent or unknown. 
   80        */
   81       public Throwable getCause() 
   82       {
   83           // super.printStackTrace calls getCause to handle the cause. 
   84           // Returning null prevents the superclass from handling the cause;
   85           // instead the local implementation of printStackTrace should
   86           // handle the cause. Otherwise, the cause is printed twice.
   87           return inPrintStackTrace ? null : cause;
   88       }
   89   
   90       /**
   91        * Initializes the cause of this throwable to the specified value. (The
   92        * cause is the throwable that caused this throwable to get thrown.) 
   93        * @param cause the cause (which is saved for later retrieval by the
   94        * {@link #getCause()} method). (A null value is permitted, and
   95        * indicates that the cause is nonexistent or unknown.)
   96        * @return a reference to this <code>ModelFatalException</code> instance.
   97        */
   98       public Throwable initCause(Throwable cause)
   99       {
  100           this.cause = cause;
  101           return this;
  102       }
  103       
  104   	/**
  105        * Returns the error message string of this throwable object.
  106        * @return the error message string of this <code>ModelException</code>
  107        * object if it was created with an error message string, the error 
  108        * message of the cause if it was not created a message but the cause
  109        * has a message, or <code>null</code> if neither has an error message. 
  110        */
  111   	public String getMessage()
  112   	{
  113   		String message = super.getMessage();
  114   		if ((message == null) || (message.length() == 0)) {
  115               // Get the message of the cause Throwable, if this
  116               // ModelException does not have a message
  117               if (cause != null)
  118                   message	= cause.getMessage();
  119   		}
  120   		return message;
  121   	}
  122   
  123       /** 
  124        * The <code>String</code> representation includes the name of the class,
  125        * the descriptive comment (if any),
  126        * and the <code>String</code> representation of the cause (if any).
  127        * @return the <code>String</code>.
  128        */
  129       public synchronized String toString() 
  130       {
  131           StringBuffer sb = new StringBuffer();
  132           sb.append(super.toString());
  133           // Do not include cause information, if called by printStackTrace: 
  134           // the stacktrace will include the cause anyway.
  135           if ((cause != null) && !inPrintStackTrace) {
  136               sb.append("\n");  //NOI18N
  137               sb.append(msg.msg("MSG_CauseThrowable")); //NOI18N
  138               sb.append("\n");  //NOI18N
  139               sb.append(cause.toString()); //NOI18N
  140           }
  141           return sb.toString();
  142       }
  143     
  144       /**
  145        * Prints this <code>ModelFatalException</code> and its backtrace to the 
  146        * standard error output.
  147        * Print cause Throwable's stack trace as well.
  148        */
  149       public void printStackTrace()
  150       {
  151           printStackTrace(System.err);
  152       }
  153   
  154       /**
  155        * Prints this <code>ModelFatalException</code> and its backtrace to the 
  156        * specified print stream.
  157        * Print cause Throwable's stack trace as well.
  158        * @param s <code>PrintStream</code> to use for output
  159        */
  160       public synchronized void printStackTrace(java.io.PrintStream s) 
  161       { 
  162           synchronized (s) {
  163               inPrintStackTrace = true;
  164               super.printStackTrace(s);
  165               if (cause != null) {
  166                   s.println(msg.msg("MSG_CauseThrowableStackTrace")); //NOI18N
  167                   cause.printStackTrace(s);
  168               }
  169               inPrintStackTrace = false;
  170           }
  171       }
  172       
  173       /**
  174        * Prints this <code>ModelFatalException</code> and its backtrace to the specified
  175        * print writer.
  176        * Print cause Throwable' stack trace as well.
  177        * @param s <code>PrintWriter</code> to use for output
  178        */
  179       public void printStackTrace(java.io.PrintWriter s) 
  180       { 
  181           synchronized (s) {
  182               inPrintStackTrace = true;
  183               super.printStackTrace(s);
  184               if (cause != null) {
  185                   s.println(msg.msg("MSG_CauseThrowableStackTrace")); //NOI18N
  186                   cause.printStackTrace(s);
  187               }
  188               inPrintStackTrace = false;
  189           }
  190       }
  191       
  192   }

Save This Page
Home » jdo2-model-2.3-ea-src » org.apache.jdo.model » [javadoc | source]