001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2012  Paul Ferraro
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package net.sf.hajdbc;
019
020import net.sf.hajdbc.durability.Durability.Phase;
021
022
023/**
024 * @author paul
025 *
026 */
027public abstract class AbstractExceptionFactory<E extends Exception> implements ExceptionFactory<E>
028{
029        private static final long serialVersionUID = 6715631233424287636L;
030
031        private Class<E> targetClass;
032        
033        protected AbstractExceptionFactory(Class<E> targetClass)
034        {
035                this.targetClass = targetClass;
036        }
037
038        @Override
039        public Class<E> getTargetClass()
040        {
041                return this.targetClass;
042        }
043
044        @Override
045        public ExceptionType getType()
046        {
047                return ExceptionType.valueOf(this.targetClass);
048        }
049
050        /**
051         * {@inheritDoc}
052         * @see net.sf.hajdbc.ExceptionFactory#createException(java.lang.Throwable)
053         */
054        @Override
055        public E createException(Throwable e)
056        {
057                if (this.targetClass.isInstance(e))
058                {
059                        return this.targetClass.cast(e);
060                }
061                
062                E exception = this.createException(e.getMessage());
063                
064                exception.initCause(e);
065                
066                return exception;
067        }
068
069        @Override
070        public boolean correctHeuristic(E exception, Phase phase)
071        {
072                return false;
073        }
074
075        @Override
076        public boolean equals(E exception1, E exception2)
077        {
078                String message1 = exception1.getMessage();
079                String message2 = exception2.getMessage();
080                
081                return ((message1 != null) && (message2 != null)) ? message1.equals(message2) : message1 == message2;
082        }
083}