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