/* * The contents of this file are subject to the terms of the Common Development * and Distribution License (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.html * or http://www.netbeans.org/cddl.txt. * * When distributing Covered Code, include this CDDL Header Notice in each file * and include the License file at http://www.netbeans.org/cddl.txt. * If applicable, add the following below the CDDL Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * * The Original Software is NetBeans. The Initial Developer of the Original * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.j2ee.common.ui; import org.netbeans.modules.j2ee.common.source.*; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import javax.swing.SwingUtilities; import org.netbeans.api.db.explorer.ConnectionManager; import org.netbeans.api.db.explorer.DatabaseConnection; import org.netbeans.api.db.explorer.JDBCDriver; import org.netbeans.api.db.explorer.JDBCDriverManager; import org.netbeans.api.project.Project; import org.netbeans.modules.j2ee.deployment.common.api.ConfigurationException; import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; import org.netbeans.modules.j2ee.deployment.common.api.Datasource; /** * * @author John Baker */ public class DatabaseConnectionResolver { private Set brokenDatasources = null; private final static JDBCDriver[] NULL_JDBC_DRIVER_ARRAY = new JDBCDriver[0]; private boolean connStatus = false; // show Add Connection Dialog to allow users to add a connection public boolean addDatabaseConnection(Datasource brokenDatasource, String datasourceName) { // if data source is not null then pass data source parameters to the Add Connection dialog if (brokenDatasource != null) { // next check to see if the driver has been added String driverName = brokenDatasource.getDriverClassName(); final JDBCDriver matchingDriver = findMatchingDriver(driverName); final Datasource edtDS = brokenDatasource; // OK if matchingDriver is null, user can add driver in the Add Connection dialog SwingUtilities.invokeLater(new Runnable() { public void run() { setConnStatus(ConnectionManager.getDefault().showAddConnectionDialogFromEventThread(matchingDriver, edtDS.getUrl(), edtDS.getUsername(), edtDS.getPassword())); } }); } else { // user to add a driver from the Add Connection dialog if not already registered SwingUtilities.invokeLater(new Runnable() { public void run() { setConnStatus(ConnectionManager.getDefault().showAddConnectionDialogFromEventThread(null)); } }); } // setConnStatus sets instance variable, connStatus return connStatus; } // Set the connection status result of adding a connection private synchronized void setConnStatus(DatabaseConnection dbConn) { if (dbConn == null) { connStatus = false; } else { connStatus = true; } } // get project data sources public Set getBrokenDataSources(Project project) { J2eeModuleProvider jmp = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class); Set dss = null; try { dss = jmp.getModuleDatasources(); } catch (ConfigurationException e) { dss = new HashSet(); // TODO: give some feedback to the user } brokenDatasources = new HashSet(); Iterator it = dss.iterator(); while (it.hasNext()) { Datasource ds = (Datasource)it.next(); if(!isFound(ds)){ brokenDatasources.add(ds); } } return brokenDatasources; } // get a specific broken project data source public Datasource getBrokenDataSource(Project project, String item) { Set dss = getBrokenDataSources(project); Iterator it = dss.iterator(); Datasource brokenDs = null; while (it.hasNext()) { brokenDs = (Datasource)it.next(); String name = brokenDs.getJndiName(); name = name.substring(name.indexOf("/")+1); if (name.equals(item)) break; } return brokenDs; } private boolean isFound(Datasource ds) { boolean found = false; String url = ds.getUrl(); String username = ds.getUsername(); DatabaseConnection[] dbConns = ConnectionManager.getDefault().getConnections(); for(int i=0; i