001/* 002 * HA-JDBC: High-Availability JDBC 003 * Copyright (c) 2004-2007 Paul Ferraro 004 * 005 * This library is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU Lesser General Public License as published by the 007 * Free Software Foundation; either version 2.1 of the License, or (at your 008 * option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, but WITHOUT 011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 013 * for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public License 016 * along with this library; if not, write to the Free Software Foundation, 017 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018 * 019 * Contact: ferraro@users.sourceforge.net 020 */ 021package net.sf.hajdbc.dialect; 022 023import net.sf.hajdbc.Dialect; 024import net.sf.hajdbc.util.ClassEnum; 025import net.sf.hajdbc.util.Enums; 026 027/** 028 * @author Paul Ferraro 029 * @since 1.1 030 */ 031public enum DialectClass implements ClassEnum<Dialect> 032{ 033 DB2(DB2Dialect.class), 034 DERBY(DerbyDialect.class), 035 FIREBIRD(FirebirdDialect.class), 036 H2(H2Dialect.class), 037 HSQLDB(HSQLDBDialect.class), 038 INGRES(IngresDialect.class), 039 MAXDB(MaxDBDialect.class), 040 MCKOI(MckoiDialect.class), 041 MYSQL(MySQLDialect.class), 042 ORACLE(OracleDialect.class), 043 POSTGRESQL(PostgreSQLDialect.class), 044 STANDARD(StandardDialect.class), 045 SYBASE(SybaseDialect.class); 046 047 private Class<? extends Dialect> dialectClass; 048 049 private DialectClass(Class<? extends Dialect> dialectClass) 050 { 051 this.dialectClass = dialectClass; 052 } 053 054 /** 055 * @see net.sf.hajdbc.util.ClassEnum#isInstance(java.lang.Object) 056 */ 057 @Override 058 public boolean isInstance(Dialect dialect) 059 { 060 return this.dialectClass.equals(dialect.getClass()); 061 } 062 063 /** 064 * @see net.sf.hajdbc.util.ClassEnum#newInstance() 065 */ 066 @Override 067 public Dialect newInstance() throws Exception 068 { 069 return this.dialectClass.newInstance(); 070 } 071 072 /** 073 * Creates a new instance of the Dialect implementation from the specified class name. 074 * @param id the class name of a Dialect instance. 075 * @return a new Dialect instance 076 * @throws Exception if a Dialect instance could not be instantiated from the specified class name. 077 */ 078 public static Dialect deserialize(String id) throws Exception 079 { 080 try 081 { 082 DialectClass dialectClass = (id != null) ? Enums.valueOf(DialectClass.class, id) : STANDARD; 083 084 return dialectClass.newInstance(); 085 } 086 catch (IllegalArgumentException e) 087 { 088 return Class.forName(id).asSubclass(Dialect.class).newInstance(); 089 } 090 } 091 092 /** 093 * Return a String representation that identifies the specified Dialect. 094 * @param dialect a Dialect implementation 095 * @return the class name of this dialect 096 */ 097 public static String serialize(Dialect dialect) 098 { 099 for (DialectClass dialectClass: DialectClass.values()) 100 { 101 if (dialectClass.isInstance(dialect)) 102 { 103 return Enums.id(dialectClass); 104 } 105 } 106 107 return dialect.getClass().getName(); 108 } 109}