AMQException.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid;
022 
023 import javax.management.JMException;
024 
025 import org.apache.qpid.protocol.AMQConstant;
026 
027 /**
028  * AMQException forms the root exception of all exceptions relating to the AMQ protocol. It provides space to associate
029  * a required AMQ error code with the exception, which is a numeric value, with a meaning defined by the protocol.
030  *
031  <p/><table id="crc"><caption>CRC Card</caption>
032  <tr><th> Responsibilities <th> Collaborations
033  <tr><td> Represents an exception condition associated with an AMQ protocol status code.
034  </table>
035  *
036  * @todo This exception class is also used as a generic exception throughout Qpid code. This usage may not be strictly
037  *       correct if this is to signify a protocol exception. Should review.
038  */
039 public class AMQException extends Exception
040 {
041     /** Holds the AMQ error code constant associated with this exception. */
042     private AMQConstant _errorCode;
043    
044     /**
045      * Creates an exception with an optional error code, optional message and optional underlying cause.
046      *
047      @param errorCode The error code. May be null if not to be set.
048      @param msg       The exception message. May be null if not to be set.
049      @param cause         The underlying cause of the exception. May be null if not to be set.
050      */
051     public AMQException(AMQConstant errorCode, String msg, Throwable cause)
052     {
053         super(((msg == null"" : msg((errorCode == null"" (" [error code " + errorCode + "]")), cause);
054         _errorCode = errorCode;
055     }
056 
057     /*
058      * Deprecated constructors brought from M2.1
059      */
060     @Deprecated
061     public AMQException (String msg
062     {
063         this(null, (msg == null"" : msg);
064     }
065     
066     @Deprecated 
067     public AMQException (AMQConstant errorCode, String msg
068     {
069         this(errorCode, (msg == null"" : msg, null);
070     }
071 
072     @Deprecated
073     public AMQException(String msg, Throwable cause)
074     {
075         this(null, msg, cause);
076     }
077 
078     
079     /**
080      * Gets the AMQ protocol exception code associated with this exception.
081      *
082      @return The AMQ protocol exception code associated with this exception.
083      */
084     public AMQConstant getErrorCode()
085     {
086         return _errorCode;
087     }
088 
089     public boolean isHardError()
090     {
091         return true;
092     }
093 
094     /**
095      * Rethrown this exception as a new exception.
096      *
097      * Attempt to create a new exception of the same class if they hav the default constructor of:
098      * {AMQConstant.class, String.class, Throwable.class}
099      *
100      * Individual subclasses may override as requried to create a new instance.
101      
102      */
103     public AMQException cloneForCurrentThread()
104     {
105         Class amqeClass = this.getClass();
106         Class<?>[] paramClasses = {AMQConstant.class, String.class, Throwable.class};
107         Object[] params = {getErrorCode(), getMessage()this};
108 
109         AMQException newAMQE;
110 
111         try
112         {
113             newAMQE = (AMQExceptionamqeClass.getConstructor(paramClasses).newInstance(params);
114         }
115         catch (Exception creationException)
116         {
117             newAMQE = new AMQException(getErrorCode(), getMessage()this);
118         }
119 
120         return newAMQE;
121     }
122 }