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.MessageEOFException;
021import javax.jms.MessageFormatException;
022
023public final class JMSExceptionSupport {
024
025    private JMSExceptionSupport() {
026    }
027
028    public static JMSException create(String msg, Throwable cause) {
029        JMSException exception = new JMSException(msg);
030        exception.initCause(cause);
031        return exception;
032    }
033
034    public static JMSException create(String msg, Exception cause) {
035        JMSException exception = new JMSException(msg);
036        exception.setLinkedException(cause);
037        exception.initCause(cause);
038        return exception;
039    }
040
041    public static JMSException create(Throwable cause) {
042        if (cause instanceof JMSException) {
043            return (JMSException)cause;
044        }
045        String msg = cause.getMessage();
046        if (msg == null || msg.length() == 0) {
047            msg = cause.toString();
048        }
049        JMSException exception = new JMSException(msg);
050        exception.initCause(cause);
051        return exception;
052    }
053
054    public static JMSException create(Exception cause) {
055        if (cause instanceof JMSException) {
056            return (JMSException)cause;
057        }
058        String msg = cause.getMessage();
059        if (msg == null || msg.length() == 0) {
060            msg = cause.toString();
061        }
062        JMSException exception = new JMSException(msg);
063        exception.setLinkedException(cause);
064        exception.initCause(cause);
065        return exception;
066    }
067
068    public static MessageEOFException createMessageEOFException(Exception cause) {
069        String msg = cause.getMessage();
070        if (msg == null || msg.length() == 0) {
071            msg = cause.toString();
072        }
073        MessageEOFException exception = new MessageEOFException(msg);
074        exception.setLinkedException(cause);
075        exception.initCause(cause);
076        return exception;
077    }
078
079    public static MessageFormatException createMessageFormatException(Exception cause) {
080        String msg = cause.getMessage();
081        if (msg == null || msg.length() == 0) {
082            msg = cause.toString();
083        }
084        MessageFormatException exception = new MessageFormatException(msg);
085        exception.setLinkedException(cause);
086        exception.initCause(cause);
087        return exception;
088    }
089}