001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.explorer;
043:
044: import java.io.IOException;
045: import java.util.ArrayList;
046: import java.util.Collection;
047: import java.util.Iterator;
048: import java.util.List;
049: import org.netbeans.api.db.explorer.ConnectionListener;
050: import org.netbeans.api.db.explorer.DatabaseException;
051: import org.netbeans.modules.db.explorer.nodes.RootNode;
052: import org.openide.util.Lookup;
053: import org.openide.util.LookupEvent;
054: import org.openide.util.LookupListener;
055: import org.openide.util.lookup.Lookups;
056:
057: /**
058: * Class which encapsulates the Vector used to keep a list of connections
059: * in the Database Explorer. It also can fire events when connections are added
060: * and removed through ConnectionListener.
061: *
062: * This class only maintains a list of DBConnection objects. It has no links
063: * to the UI (nodes representing these objects), therefore adding a DBConnection
064: * doesn't create a node for it.
065: *
066: * @author Andrei Badea
067: */
068: public class ConnectionList {
069:
070: private static ConnectionList DEFAULT;
071:
072: private Lookup.Result result = getLookupResult();
073:
074: private List/*<ConnectionListener>*/listeners = new ArrayList(1);
075:
076: public static synchronized ConnectionList getDefault() {
077: if (DEFAULT == null) {
078: DatabaseConnectionConvertor.importOldConnections();
079: RootNode.getOption().save();
080: DEFAULT = new ConnectionList();
081: }
082: return DEFAULT;
083: }
084:
085: private ConnectionList() {
086: // issue 75204: forces the DataObject's corresponding to the DatabaseConnection's
087: // to be initialized and held strongly so the same DatabaseConnection is
088: // returns as long as it is held strongly
089: result.allInstances();
090:
091: result.addLookupListener(new LookupListener() {
092: public void resultChanged(LookupEvent e) {
093: fireListeners();
094: }
095: });
096: }
097:
098: public DatabaseConnection[] getConnections() {
099: Collection dbconns = result.allInstances();
100: return (DatabaseConnection[]) dbconns
101: .toArray(new DatabaseConnection[dbconns.size()]);
102: }
103:
104: public DatabaseConnection getConnection(DatabaseConnection impl) {
105: if (impl == null) {
106: throw new NullPointerException();
107: }
108: DatabaseConnection[] dbconns = getConnections();
109: for (int i = 0; i < dbconns.length; i++) {
110: if (impl.equals(dbconns[i])) {
111: return dbconns[i];
112: }
113: }
114: return null;
115: }
116:
117: public void add(DatabaseConnection dbconn) throws DatabaseException {
118: if (dbconn == null) {
119: throw new NullPointerException();
120: }
121: try {
122: DatabaseConnectionConvertor.create(dbconn);
123: } catch (IOException e) {
124: throw new DatabaseException(e);
125: }
126: }
127:
128: public boolean contains(DatabaseConnection dbconn) {
129: return getConnection(dbconn) != null;
130: }
131:
132: public void remove(DatabaseConnection dbconn)
133: throws DatabaseException {
134: if (dbconn == null) {
135: throw new NullPointerException();
136: }
137: try {
138: DatabaseConnectionConvertor.remove(dbconn);
139: } catch (IOException e) {
140: throw new DatabaseException(e);
141: }
142: }
143:
144: public void addConnectionListener(ConnectionListener listener) {
145: synchronized (listeners) {
146: listeners.add(listener);
147: }
148: }
149:
150: public void removeConnectionListener(ConnectionListener listener) {
151: synchronized (listeners) {
152: listeners.remove(listener);
153: }
154: }
155:
156: private void fireListeners() {
157: List listenersCopy;
158:
159: synchronized (listeners) {
160: listenersCopy = new ArrayList(listeners);
161: }
162:
163: for (Iterator i = listenersCopy.iterator(); i.hasNext();) {
164: ConnectionListener l = (ConnectionListener) i.next();
165: l.connectionsChanged();
166: }
167: }
168:
169: private synchronized Lookup.Result getLookupResult() {
170: return Lookups.forPath(
171: DatabaseConnectionConvertor.CONNECTIONS_PATH)
172: .lookupResult(DatabaseConnection.class);
173: }
174: }
|