KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > jdbc > datasource > JDBCDataSourceFactory


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

22
23 package org.xquark.jdbc.datasource;
24
25 import java.sql.Driver JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import org.apache.commons.dbcp.SQLNestedException;
32
33 public class JDBCDataSourceFactory {
34     
35     /** List of supported JDBC drivers */
36     
37     private static final String JavaDoc[] DRIVER_LIST = {
38         "com.mysql.jdbc.Driver",
39         "com.microsoft.jdbc.sqlserver.SQLServerDriver",
40         "com.sybase.jdbc2.jdbc.SybDriver",
41         "oracle.jdbc.driver.OracleDriver"
42     };
43
44     private ClassLoader JavaDoc classLoader;
45     private List JavaDoc drivers;
46
47     private JDBCDataSourceFactory(ClassLoader JavaDoc cl) {
48         if (cl != null) classLoader = cl;
49         else classLoader = getClass().getClassLoader();
50         drivers = new ArrayList JavaDoc();
51         for (int i = 0; i < DRIVER_LIST.length; i++) {
52             try {
53                 Class JavaDoc c = Class.forName(DRIVER_LIST[i], true, classLoader);
54                 drivers.add(c.newInstance());
55             } catch (ClassNotFoundException JavaDoc e) {
56             } catch (IllegalAccessException JavaDoc e) {
57             } catch (InstantiationException JavaDoc e) {
58             }
59         }
60     }
61     
62     public static JDBCDataSourceFactory newInstance() {
63         return new JDBCDataSourceFactory(null);
64     }
65     
66     public static JDBCDataSourceFactory newInstance(ClassLoader JavaDoc loader) {
67         return new JDBCDataSourceFactory(loader);
68     }
69     
70     public JDBCDataSource newDataSource(String JavaDoc url, String JavaDoc user, String JavaDoc password)
71     throws SQLException JavaDoc
72     {
73         return newDataSource(null, url, user, password);
74     }
75
76     public JDBCDataSource newDataSource(String JavaDoc driverName, String JavaDoc url, String JavaDoc user, String JavaDoc password)
77     throws SQLException JavaDoc
78     {
79         Properties JavaDoc jdbcProperties = new Properties JavaDoc();
80         jdbcProperties.put("user", user);
81         jdbcProperties.put("password", password);
82         return createDataSource(driverName, url, jdbcProperties);
83     }
84
85     public JDBCDataSource newDataSource(String JavaDoc driverName, String JavaDoc url, Properties JavaDoc jdbcProperties)
86     throws SQLException JavaDoc
87     {
88         if (jdbcProperties.get("user") == null)
89             throw new SQLException JavaDoc("Missing user information in JDBC properties");
90         if (jdbcProperties.get("password") == null)
91             throw new SQLException JavaDoc("Missing password information in JDBC properties");
92         return createDataSource(driverName, url, jdbcProperties);
93     }
94     
95     private JDBCDataSource createDataSource(String JavaDoc driverName, String JavaDoc url, Properties JavaDoc jdbcProperties)
96     throws SQLException JavaDoc
97     {
98         Driver JavaDoc driver = null;
99         if (driverName != null) {
100             try {
101                 Class JavaDoc c = Class.forName(driverName, true, classLoader);
102                 Driver JavaDoc d = (Driver JavaDoc) c.newInstance();
103                 if (d.acceptsURL(url)) {
104                     driver = d;
105                 }
106             } catch (ClassNotFoundException JavaDoc e) {
107                 throw new SQLNestedException("Could not load driver "+driverName, e);
108             } catch (InstantiationException JavaDoc e) {
109                 throw new SQLNestedException("Could not create driver "+driverName, e);
110             } catch (IllegalAccessException JavaDoc e) {
111                 throw new SQLNestedException("Could not create driver "+driverName, e);
112             }
113         } else {
114                 for (int i = 0; driver == null && i < drivers.size(); i++) {
115                     Driver JavaDoc d = (Driver JavaDoc) drivers.get(i);
116                     if (d.acceptsURL(url)) {
117                         driver = d;
118                     }
119                 }
120         }
121         if (driver == null)
122             throw new SQLException JavaDoc("No suitable driver found for "+url);
123         return new JDBCDataSource(driver, url, jdbcProperties);
124     }
125
126 }
127
Popular Tags