[hg] main-silver: #215480: Check that DB driver class is really ...

  • From: Jaroslav Havlin < >
  • To:
  • Subject: [hg] main-silver: #215480: Check that DB driver class is really ...
  • Date: Fri, 07 Dec 2012 16:07:28 -0800

changeset 2c15c079846c in main-silver ((none))
details: http://hg.netbeans.org/main-silver/rev/2c15c079846c
description:
        #215480: Check that DB driver class is really a java.sql.Driver

diffstat:

 db/src/org/netbeans/modules/db/explorer/DbDriverManager.java     |   9 +++-
 db/src/org/netbeans/modules/db/explorer/dlg/AddDriverDialog.form |   2 +-
 db/src/org/netbeans/modules/db/explorer/dlg/AddDriverDialog.java |  23 
++++++---
 db/src/org/netbeans/modules/db/explorer/dlg/Bundle.properties    |   1 +
 4 files changed, 26 insertions(+), 9 deletions(-)

diffs (106 lines):

diff --git a/db/src/org/netbeans/modules/db/explorer/DbDriverManager.java 
b/db/src/org/netbeans/modules/db/explorer/DbDriverManager.java
--- a/db/src/org/netbeans/modules/db/explorer/DbDriverManager.java
+++ b/db/src/org/netbeans/modules/db/explorer/DbDriverManager.java
@@ -223,13 +223,20 @@
      */
     public Driver getDriver(JDBCDriver jdbcDriver) throws SQLException {
         ClassLoader l = getClassLoader(jdbcDriver);
+        Object driver;
         try {
-            return (Driver)Class.forName(jdbcDriver.getClassName(), true, 
l).newInstance();
+            driver = Class.forName(jdbcDriver.getClassName(), true, 
l).newInstance();
         } catch (Exception e) {
             SQLException sqlex = createDriverNotFoundException();
             sqlex.initCause(e);
             throw sqlex;
         }
+        if (driver instanceof Driver) {
+            return (Driver) driver;
+        } else {
+            throw new SQLException(driver.getClass().getName()
+                    + " is not a driver");                              
//NOI18N
+        }
     }
     
     /**
diff --git a/db/src/org/netbeans/modules/db/explorer/dlg/AddDriverDialog.form 
b/db/src/org/netbeans/modules/db/explorer/dlg/AddDriverDialog.form
--- a/db/src/org/netbeans/modules/db/explorer/dlg/AddDriverDialog.form
+++ b/db/src/org/netbeans/modules/db/explorer/dlg/AddDriverDialog.form
@@ -1,4 +1,4 @@
-<?xml version="1.1" encoding="UTF-8" ?>
+<?xml version="1.0" encoding="UTF-8" ?>
 
 <Form version="1.2" maxVersion="1.2" 
type="org.netbeans.modules.form.forminfo.JPanelFormInfo">
   <AuxValues>
diff --git a/db/src/org/netbeans/modules/db/explorer/dlg/AddDriverDialog.java 
b/db/src/org/netbeans/modules/db/explorer/dlg/AddDriverDialog.java
--- a/db/src/org/netbeans/modules/db/explorer/dlg/AddDriverDialog.java
+++ b/db/src/org/netbeans/modules/db/explorer/dlg/AddDriverDialog.java
@@ -104,6 +104,7 @@
     private static final Logger LOGGER = 
Logger.getLogger(AddDriverDialog.class.getName());
     private JDBCDriver drv;
     private final AddConnectionWizard wd;
+    private volatile URLClassLoader jarClassLoader = null;
 
     /** Creates new AddDriverDialog.
      * @param driverNode driver node to be customized or null to create a 
new one
@@ -578,6 +579,7 @@
                 //PENDING
             }
         }
+        getJarClassLoader(); // init class loader while the JAR file is 
cached
     }
     
     private void findDriverClassByInspection() {
@@ -587,13 +589,7 @@
             @Override
             public void run() {
                 startProgress();
-                                     
-                // This classloader is used to load classes
-                // from the jar files for the driver.  We can then use
-                // introspection to see if a class in one of these jar files
-                // implements java.sql.Driver
-                URLClassLoader jarloader = 
-                    new URLClassLoader(drvs.toArray(new URL[drvs.size 
()]),this.getClass ().getClassLoader ());
+                URLClassLoader jarloader = getJarClassLoader();
 
                 for (int i = 0; i < dlm.size(); i++) {
                     try {
@@ -631,6 +627,16 @@
         }, 0);
     }
     
+    private URLClassLoader getJarClassLoader() {
+        // This classloader is used to load classes
+        // from the jar files for the driver.  We can then use
+        // introspection to see if a class in one of these jar files
+        // implements java.sql.Driver
+        jarClassLoader =
+                new URLClassLoader(drvs.toArray(new URL[drvs.size()]),
+                this.getClass().getClassLoader());
+        return jarClassLoader;
+    }
     private void addDriverClass(String drv) {
         if (((DefaultComboBoxModel) 
drvClassComboBox.getModel()).getIndexOf(drv) < 0) {
             drvClassComboBox.addItem(drv);
@@ -692,6 +698,9 @@
             }
         } else if 
(drvClassComboBox.getEditor().getItem().toString().length() == 0) {
             message = NbBundle.getMessage(AddDriverDialog.class, 
"AddDriverMissingClass");
+        } else if (jarClassLoader != null && !isDriverClass(jarClassLoader,
+                drvClassComboBox.getEditor().getItem().toString())) {
+            message = NbBundle.getMessage(AddDriverDialog.class, 
"AddDriverNotJavaSqlDriver"); //NOI18N
         } else if (nameTextField.getText().length() == 0) {
             message = NbBundle.getMessage(AddDriverDialog.class, 
"AddDriverMissingName");
         } else if (!customizer && nameTextField.getText().length() > 0) {
diff --git a/db/src/org/netbeans/modules/db/explorer/dlg/Bundle.properties 
b/db/src/org/netbeans/modules/db/explorer/dlg/Bundle.properties
--- a/db/src/org/netbeans/modules/db/explorer/dlg/Bundle.properties
+++ b/db/src/org/netbeans/modules/db/explorer/dlg/Bundle.properties
@@ -56,6 +56,7 @@
 AddDriverMissingDriverFiles=<Missing Driver Files>
 AddDriverDuplicateFile=Driver File {0} is already added.
 AddDriverMissingClass=Driver Class is missing.
+AddDriverNotJavaSqlDriver=Specified class is not a driver (java.sql.Driver).
 AddDriverMissingName=Driver Name is missing.
 AddDriverDuplicateName=Driver Name is already used.
 ACS_AddDriverDialogA11yDesc=New JDBC driver dialog.

[hg] main-silver: #215480: Check that DB driver class is really ...

Jaroslav Havlin 12/08/2012

Project Features

About this Project

DB was started in November 2009, is owned by Antonin Nebuzelsky, and has 113 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20131025.e7cbc9d). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close