KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > jdbc > GenericDriver


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.jdbc;
17
18 import scriptella.spi.AbstractScriptellaDriver;
19 import scriptella.spi.ConnectionParameters;
20
21 import java.io.PrintWriter JavaDoc;
22 import java.sql.Connection JavaDoc;
23 import java.sql.DriverManager JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 /**
30  * Adapter for JDBC drivers.
31  *
32  * @author Fyodor Kupolov
33  * @version 1.0
34  */

35 public class GenericDriver extends AbstractScriptellaDriver {
36
37     private static final Logger JavaDoc LOG = Logger.getLogger(GenericDriver.class.getName());
38
39     static {
40         //Redirects DriverManager's logging
41
final Logger JavaDoc LOG = Logger.getLogger("scriptella.DriverManagerLog");
42         if (LOG.isLoggable(Level.FINE)) {
43             if (DriverManager.getLogWriter() == null) {
44                 DriverManager.setLogWriter(new PrintWriter JavaDoc(System.out) {
45                     public void println(String JavaDoc s) {
46                         LOG.fine(s);
47                     }
48                 });
49
50             }
51         }
52     }
53
54     /**
55      * Tries to load one of the specified driver class names.
56      *
57      * @param drivers database driver candidate names.
58      * @throws JdbcException if no drivers were loaded
59      */

60     protected void loadDrivers(String JavaDoc... drivers) {
61         if (drivers.length > 0) {
62             Throwable JavaDoc throwable = null;
63             for (String JavaDoc name : drivers) {
64                 if (LOG.isLoggable(Level.FINE)) {
65                     LOG.fine("Trying to load driver class " + name);
66                 }
67                 try {
68                     Class.forName(name);
69                     break;
70                 } catch (Throwable JavaDoc t) {
71                     if (throwable == null) {
72                         throwable = t;
73                     } else {
74                         if (LOG.isLoggable(Level.FINE)) {
75                             LOG.log(Level.FINE, "Failed to load driver class " + name, t);
76                         }
77                     }
78                 }
79             }
80             if (throwable != null) {
81                 throw new JdbcException("Couldn't find appropriate jdbc driver : " + drivers[0] +
82                         ". Please check class path settings", throwable);
83             }
84         }
85     }
86
87     public JdbcConnection connect(ConnectionParameters params) {
88         try {
89             Properties JavaDoc props = new Properties JavaDoc();
90             props.putAll(params.getProperties());
91             //according to JDBC spec
92
if (params.getUser() != null) {
93                 props.put("user", params.getUser());
94             }
95             if (params.getPassword() != null) {
96                 props.put("password", params.getPassword());
97             }
98             return connect(params, props);
99
100         } catch (SQLException JavaDoc e) {
101             throw new JdbcException("Unable to obtain connection for URL " + params.getUrl(), e);
102         }
103     }
104
105
106     /**
107      * Creates Scriptella JDBC connection.
108      *
109      * @param parameters connection parameters
110      * @param props properties to pass to jdbc driver
111      * @return Scriptella JDBC connection.
112      * @throws SQLException if DB exception occurs.
113      */

114     protected JdbcConnection connect(ConnectionParameters parameters, Properties JavaDoc props) throws SQLException JavaDoc {
115         return new JdbcConnection(getConnection(parameters.getUrl(), props), parameters);
116     }
117
118     /**
119      * A helper method for subclasses to avoid direct interaction with DriverManager API.
120      * <p>Calls {@link DriverManager#getConnection(String,java.util.Properties)}
121      */

122     protected Connection JavaDoc getConnection(String JavaDoc url, Properties JavaDoc props) throws SQLException JavaDoc {
123         return DriverManager.getConnection(url, props);
124     }
125
126
127 }
128
Popular Tags