[hg] main-silver: #229250: If more than one connector listed--na...

  • From: Jaroslav Havlin < >
  • To:
  • Subject: [hg] main-silver: #229250: If more than one connector listed--na...
  • Date: Tue, 04 Jun 2013 13:34:11 -0700

changeset 90117a807fc9 in main-silver ((none))
details: http://hg.netbeans.org/main-silver/rev/90117a807fc9
description:
        #229250: If more than one connector listed--names keep multiplying in 
connection wizard driver name list

        Patch by Matthias42.

diffstat:

 db/src/org/netbeans/api/db/explorer/JDBCDriver.java        |  30 ++++
 db/src/org/netbeans/api/db/explorer/JDBCDriverManager.java |   2 +-
 db/src/org/netbeans/modules/db/util/Bundle.properties      |   2 +
 db/src/org/netbeans/modules/db/util/DriverListUtil.java    |  90 
+++----------
 db/src/org/netbeans/modules/db/util/JdbcUrl.java           |  59 +++++++-
 5 files changed, 105 insertions(+), 78 deletions(-)

diffs (326 lines):

diff --git a/db/src/org/netbeans/api/db/explorer/JDBCDriver.java 
b/db/src/org/netbeans/api/db/explorer/JDBCDriver.java
--- a/db/src/org/netbeans/api/db/explorer/JDBCDriver.java
+++ b/db/src/org/netbeans/api/db/explorer/JDBCDriver.java
@@ -48,6 +48,7 @@
 import java.sql.Driver;
 import java.sql.SQLException;
 import java.util.Arrays;
+import java.util.Objects;
 import org.netbeans.modules.db.explorer.DbDriverManager;
 
 /**
@@ -146,4 +147,33 @@
                 "',className='" + clazz + // NOI18N
                 "',urls=" + Arrays.asList(urls) + "]"; // NOI18N
     }
+
+    @Override
+    public int hashCode() {
+        return clazz.hashCode();
 }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        final JDBCDriver other = (JDBCDriver) obj;
+        if (!Arrays.deepEquals(this.urls, other.urls)) {
+            return false;
+        }
+        if (!Objects.equals(this.clazz, other.clazz)) {
+            return false;
+        }
+        if (!Objects.equals(this.displayName, other.displayName)) {
+            return false;
+        }
+        if (!Objects.equals(this.name, other.name)) {
+            return false;
+        }
+        return true;
+    }
+}
diff --git a/db/src/org/netbeans/api/db/explorer/JDBCDriverManager.java 
b/db/src/org/netbeans/api/db/explorer/JDBCDriverManager.java
--- a/db/src/org/netbeans/api/db/explorer/JDBCDriverManager.java
+++ b/db/src/org/netbeans/api/db/explorer/JDBCDriverManager.java
@@ -132,7 +132,7 @@
         if (drvClass == null) {
             throw new NullPointerException();
         }
-        LinkedList<JDBCDriver> res = new LinkedList<JDBCDriver>();
+        LinkedList<JDBCDriver> res = new LinkedList<>();
         JDBCDriver[] drvs = getDrivers();
         for (int i = 0; i < drvs.length; i++) {
             if (drvClass.equals(drvs[i].getClassName())) {
diff --git a/db/src/org/netbeans/modules/db/util/Bundle.properties 
b/db/src/org/netbeans/modules/db/util/Bundle.properties
--- a/db/src/org/netbeans/modules/db/util/Bundle.properties
+++ b/db/src/org/netbeans/modules/db/util/Bundle.properties
@@ -77,3 +77,5 @@
 PropertyEditorPanel.propertyTable.columnModel.title0=Property
 PropertyEditorPanel.addRowButton.text=Add Property
 PropertyEditorPanel.removeRowButton.text=Remove Property
+
+JDBC_URL_DRIVER_NAME={0} on {1}
\ No newline at end of file
diff --git a/db/src/org/netbeans/modules/db/util/DriverListUtil.java 
b/db/src/org/netbeans/modules/db/util/DriverListUtil.java
--- a/db/src/org/netbeans/modules/db/util/DriverListUtil.java
+++ b/db/src/org/netbeans/modules/db/util/DriverListUtil.java
@@ -45,34 +45,32 @@
 package org.netbeans.modules.db.util;
 
 import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.LinkedList;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 import java.util.TreeSet;
-
 import org.netbeans.api.db.explorer.JDBCDriver;
 import org.netbeans.api.db.explorer.JDBCDriverManager;
 import org.openide.util.NbBundle;
 
 public class DriverListUtil {
 
-    private static List<JdbcUrl> urls = new LinkedList<JdbcUrl>();
+    private static Set<JdbcUrl> templateUrl = new HashSet<>();
         
     private DriverListUtil() {
     }
     
     
     private static void add(JdbcUrl url) {
-        urls.add(url);
+        templateUrl.add(url);
     }
     
     private static void add(String name, String type, String 
driverClassName, String urlTemplate) {
-        urls.add(new JdbcUrl(name, name, driverClassName, type, 
urlTemplate));
+        templateUrl.add(new JdbcUrl(name, name, driverClassName, type, 
urlTemplate));
     }
     
     private static void add(String name, String type, String 
driverClassName, String urlTemplate, boolean parseUrl) {
-        urls.add(new JdbcUrl(name, name, driverClassName, type, urlTemplate, 
parseUrl));
+        templateUrl.add(new JdbcUrl(name, name, driverClassName, type, 
urlTemplate, parseUrl));
     }
         
     private  static void add(String name, String driverClassName, String 
urlTemplate) {
@@ -335,8 +333,8 @@
     }
     
     public static Set<String> getDrivers() {
-        TreeSet<String> drivers = new TreeSet<String>();
-        for (JdbcUrl url : urls) {
+        TreeSet<String> drivers = new TreeSet<>();
+        for (JdbcUrl url : templateUrl) {
             // A set contains no duplicate elements, so if the same class 
name 
             // is found twice, that's OK, because it just replaces the entry
             // that was already there
@@ -346,52 +344,34 @@
     }
     
     public static List<JdbcUrl> getJdbcUrls(JDBCDriver driver) {
-        ArrayList<JdbcUrl> driverUrls = new ArrayList<JdbcUrl>();
-        JdbcUrl newurl = null;
+        Set<JdbcUrl> driverUrls = new HashSet<>();
         
-        for (JdbcUrl url : urls) {
+        for (JdbcUrl url : templateUrl) {
             if (url.getClassName().equals(driver.getClassName())) {
-                if (url.getDriver() == null) {
-                    url.setDriver(driver);
-                    // Clear out any properties that may be set
-                    url.clear();
-
-                    driverUrls.add(url);
-                } else {
-                    if (! isDriverEquals(driver, url.getDriver())) {
-                        // We already have one driver registered for this 
class name.
-                        // This is a new driver for the same driver class.  
That means
-                        // it should be on the list with its own entry, but 
with the
-                        // same URL tempate
-                        newurl = new JdbcUrl(driver, url.getUrlTemplate(), 
url.isParseUrl());
+                JdbcUrl newurl = new JdbcUrl(url, driver);
+                String connectionName = NbBundle.getMessage(
+                        DriverListUtil.class, "JDBC_URL_DRIVER_NAME",
+                        url.getDisplayName(), driver.getDisplayName());
+                newurl.setDisplayName(connectionName);
                         driverUrls.add(newurl);
-                    } else {
-                        driverUrls.add(url);
                     }
                 }
-            }
-        }
-
-        // Have to do this out of the loop or we get a 
ConcurrentModificationException
-        if (newurl != null) {
-            add(newurl);
-        }
 
         if (driverUrls.isEmpty()) {
             driverUrls.add(new JdbcUrl(driver));
         }
 
-        return driverUrls;
+        return new ArrayList<>(driverUrls);
     }
     
-    static List<JdbcUrl> getJdbcUrls() {
+    static Set<JdbcUrl> getJdbcUrls() {
         // For unit testing
-        return urls;
+        return templateUrl;
     }
     
     public static String getName(String driverClass) {
         // Find the first match
-        for ( JdbcUrl url : urls) {
+        for ( JdbcUrl url : templateUrl) {
             if (url.getClassName().equals(driverClass)) {
                 return url.getName();
             }
@@ -402,40 +382,20 @@
     
     public static String findFreeName(String name) {
         String ret;
-        List<String> names = new ArrayList<String> ();
+        List<String> names = new ArrayList<>();
         JDBCDriver[] drivers = JDBCDriverManager.getDefault().getDrivers();
-        for (int i = 0; i < drivers.length; i++)
+        for (int i = 0; i < drivers.length; i++) {
             names.add(drivers[i].getDisplayName());
-        
-        if (names.contains(name))
+        }
+        if (names.contains(name)) {
             for (int i = 1;;i++) {
                 ret = name + " (" + i + ")"; // NOI18N
-                if (!names.contains(ret))
+                if (!names.contains(ret)) {
                     return ret;
             }
-        else
+            }
+        } else {
             return name;
     }
-
-    private static boolean isDriverEquals(JDBCDriver driverOne, JDBCDriver 
driverTwo) {
-        // I didn't put this as a method on JDBCDriver because I don't want 
to change
-        // the behavior of equals() on a public class, and what we want for 
equals()
-        // may not be what others want.
-        if (driverOne == null && driverTwo == null) {
-            return true;
         }
-
-        if (driverOne == null || driverTwo == null) {
-            return false;
         }
-
-        if (driverOne == driverTwo) {
-            return true;
-        }
-
-        return driverOne.getClassName().equals(driverTwo.getClassName()) &&
-                
driverOne.getDisplayName().equals(driverTwo.getDisplayName()) &&
-                driverOne.getName().equals(driverTwo.getName()) &&
-                Arrays.equals(driverOne.getURLs(), driverTwo.getURLs());
-    }
-}
diff --git a/db/src/org/netbeans/modules/db/util/JdbcUrl.java 
b/db/src/org/netbeans/modules/db/util/JdbcUrl.java
--- a/db/src/org/netbeans/modules/db/util/JdbcUrl.java
+++ b/db/src/org/netbeans/modules/db/util/JdbcUrl.java
@@ -46,6 +46,7 @@
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Objects;
 import java.util.Set;
 import org.netbeans.api.db.explorer.JDBCDriver;
 import org.openide.util.NbBundle;
@@ -129,6 +130,14 @@
         this.driver = driver;
     }
     
+    public JdbcUrl(JdbcUrl template, JDBCDriver driver) {
+        this(template.getName(), template.getDisplayName(),
+                template.getClassName(),
+                template.getType(), template.getUrlTemplate(),
+                template.isParseUrl());
+        this.driver = driver;
+    }
+
     public JdbcUrl(JDBCDriver driver) {
         this(driver, null, null);
     }
@@ -163,6 +172,10 @@
         return driver;
     }
 
+    public void setDisplayName(String displayName) {
+        this.displayName = displayName;
+    }
+
     public String getDisplayName() {
         if (isEmpty(getType())) {
             return displayName;
@@ -191,21 +204,43 @@
     }
     
     @Override
-    public boolean equals(Object other) {
-        if (other == null || ! (other instanceof JdbcUrl)) {
-            return false;
-        }
-
-        JdbcUrl otherUrl = (JdbcUrl)other;
-        
-        return otherUrl.getDisplayName().equals(this.getDisplayName());
+    public int hashCode() {
+        int hash = 3;
+        hash = 23 * hash + Objects.hashCode(this.urlTemplate);
+        return hash;
     }
 
     @Override
-    public int hashCode() {
-        int hash = 3;
-        hash = 97 * hash + (this.getDisplayName() != null ? 
this.getDisplayName().hashCode() : 0);
-        return hash;
+    public boolean equals(Object obj) {
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        final JdbcUrl other = (JdbcUrl) obj;
+        if (!Objects.equals(this.driver, other.driver)) {
+            return false;
+        }
+        if (this.parseUrl != other.parseUrl) {
+            return false;
+        }
+        if (!Objects.equals(this.name, other.name)) {
+            return false;
+        }
+        if (!Objects.equals(this.displayName, other.displayName)) {
+            return false;
+        }
+        if (!Objects.equals(this.className, other.className)) {
+            return false;
+        }
+        if (!Objects.equals(this.urlTemplate, other.urlTemplate)) {
+            return false;
+        }
+        if (!Objects.equals(this.type, other.type)) {
+            return false;
+        }
+        return true;
     }
     
     protected boolean isEmpty(String str) {

[hg] main-silver: #229250: If more than one connector listed--na...

Jaroslav Havlin 06/04/2013

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