001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.util;
018
019import javax.jms.JMSException;
020import javax.jms.JMSSecurityException;
021import javax.jms.MessageEOFException;
022import javax.jms.MessageFormatException;
023
024import org.apache.activemq.MaxFrameSizeExceededException;
025
026public final class JMSExceptionSupport {
027
028    private JMSExceptionSupport() {
029    }
030
031    public static JMSException create(String msg, Throwable cause) {
032        JMSException exception = new JMSException(msg);
033        exception.initCause(cause);
034        return exception;
035    }
036
037    public static JMSException create(String msg, Exception cause) {
038        JMSException exception = new JMSException(msg);
039        exception.setLinkedException(cause);
040        exception.initCause(cause);
041        return exception;
042    }
043
044    public static JMSException create(Throwable cause) {
045        if (cause instanceof JMSException) {
046            return (JMSException)cause;
047        }
048        String msg = cause.getMessage();
049        if (msg == null || msg.length() == 0) {
050            msg = cause.toString();
051        }
052        JMSException exception;
053        if (cause instanceof SecurityException) {
054            exception = new JMSSecurityException(msg);
055        } else {
056            exception = new JMSException(msg);
057        }
058        exception.initCause(cause);
059        return exception;
060    }
061
062    public static JMSException create(Exception cause) {
063        if (cause instanceof JMSException) {
064            return (JMSException)cause;
065        }
066        if (cause instanceof MaxFrameSizeExceededException) {
067            JMSException jmsException = new JMSException(cause.getMessage(), "41300");
068            jmsException.setLinkedException(cause);
069            jmsException.initCause(cause);
070            return jmsException;
071        }
072        String msg = cause.getMessage();
073        if (msg == null || msg.length() == 0) {
074            msg = cause.toString();
075        }
076        JMSException exception;
077        if (cause instanceof SecurityException) {
078            exception = new JMSSecurityException(msg);
079        } else {
080            exception = new JMSException(msg);
081        }
082        exception.setLinkedException(cause);
083        exception.initCause(cause);
084        return exception;
085    }
086
087    public static MessageEOFException createMessageEOFException(Exception cause) {
088        String msg = cause.getMessage();
089        if (msg == null || msg.length() == 0) {
090            msg = cause.toString();
091        }
092        MessageEOFException exception = new MessageEOFException(msg);
093        exception.setLinkedException(cause);
094        exception.initCause(cause);
095        return exception;
096    }
097
098    public static MessageFormatException createMessageFormatException(Exception cause) {
099        String msg = cause.getMessage();
100        if (msg == null || msg.length() == 0) {
101            msg = cause.toString();
102        }
103        MessageFormatException exception = new MessageFormatException(msg);
104        exception.setLinkedException(cause);
105        exception.initCause(cause);
106        return exception;
107    }
108}