001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.jms; 018 019import javax.jms.JMSException; 020 021import org.springframework.core.NestedRuntimeException; 022import org.springframework.lang.Nullable; 023 024/** 025 * Base class for exception thrown by the framework whenever it 026 * encounters a problem related to JMS. 027 * 028 * @author Mark Pollack 029 * @author Juergen Hoeller 030 * @since 1.1 031 */ 032@SuppressWarnings("serial") 033public abstract class JmsException extends NestedRuntimeException { 034 035 /** 036 * Constructor that takes a message. 037 * @param msg the detail message 038 */ 039 public JmsException(String msg) { 040 super(msg); 041 } 042 043 /** 044 * Constructor that takes a message and a root cause. 045 * @param msg the detail message 046 * @param cause the cause of the exception. This argument is generally 047 * expected to be a proper subclass of {@link javax.jms.JMSException}, 048 * but can also be a JNDI NamingException or the like. 049 */ 050 public JmsException(String msg, @Nullable Throwable cause) { 051 super(msg, cause); 052 } 053 054 /** 055 * Constructor that takes a plain root cause, intended for 056 * subclasses mirroring corresponding {@code javax.jms} exceptions. 057 * @param cause the cause of the exception. This argument is generally 058 * expected to be a proper subclass of {@link javax.jms.JMSException}. 059 */ 060 public JmsException(@Nullable Throwable cause) { 061 super(cause != null ? cause.getMessage() : null, cause); 062 } 063 064 065 /** 066 * Convenience method to get the vendor specific error code if 067 * the root cause was an instance of JMSException. 068 * @return a string specifying the vendor-specific error code if the 069 * root cause is an instance of JMSException, or {@code null} 070 */ 071 @Nullable 072 public String getErrorCode() { 073 Throwable cause = getCause(); 074 if (cause instanceof JMSException) { 075 return ((JMSException) cause).getErrorCode(); 076 } 077 return null; 078 } 079 080 /** 081 * Return the detail message, including the message from the linked exception 082 * if there is one. 083 * @see javax.jms.JMSException#getLinkedException() 084 */ 085 @Override 086 @Nullable 087 public String getMessage() { 088 String message = super.getMessage(); 089 Throwable cause = getCause(); 090 if (cause instanceof JMSException) { 091 Exception linkedEx = ((JMSException) cause).getLinkedException(); 092 if (linkedEx != null) { 093 String linkedMessage = linkedEx.getMessage(); 094 String causeMessage = cause.getMessage(); 095 if (linkedMessage != null && (causeMessage == null || !causeMessage.contains(linkedMessage))) { 096 message = message + "; nested exception is " + linkedEx; 097 } 098 } 099 } 100 return message; 101 } 102 103}