KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > conn > DriverDataSource


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.conn;
21
22 import java.io.PrintWriter JavaDoc;
23 import java.sql.Connection JavaDoc;
24 import java.sql.Driver JavaDoc;
25 import java.sql.DriverManager JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 import javax.sql.DataSource JavaDoc;
30
31 import org.apache.cayenne.util.Util;
32
33 /**
34  * A non-pooling DataSource implementation wrapping a JDBC driver.
35  *
36  * @author Andrus Adamchik
37  */

38 public class DriverDataSource implements DataSource JavaDoc {
39
40     protected Driver JavaDoc driver;
41
42     protected String JavaDoc connectionUrl;
43     protected String JavaDoc userName;
44     protected String JavaDoc password;
45
46     protected ConnectionEventLoggingDelegate logger;
47
48     /**
49      * Loads JDBC driver using current thread class loader.
50      *
51      * @since 3.0
52      */

53     private static Driver JavaDoc loadDriver(String JavaDoc driverClassName) throws SQLException JavaDoc {
54
55         Class JavaDoc driverClass;
56         try {
57             driverClass = Class.forName(driverClassName, true, Thread
58                     .currentThread()
59                     .getContextClassLoader());
60         }
61         catch (Exception JavaDoc ex) {
62             throw new SQLException JavaDoc("Can not load JDBC driver named '"
63                     + driverClassName
64                     + "': "
65                     + ex.getMessage());
66         }
67
68         try {
69             return (Driver JavaDoc) driverClass.newInstance();
70         }
71         catch (Exception JavaDoc ex) {
72             throw new SQLException JavaDoc("Error instantiating driver '"
73                     + driverClassName
74                     + "': "
75                     + ex.getMessage());
76         }
77     }
78
79     /**
80      * Creates a new DriverDataSource.
81      */

82     public DriverDataSource(String JavaDoc driverClassName, String JavaDoc connectionUrl)
83             throws SQLException JavaDoc {
84         this(driverClassName, connectionUrl, null, null);
85     }
86
87     /**
88      * @since 3.0
89      */

90     public DriverDataSource(String JavaDoc driverClassName, String JavaDoc connectionUrl,
91             String JavaDoc userName, String JavaDoc password) throws SQLException JavaDoc {
92
93         setDriverClassName(driverClassName);
94
95         this.connectionUrl = connectionUrl;
96         this.userName = userName;
97         this.password = password;
98     }
99
100     /**
101      * Creates a new DriverDataSource wrapping a given Driver.
102      *
103      * @since 1.1
104      */

105     public DriverDataSource(Driver JavaDoc driver, String JavaDoc connectionUrl, String JavaDoc userName,
106             String JavaDoc password) {
107
108         this.driver = driver;
109         this.connectionUrl = connectionUrl;
110         this.userName = userName;
111         this.password = password;
112     }
113
114     /**
115      * Returns a new database connection, using preconfigured data to locate the database
116      * and obtain a connection.
117      */

118     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
119         // login with internal credentials
120
return getConnection(userName, password);
121     }
122
123     /**
124      * Returns a new database connection using provided credentials to login to the
125      * database.
126      */

127     public Connection JavaDoc getConnection(String JavaDoc userName, String JavaDoc password) throws SQLException JavaDoc {
128         try {
129             if (logger != null) {
130                 logger.logConnect(connectionUrl, userName, password);
131             }
132
133             Connection JavaDoc c = null;
134
135             if (driver == null) {
136                 c = DriverManager.getConnection(connectionUrl, userName, password);
137             }
138             else {
139                 Properties JavaDoc connectProperties = new Properties JavaDoc();
140
141                 if (userName != null) {
142                     connectProperties.put("user", userName);
143                 }
144
145                 if (password != null) {
146                     connectProperties.put("password", password);
147                 }
148                 c = driver.connect(connectionUrl, connectProperties);
149             }
150
151             // some drivers (Oracle) return null connections instead of throwing
152
// an exception... fix it here
153

154             if (c == null) {
155                 throw new SQLException JavaDoc("Can't establish connection: " + connectionUrl);
156             }
157
158             if (logger != null) {
159                 logger.logConnectSuccess();
160             }
161
162             return c;
163         }
164         catch (SQLException JavaDoc sqlex) {
165             if (logger != null) {
166                 logger.logConnectFailure(sqlex);
167             }
168
169             throw sqlex;
170         }
171     }
172
173     public int getLoginTimeout() throws SQLException JavaDoc {
174         return -1;
175     }
176
177     public void setLoginTimeout(int seconds) throws SQLException JavaDoc {
178         // noop
179
}
180
181     public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc {
182         return DriverManager.getLogWriter();
183     }
184
185     public void setLogWriter(PrintWriter JavaDoc out) throws SQLException JavaDoc {
186         DriverManager.setLogWriter(out);
187     }
188
189     public ConnectionEventLoggingDelegate getLogger() {
190         return logger;
191     }
192
193     public void setLogger(ConnectionEventLoggingDelegate delegate) {
194         logger = delegate;
195     }
196
197     /**
198      * @since 3.0
199      */

200     public String JavaDoc getConnectionUrl() {
201         return connectionUrl;
202     }
203
204     /**
205      * @since 3.0
206      */

207     public void setConnectionUrl(String JavaDoc connectionUrl) {
208         this.connectionUrl = connectionUrl;
209     }
210
211     /**
212      * @since 3.0
213      */

214     public String JavaDoc getPassword() {
215         return password;
216     }
217
218     /**
219      * @since 3.0
220      */

221     public void setPassword(String JavaDoc password) {
222         this.password = password;
223     }
224
225     /**
226      * @since 3.0
227      */

228     public String JavaDoc getUserName() {
229         return userName;
230     }
231
232     /**
233      * @since 3.0
234      */

235     public void setUserName(String JavaDoc userName) {
236         this.userName = userName;
237     }
238
239     public String JavaDoc getDriverClassName() {
240         return driver != null ? driver.getClass().getName() : null;
241     }
242
243     public void setDriverClassName(String JavaDoc driverClassName) throws SQLException JavaDoc {
244         if (!Util.nullSafeEquals(getDriverClassName(), driverClassName)) {
245             this.driver = driverClassName != null ? loadDriver(driverClassName) : null;
246         }
247     }
248 }
249
Popular Tags