diff --git a/db/src/org/netbeans/modules/db/explorer/ConnectionList.java b/db/src/org/netbeans/modules/db/explorer/ConnectionList.java --- a/db/src/org/netbeans/modules/db/explorer/ConnectionList.java +++ b/db/src/org/netbeans/modules/db/explorer/ConnectionList.java @@ -43,18 +43,15 @@ import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; +import java.util.Collection; import java.util.Iterator; import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; import org.netbeans.api.db.explorer.ConnectionListener; import org.netbeans.api.db.explorer.DatabaseException; import org.netbeans.modules.db.explorer.infos.RootNodeInfo; import org.openide.util.Lookup; import org.openide.util.LookupEvent; import org.openide.util.LookupListener; -import org.openide.util.NbBundle; import org.openide.util.lookup.Lookups; @@ -64,28 +61,19 @@ * and removed through ConnectionListener. * * This class only maintains a list of DBConnection objects. It has no links - * to the UI (nodes representing these objects), therefore adding a DBConnection + * to the UI (nodes representing these objects), therefore adding a DBConnection * doesn't create a node for it. - * + * * @author Andrei Badea */ public class ConnectionList { - private static Logger LOGGER = Logger.getLogger(ConnectionList.class.getName()); - + private static ConnectionList DEFAULT; - + private Lookup.Result result = getLookupResult(); - - // A write-through cache of connections, initialized when the ConnectionList - // singleton is constructed. This eliminates having to always reconstruct - // the connection from the file each time we get it, and also eliminates - // false duplicates where the name is the same but the instances are different - // (see issue 142036 and issue 137811) - private HashMap connectionCache = - new HashMap(); - - private final List/**/ listeners = new ArrayList(1); - + + private List/**/ listeners = new ArrayList(1); + public static synchronized ConnectionList getDefault() { if (DEFAULT == null) { DatabaseConnectionConvertor.importOldConnections(); @@ -94,114 +82,90 @@ } return DEFAULT; } - + private ConnectionList() { // issue 75204: forces the DataObject's corresponding to the DatabaseConnection's // to be initialized and held strongly so the same DatabaseConnection is // returns as long as it is held strongly result.allInstances(); - + result.addLookupListener(new LookupListener() { public void resultChanged(LookupEvent e) { fireListeners(); } }); - - initializeCache(); - } - - private void initializeCache() { - for ( Iterator it = result.allInstances().iterator() ; it.hasNext() ; ) { - DatabaseConnection dbconn = (DatabaseConnection)it.next(); - connectionCache.put(dbconn.getName(), dbconn); - } - } - - - /** - * Refresh the cache. Used for testing - */ - public void refreshCache() { - connectionCache.clear(); - initializeCache(); } public DatabaseConnection[] getConnections() { - return connectionCache.values().toArray(new DatabaseConnection[connectionCache.size()]); + Collection dbconns = result.allInstances(); + return (DatabaseConnection[])dbconns.toArray(new DatabaseConnection[dbconns.size()]); } - + public DatabaseConnection getConnection(DatabaseConnection impl) { + System.out.println("getConnection " + System.identityHashCode(impl)); if (impl == null) { throw new NullPointerException(); } + DatabaseConnection[] dbconns = getConnections(); + for (int i = 0; i < dbconns.length; i++) { + if (impl.equals(dbconns[i])) { + return dbconns[i]; + } + } + return null; + } - return connectionCache.get(impl.getName()); - } - public void add(DatabaseConnection dbconn) throws DatabaseException { if (dbconn == null) { throw new NullPointerException(); - } - if (connectionCache.containsKey(dbconn.getName())) { - throw new DatabaseException(NbBundle.getMessage(ConnectionList.class, "ERR_CONN_ALREADY_EXISTS", dbconn.getName())); } try { DatabaseConnectionConvertor.create(dbconn); } catch (IOException e) { throw new DatabaseException(e); } + } - connectionCache.put(dbconn.getName(), dbconn); + public boolean contains(DatabaseConnection dbconn) { + return getConnection(dbconn) != null; } - - public boolean contains(DatabaseConnection dbconn) { - return connectionCache.containsKey(dbconn.getName()); - } - + public void remove(DatabaseConnection dbconn) throws DatabaseException { if (dbconn == null) { throw new NullPointerException(); } - - if (!connectionCache.containsKey(dbconn.getName())) { - LOGGER.log(Level.INFO, "The connection " + dbconn.getName() + " is not in our connection list cache"); - } else { - connectionCache.remove(dbconn.getName()); - } - try { DatabaseConnectionConvertor.remove(dbconn); } catch (IOException e) { throw new DatabaseException(e); } - } - + public void addConnectionListener(ConnectionListener listener) { synchronized (listeners) { listeners.add(listener); } } - + public void removeConnectionListener(ConnectionListener listener) { synchronized (listeners) { listeners.remove(listener); } } - + private void fireListeners() { List listenersCopy; - + synchronized (listeners) { listenersCopy = new ArrayList(listeners); } - + for (Iterator i = listenersCopy.iterator(); i.hasNext();) { ConnectionListener l = (ConnectionListener)i.next(); l.connectionsChanged(); } } - + private synchronized Lookup.Result getLookupResult() { return Lookups.forPath(DatabaseConnectionConvertor.CONNECTIONS_PATH).lookupResult(DatabaseConnection.class); } diff --git a/db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java b/db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java --- a/db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java +++ b/db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java @@ -169,6 +169,7 @@ /** Default constructor */ public DatabaseConnection() { + System.out.println("Creating " + System.identityHashCode(this)); dbconn = DatabaseConnectionAccessor.DEFAULT.createDatabaseConnection(this); propertySupport = new PropertyChangeSupport(this); } diff --git a/db/src/org/netbeans/modules/db/explorer/DatabaseConnectionConvertor.java b/db/src/org/netbeans/modules/db/explorer/DatabaseConnectionConvertor.java --- a/db/src/org/netbeans/modules/db/explorer/DatabaseConnectionConvertor.java +++ b/db/src/org/netbeans/modules/db/explorer/DatabaseConnectionConvertor.java @@ -215,6 +215,7 @@ } private static DatabaseConnection createDatabaseConnection(Handler handler) { + System.out.println("new " + handler.connectionUrl + " " + handler.schema); DatabaseConnection dbconn = new DatabaseConnection( handler.driverClass, handler.driverName, diff --git a/db/src/org/netbeans/modules/db/explorer/infos/ConnectionNodeInfo.java b/db/src/org/netbeans/modules/db/explorer/infos/ConnectionNodeInfo.java --- a/db/src/org/netbeans/modules/db/explorer/infos/ConnectionNodeInfo.java +++ b/db/src/org/netbeans/modules/db/explorer/infos/ConnectionNodeInfo.java @@ -71,7 +71,7 @@ //import org.netbeans.modules.db.explorer.nodes.ConnectionNode; -public class ConnectionNodeInfo extends DatabaseNodeInfo implements ConnectionOperations { +public class ConnectionNodeInfo extends DatabaseNodeInfo { static final long serialVersionUID =-8322295510950137669L; @@ -317,8 +317,6 @@ } - - private void connect(String dbsys) throws DatabaseException { String drvurl = getDriver(); @@ -349,13 +347,6 @@ dbe.initCause(e); throw dbe; } - } - - /* - * Connects this connection node to the database. - */ - public void connect() throws DatabaseException { - connect((String)null); } /* diff --git a/db/src/org/netbeans/modules/db/explorer/infos/ConnectionOperations.java b/db/src/org/netbeans/modules/db/explorer/infos/ConnectionOperations.java deleted file mode 100644 --- a/db/src/org/netbeans/modules/db/explorer/infos/ConnectionOperations.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. - * - * The contents of this file are subject to the terms of either the GNU - * General Public License Version 2 only ("GPL") or the Common - * Development and Distribution License("CDDL") (collectively, the - * "License"). You may not use this file except in compliance with the - * License. You can obtain a copy of the License at - * http://www.netbeans.org/cddl-gplv2.html - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the - * specific language governing permissions and limitations under the - * License. When distributing the software, include this License Header - * Notice in each file and include the License file at - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this - * particular file as subject to the "Classpath" exception as provided - * by Sun in the GPL Version 2 section of the License file that - * accompanied this code. If applicable, add the following below the - * License Header, with the fields enclosed by brackets [] replaced by - * your own identifying information: - * "Portions Copyrighted [year] [name of copyright owner]" - * - * Contributor(s): - * - * The Original Software is NetBeans. The Initial Developer of the Original - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun - * Microsystems, Inc. All Rights Reserved. - * - * If you wish your version of this file to be governed by only the CDDL - * or only the GPL Version 2, indicate your decision by adding - * "[Contributor] elects to include this software in this distribution - * under the [CDDL or GPL Version 2] license." If you do not indicate a - * single choice of license, a recipient has the option to distribute - * your version of this file under either the CDDL, the GPL Version 2 or - * to extend the choice of license to its licensees as provided above. - * However, if you add GPL Version 2 code and therefore, elected the GPL - * Version 2 license, then the option applies only if the new code is - * made subject to such option by the copyright holder. - */ - -package org.netbeans.modules.db.explorer.infos; - -import org.netbeans.api.db.explorer.DatabaseException; - -/** -* Interface of driver-related nodes. -* @author Slavek Psenicka -*/ -public interface ConnectionOperations -{ - public void connect() - throws DatabaseException; - - public void disconnect() - throws DatabaseException; -} diff --git a/db/src/org/netbeans/modules/db/explorer/infos/DatabaseNodeInfo.java b/db/src/org/netbeans/modules/db/explorer/infos/DatabaseNodeInfo.java --- a/db/src/org/netbeans/modules/db/explorer/infos/DatabaseNodeInfo.java +++ b/db/src/org/netbeans/modules/db/explorer/infos/DatabaseNodeInfo.java @@ -85,6 +85,7 @@ public static final String DATABASE = "db"; //NOI18N public static final String URL = "url"; //NOI18N public static final String PREFIX = "prefix"; //NOI18N + public static final String DATABASE_CONNECTION = "database_connection"; //NOI18N public static final String CONNECTION = "connection"; //NOI18N public static final String CODE = "code"; //NOI18N public static final String NODE = "node"; //NOI18N @@ -121,7 +122,7 @@ // Sychronized on this private boolean connected = false; - + // Thread-safe, no synchronization private final ChangeSupport changeSupport = new ChangeSupport(this); @@ -515,15 +516,16 @@ public DatabaseConnection getDatabaseConnection() { - DatabaseConnection con = new DatabaseConnection(getDriver(), getDatabase(), getUser(), getPassword()); - if(get(REMEMBER_PWD)!=null) { - con.setRememberPassword(((Boolean)get(REMEMBER_PWD)).booleanValue()); - } - else - con.setRememberPassword(Boolean.FALSE.booleanValue()); - con.setSchema(getSchema()); - con.setDriverName((String)get("drivername")); - return con; + return (DatabaseConnection) get(DATABASE_CONNECTION); +// DatabaseConnection con = new DatabaseConnection(getDriver(), getDatabase(), getUser(), getPassword()); +// if(get(REMEMBER_PWD)!=null) { +// con.setRememberPassword(((Boolean)get(REMEMBER_PWD)).booleanValue()); +// } +// else +// con.setRememberPassword(Boolean.FALSE.booleanValue()); +// con.setSchema(getSchema()); +// con.setDriverName((String)get("drivername")); +// return con; } public DatabaseDriver getDatabaseDriver() @@ -531,8 +533,9 @@ return (DatabaseDriver)get(DBDRIVER); } - public void setDatabaseConnection(DBConnection cinfo) + public void setDatabaseConnection(DatabaseConnection cinfo) { + put(DATABASE_CONNECTION, cinfo); String pwd = cinfo.getPassword(); put(DRIVER, cinfo.getDriver()); put(DATABASE, cinfo.getDatabase()); diff --git a/db/src/org/netbeans/modules/db/explorer/infos/RootNodeInfo.java b/db/src/org/netbeans/modules/db/explorer/infos/RootNodeInfo.java --- a/db/src/org/netbeans/modules/db/explorer/infos/RootNodeInfo.java +++ b/db/src/org/netbeans/modules/db/explorer/infos/RootNodeInfo.java @@ -251,11 +251,12 @@ private ConnectionNodeInfo createConnectionNodeInfo(DatabaseConnection dbconn) throws DatabaseException { ConnectionNodeInfo ninfo = (ConnectionNodeInfo)createNodeInfo(this, DatabaseNode.CONNECTION); - ninfo.setUser(dbconn.getUser()); - ninfo.setDatabase(dbconn.getDatabase()); - ninfo.setSchema(dbconn.getSchema()); - ninfo.setName(dbconn.getName()); - ninfo.setDatabaseConnection(dbconn); + ninfo.setDatabaseConnection(dbconn); +// ninfo.setUser(dbconn.getUser()); +// ninfo.setDatabase(dbconn.getDatabase()); +// ninfo.setSchema(dbconn.getSchema()); +// ninfo.setName(dbconn.getName()); +// ninfo.setDatabaseConnection(dbconn); if (DatabaseConnection.test(dbconn.getConnection(), dbconn.getName())) { ninfo.connect(dbconn); }