KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > dialect > DialectFactory


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 Paul Ferraro
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at your
8  * option) any later version.
9  *
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 java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import net.sf.hajdbc.Dialect;
27
28 /**
29  * @author Paul Ferraro
30  * @since 1.1
31  */

32 public class DialectFactory
33 {
34     private static Map JavaDoc<String JavaDoc, Class JavaDoc<? extends Dialect>> dialectMap = new HashMap JavaDoc<String JavaDoc, Class JavaDoc<? extends Dialect>>();
35     
36     static
37     {
38         dialectMap.put("default", DefaultDialect.class);
39         dialectMap.put("db2", DB2Dialect.class);
40         dialectMap.put("derby", DerbyDialect.class);
41         dialectMap.put("firebird", DefaultDialect.class);
42         dialectMap.put("hsqldb", HSQLDBDialect.class);
43         dialectMap.put("ingres", DefaultDialect.class);
44         dialectMap.put("maxdb", MaxDBDialect.class);
45         dialectMap.put("mckoi", DefaultDialect.class);
46         dialectMap.put("mysql", MySQLDialect.class);
47         dialectMap.put("oracle", MaxDBDialect.class);
48         dialectMap.put("postgresql", PostgreSQLDialect.class);
49     }
50     
51     /**
52      * Creates a new instance of the Dialect implementation from the specified class name.
53      * @param id the class name of a Dialect instance.
54      * @return a new Dialect instance
55      * @throws Exception if a Dialect instance could not be instantiated from the specified class name.
56      */

57     public static Dialect deserialize(String JavaDoc id) throws Exception JavaDoc
58     {
59         Class JavaDoc<? extends Dialect> targetClass = (id != null) ? dialectMap.get(id.toLowerCase()) : DefaultDialect.class;
60         
61         if (targetClass == null)
62         {
63             targetClass = Class.forName(id).asSubclass(Dialect.class);
64         }
65         
66         return targetClass.newInstance();
67     }
68     
69     /**
70      * Return a String representation that identifies the specified Dialect.
71      * @param dialect a Dialect implementation
72      * @return the class name of this dialect
73      */

74     public static String JavaDoc serialize(Dialect dialect)
75     {
76         return dialect.getClass().getName();
77     }
78     
79     private DialectFactory()
80     {
81         // Hide constructor
82
}
83 }
84
Popular Tags