/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/
package org.codehaus.activemq.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.jms.JMSException;
import java.io.IOException;

/**
 * A helper class for creating new JMS exceptions
 *
 * @version $Revision: 1.1 $
 */
public class JMSExceptionHelper {

    private static final Log log = LogFactory.getLog(JMSExceptionHelper.class);

    public static JMSException newJMSException(String message, Throwable cause) {
        if (cause instanceof Exception) {
            return newJMSException(message, (Exception) cause);
        }
        else {
            // lets wrap in an Exception
            return newJMSException(message, new Exception(cause));
        }
    }

    public static JMSException newJMSException(String message, Exception cause) {
        // we should turn off warn level logging other than when debugging etc
        log.trace(message, cause);
        //log.warn(message, cause);
        JMSException jmsEx = new JMSException(message);
        jmsEx.setLinkedException(cause);
        jmsEx.initCause(cause);
        return jmsEx;
    }

    public static JMSException newJMSException(Throwable e) {
        if (e instanceof JMSException) {
            return (JMSException) e;
        }
        else {
            return newJMSException(e.getMessage(), e);
        }
    }

    public static IOException newIOException(JMSException e) {
        IOException answer = new IOException(e.getMessage());
        answer.setStackTrace(e.getStackTrace());
        return answer;
    }

}
