01: /*
02: * HA-JDBC: High-Availability JDBC
03: * Copyright (c) 2004-2007 Paul Ferraro
04: *
05: * This library is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU Lesser General Public License as published by the
07: * Free Software Foundation; either version 2.1 of the License, or (at your
08: * option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful, but WITHOUT
11: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13: * for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public License
16: * along with this library; if not, write to the Free Software Foundation,
17: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: *
19: * Contact: ferraro@users.sourceforge.net
20: */
21: package net.sf.hajdbc.dialect;
22:
23: import net.sf.hajdbc.Dialect;
24: import net.sf.hajdbc.util.ClassEnum;
25:
26: /**
27: * @author Paul Ferraro
28: * @since 1.1
29: */
30: public enum DialectClass implements ClassEnum<Dialect> {
31: DB2(DB2Dialect.class), DERBY(DerbyDialect.class), FIREBIRD(
32: FirebirdDialect.class), H2(H2Dialect.class), HSQLDB(
33: HSQLDBDialect.class), INGRES(IngresDialect.class), MAXDB(
34: MaxDBDialect.class), MCKOI(MckoiDialect.class), MYSQL(
35: MySQLDialect.class), ORACLE(OracleDialect.class), POSTGRESQL(
36: PostgreSQLDialect.class), STANDARD(StandardDialect.class), SYBASE(
37: SybaseDialect.class);
38:
39: private Class<? extends Dialect> dialectClass;
40:
41: private DialectClass(Class<? extends Dialect> dialectClass) {
42: this .dialectClass = dialectClass;
43: }
44:
45: /**
46: * @see net.sf.hajdbc.util.ClassEnum#isInstance(java.lang.Object)
47: */
48: @Override
49: public boolean isInstance(Dialect dialect) {
50: return this .dialectClass.equals(dialect.getClass());
51: }
52:
53: /**
54: * @see net.sf.hajdbc.util.ClassEnum#newInstance()
55: */
56: @Override
57: public Dialect newInstance() throws Exception {
58: return this .dialectClass.newInstance();
59: }
60:
61: /**
62: * Creates a new instance of the Dialect implementation from the specified class name.
63: * @param id the class name of a Dialect instance.
64: * @return a new Dialect instance
65: * @throws Exception if a Dialect instance could not be instantiated from the specified class name.
66: */
67: public static Dialect deserialize(String id) throws Exception {
68: try {
69: DialectClass dialectClass = (id != null) ? DialectClass
70: .valueOf(id.toUpperCase()) : STANDARD;
71:
72: return dialectClass.newInstance();
73: } catch (IllegalArgumentException e) {
74: return Class.forName(id).asSubclass(Dialect.class)
75: .newInstance();
76: }
77: }
78:
79: /**
80: * Return a String representation that identifies the specified Dialect.
81: * @param dialect a Dialect implementation
82: * @return the class name of this dialect
83: */
84: public static String serialize(Dialect dialect) {
85: for (DialectClass dialectClass : DialectClass.values()) {
86: if (dialectClass.isInstance(dialect)) {
87: return dialectClass.name().toLowerCase();
88: }
89: }
90:
91: return dialect.getClass().getName();
92: }
93: }
|