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.sql.SQLExceptionFactory; 021import net.sf.hajdbc.sql.io.IOExceptionFactory; 022import net.sf.hajdbc.sql.xa.XAExceptionFactory; 023import net.sf.hajdbc.util.Matcher; 024 025/** 026 * @author Paul Ferraro 027 */ 028@SuppressWarnings("rawtypes") 029public enum ExceptionType implements Matcher<ExceptionFactory> 030{ 031 SQL(new SQLExceptionFactory()), 032 XA(new XAExceptionFactory()), 033 IO(new IOExceptionFactory()) 034 ; 035 private final ExceptionFactory<? extends Exception> factory; 036 037 private ExceptionType(ExceptionFactory<? extends Exception> factory) 038 { 039 this.factory = factory; 040 } 041 042 @Override 043 public boolean matches(ExceptionFactory factory) 044 { 045 return this == factory.getType(); 046 } 047 048 @SuppressWarnings("unchecked") 049 public <E extends Exception> ExceptionFactory<E> getExceptionFactory() 050 { 051 return (ExceptionFactory<E>) this.factory; 052 } 053 054 public static ExceptionType valueOf(Class<?> exceptionClass) 055 { 056 for (ExceptionType type: ExceptionType.values()) 057 { 058 if (type.factory.getTargetClass().equals(exceptionClass)) 059 { 060 return type; 061 } 062 } 063 064 throw new IllegalArgumentException(exceptionClass.getName()); 065 } 066}