KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > jdbc > typing > DBMSInfoMap


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  * Copyright (C) 2003 XQuark Group.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
19  * You can also get it at http://www.gnu.org/licenses/lgpl.html
20  *
21  * For more information on this software, see http://www.xquark.org.
22  */

23
24 package org.xquark.jdbc.typing;
25
26 import java.net.URL JavaDoc;
27 import java.sql.Connection JavaDoc;
28 import java.sql.DatabaseMetaData JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32
33
34 /**
35  * AbstractConnection factory.
36  *
37  * <p>This object holds database vendor specificities routing.</p>
38  */

39 public class DBMSInfoMap implements DBMSConstants
40 {
41     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
42     private static final String JavaDoc RCSName = "$Name: $";
43
44     ///////////////////////////////////////////////////////////////
45
// Constants
46
///////////////////////////////////////////////////////////////
47
private static String JavaDoc RESOURCES_FOLDER = "resources/";
48     
49     ///////////////////////////////////////////////////////////////
50
// Data
51
///////////////////////////////////////////////////////////////
52
private static DBMSInfoLoader dbInfoLoader;
53     private static Map JavaDoc dbmsInfos;
54     
55     static
56     {
57         dbInfoLoader = new DBMSInfoLoader();
58         dbmsInfos = new HashMap JavaDoc();
59     }
60     
61     private DBMSInfoMap() {}
62     
63     ///////////////////////////////////////////////////////////////
64
// Public Methods
65
///////////////////////////////////////////////////////////////
66
public static synchronized DBMSInfo getDBMSInfo(short DBMS)
67     {
68         Short JavaDoc key = new Short JavaDoc(DBMS);
69         DBMSInfo ret = (DBMSInfo) dbmsInfos.get(key);
70         if (ret == null)
71         {
72             String JavaDoc filename;
73             switch (DBMS)
74             {
75                 case ORACLE8I : case ORACLE8 : case ORACLE :
76                     filename = ORACLE_FILE;
77                     break;
78                 case TIMESTEN40 : case TIMESTEN :
79                     filename = TIMESTEN_FILE;
80                     break;
81                 case SYBASE_ASE_11_9 : case SYBASE :
82                     filename = SYBASE_FILE;
83                     break;
84                 case SQL_SERVER_2000 : case SQL_SERVER :
85                     filename = SQL_SERVER_FILE;
86                     break;
87                 case MYSQL : case MYSQL323 :
88                     filename = MYSQL_FILE;
89                     break;
90                 default :
91                     throw new RuntimeException JavaDoc("Your RDBMS is not supported in the current version of XQuark products (basing on your JDBC driver informations).");
92             }
93             URL JavaDoc specsURL = DBMSInfoMap.class.getResource(RESOURCES_FOLDER + filename);
94             ret = (DBMSInfo) dbInfoLoader.fromXML(specsURL.toString());
95             ret.setDbms(DBMS);
96             dbmsInfos.put(key, ret);
97         }
98         return ret;
99     }
100
101     public static synchronized DBMSInfo getDBMSInfo(Connection JavaDoc conn) throws SQLException JavaDoc
102     {
103         return getDBMSInfo(getDatabaseVendor(conn));
104     }
105     
106     /** Set the database type variable according to JDBC metadata
107      */

108     public static short getDatabaseVendor(Connection JavaDoc conn) throws SQLException JavaDoc
109     {
110         short databaseVendor = -1;
111         DatabaseMetaData JavaDoc dbmeta = conn.getMetaData();
112         String JavaDoc productName = dbmeta.getDatabaseProductName();
113         String JavaDoc version = dbmeta.getDatabaseProductVersion();
114         
115         if (productName.equalsIgnoreCase("TimesTen")) // TIMESTEN
116
{
117             if (version.startsWith("04.00"))
118                 databaseVendor = TIMESTEN40;
119             else
120                 databaseVendor = TIMESTEN;
121         }
122         else if (productName.equalsIgnoreCase("Oracle")) // ORACLE
123
{
124             if (version.startsWith("Oracle8i") || version.startsWith("Personal Oracle8i"))
125                 databaseVendor = ORACLE8I;
126             else if (version.startsWith("Oracle8"))
127                 databaseVendor = ORACLE8;
128             else
129                 databaseVendor = ORACLE;
130         }
131         else if (productName.equalsIgnoreCase("MYSQL")) // ORACLE
132
{
133             if (version.startsWith("3.23"))
134                 databaseVendor = MYSQL323;
135             else
136                 databaseVendor = MYSQL;
137         }
138         else if (productName.equalsIgnoreCase("Adaptive Server Enterprise")) // Sybase
139
{
140             if (version.startsWith("Adaptive Server Enterprise/11.9"))
141                 databaseVendor = SYBASE_ASE_11_9;
142             else
143                 databaseVendor = SYBASE;
144         }
145         else if (productName.equalsIgnoreCase("Microsoft SQL Server"))
146         {
147             if (version.indexOf("2000") >= 0)
148                 databaseVendor = SQL_SERVER_2000;
149             else
150                 databaseVendor = SQL_SERVER;
151         }
152         else if (isJDBC2(conn))
153             databaseVendor = JDBC2;
154         else
155             databaseVendor = JDBC1;
156         return databaseVendor;
157     }
158
159     private static boolean isJDBC2(Connection JavaDoc conn) throws SQLException JavaDoc
160     {
161         try {
162             conn.getTypeMap();
163         }
164         catch (AbstractMethodError JavaDoc e) {
165             return false;
166         }
167         return true;
168     }
169 }
170
Popular Tags