1 19 20 package org.netbeans.modules.j2ee.common; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.List ; 25 import org.netbeans.api.db.explorer.ConnectionManager; 26 import org.netbeans.api.db.explorer.DatabaseConnection; 27 import org.netbeans.modules.j2ee.deployment.common.api.Datasource; 28 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 29 30 37 public class DatasourceHelper { 38 39 private DatasourceHelper() { 40 } 41 42 52 public static List <DatabaseConnection> findDatabaseConnections(Datasource datasource) { 53 if (datasource == null) { 54 throw new NullPointerException ("The datasource parameter cannot be null."); } 56 String databaseUrl = datasource.getUrl(); 57 String user = datasource.getUsername(); 58 if (databaseUrl == null || user == null) { 59 return Collections.emptyList(); 60 } 61 List <DatabaseConnection> result = new ArrayList <DatabaseConnection>(); 62 for (DatabaseConnection dbconn : ConnectionManager.getDefault().getConnections()) { 63 if (databaseUrl.equals(dbconn.getDatabaseURL()) && user.equals(dbconn.getUser())) { 64 result.add(dbconn); 65 } 66 } 67 if (result.size() > 0) { 68 return Collections.unmodifiableList(result); 69 } else { 70 return Collections.emptyList(); 71 } 72 } 73 74 89 public static Datasource findDatasource(J2eeModuleProvider provider, String jndiName) { 90 if (provider == null) { 91 throw new NullPointerException ("The provider parameter cannot be null."); } 93 if (jndiName == null) { 94 throw new NullPointerException ("The jndiName parameter cannot be null."); } 96 for (Datasource datasource : provider.getServerDatasources()) { 97 if (jndiName.equals(datasource.getJndiName())) { 98 return datasource; 99 } 100 } 101 for (Datasource datasource : provider.getModuleDatasources()) { 102 if (jndiName.equals(datasource.getJndiName())) { 103 return datasource; 104 } 105 } 106 return null; 107 } 108 } 109 | Popular Tags |