cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r531171 - /cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/conn/DriverDataSource.java
Date Sun, 22 Apr 2007 12:41:46 GMT
Author: aadamchik
Date: Sun Apr 22 05:41:45 2007
New Revision: 531171

URL: http://svn.apache.org/viewvc?view=rev&rev=531171
Log:
CAY-779: Improve usability of non-pooling DriverDataSource for uses outside Cayenne

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/conn/DriverDataSource.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/conn/DriverDataSource.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/conn/DriverDataSource.java?view=diff&rev=531171&r1=531170&r2=531171
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/conn/DriverDataSource.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/conn/DriverDataSource.java
Sun Apr 22 05:41:45 2007
@@ -17,7 +17,6 @@
  *  under the License.
  ****************************************************************/
 
-
 package org.apache.cayenne.conn;
 
 import java.io.PrintWriter;
@@ -29,8 +28,10 @@
 
 import javax.sql.DataSource;
 
+import org.apache.cayenne.util.Util;
+
 /**
- * A DataSource implementation wrapping a JDBC driver.
+ * A non-pooling DataSource implementation wrapping a JDBC driver.
  * 
  * @author Andrus Adamchik
  */
@@ -45,24 +46,55 @@
     protected ConnectionEventLoggingDelegate logger;
 
     /**
+     * Loads JDBC driver using current thread class loader.
+     * 
+     * @since 3.0
+     */
+    private static Driver loadDriver(String driverClassName) throws SQLException {
+
+        Class driverClass;
+        try {
+            driverClass = Class.forName(driverClassName, true, Thread
+                    .currentThread()
+                    .getContextClassLoader());
+        }
+        catch (Exception ex) {
+            throw new SQLException("Can not load JDBC driver named '"
+                    + driverClassName
+                    + "': "
+                    + ex.getMessage());
+        }
+
+        try {
+            return (Driver) driverClass.newInstance();
+        }
+        catch (Exception ex) {
+            throw new SQLException("Error instantiating driver '"
+                    + driverClassName
+                    + "': "
+                    + ex.getMessage());
+        }
+    }
+
+    /**
      * Creates a new DriverDataSource.
      */
     public DriverDataSource(String driverClassName, String connectionUrl)
             throws SQLException {
+        this(driverClassName, connectionUrl, null, null);
+    }
 
-        this.connectionUrl = connectionUrl;
+    /**
+     * @since 3.0
+     */
+    public DriverDataSource(String driverClassName, String connectionUrl,
+            String userName, String password) throws SQLException {
 
-        if (driverClassName != null) {
-            try {
-                this.driver = (Driver) Class.forName(driverClassName).newInstance();
-            }
-            catch (Exception ex) {
-                throw new SQLException("Can not load JDBC driver named '"
-                        + driverClassName
-                        + "': "
-                        + ex.getMessage());
-            }
-        }
+        setDriverClassName(driverClassName);
+
+        this.connectionUrl = connectionUrl;
+        this.userName = userName;
+        this.password = password;
     }
 
     /**
@@ -72,6 +104,7 @@
      */
     public DriverDataSource(Driver driver, String connectionUrl, String userName,
             String password) {
+
         this.driver = driver;
         this.connectionUrl = connectionUrl;
         this.userName = userName;
@@ -114,10 +147,10 @@
                 }
                 c = driver.connect(connectionUrl, connectProperties);
             }
-            
+
             // some drivers (Oracle) return null connections instead of throwing
             // an exception... fix it here
-            
+
             if (c == null) {
                 throw new SQLException("Can't establish connection: " + connectionUrl);
             }
@@ -159,5 +192,57 @@
 
     public void setLogger(ConnectionEventLoggingDelegate delegate) {
         logger = delegate;
+    }
+
+    /**
+     * @since 3.0
+     */
+    public String getConnectionUrl() {
+        return connectionUrl;
+    }
+
+    /**
+     * @since 3.0
+     */
+    public void setConnectionUrl(String connectionUrl) {
+        this.connectionUrl = connectionUrl;
+    }
+
+    /**
+     * @since 3.0
+     */
+    public String getPassword() {
+        return password;
+    }
+
+    /**
+     * @since 3.0
+     */
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    /**
+     * @since 3.0
+     */
+    public String getUserName() {
+        return userName;
+    }
+
+    /**
+     * @since 3.0
+     */
+    public void setUserName(String userName) {
+        this.userName = userName;
+    }
+
+    public String getDriverClassName() {
+        return driver != null ? driver.getClass().getName() : null;
+    }
+
+    public void setDriverClassName(String driverClassName) throws SQLException {
+        if (!Util.nullSafeEquals(getDriverClassName(), driverClassName)) {
+            this.driver = driverClassName != null ? loadDriver(driverClassName) : null;
+        }
     }
 }



Mime
View raw message