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;
022
023/**
024 * Base class for exception thrown by the framework whenever it
025 * encounters a problem related to JMS.
026 *
027 * @author Mark Pollack
028 * @author Juergen Hoeller
029 * @since 1.1
030 */
031@SuppressWarnings("serial")
032public abstract class JmsException extends NestedRuntimeException {
033
034        /**
035         * Constructor that takes a message.
036         * @param msg the detail message
037         */
038        public JmsException(String msg) {
039                super(msg);
040        }
041
042        /**
043         * Constructor that takes a message and a root cause.
044         * @param msg the detail message
045         * @param cause the cause of the exception. This argument is generally
046         * expected to be a proper subclass of {@link javax.jms.JMSException},
047         * but can also be a JNDI NamingException or the like.
048         */
049        public JmsException(String msg, Throwable cause) {
050                super(msg, cause);
051        }
052
053        /**
054         * Constructor that takes a plain root cause, intended for
055         * subclasses mirroring corresponding {@code javax.jms} exceptions.
056         * @param cause the cause of the exception. This argument is generally
057         * expected to be a proper subclass of {@link javax.jms.JMSException}.
058         */
059        public JmsException(Throwable cause) {
060                super(cause != null ? cause.getMessage() : null, cause);
061        }
062
063
064        /**
065         * Convenience method to get the vendor specific error code if
066         * the root cause was an instance of JMSException.
067         * @return a string specifying the vendor-specific error code if the
068         * root cause is an instance of JMSException, or {@code null}
069         */
070        public String getErrorCode() {
071                Throwable cause = getCause();
072                if (cause instanceof JMSException) {
073                        return ((JMSException) cause).getErrorCode();
074                }
075                return null;
076        }
077
078        /**
079         * Return the detail message, including the message from the linked exception
080         * if there is one.
081         * @see javax.jms.JMSException#getLinkedException()
082         */
083        @Override
084        public String getMessage() {
085                String message = super.getMessage();
086                Throwable cause = getCause();
087                if (cause instanceof JMSException) {
088                        Exception linkedEx = ((JMSException) cause).getLinkedException();
089                        if (linkedEx != null) {
090                                String linkedMessage = linkedEx.getMessage();
091                                String causeMessage = cause.getMessage();
092                                if (linkedMessage != null && (causeMessage == null || !causeMessage.contains(linkedMessage))) {
093                                        message = message + "; nested exception is " + linkedEx;
094                                }
095                        }
096                }
097                return message;
098        }
099
100}