Added
Link Here
|
1 |
/* |
2 |
* The contents of this file are subject to the terms of the Common Development |
3 |
* and Distribution License (the License). You may not use this file except in |
4 |
* compliance with the License. |
5 |
* |
6 |
* You can obtain a copy of the License at http://www.netbeans.org/cddl.html |
7 |
* or http://www.netbeans.org/cddl.txt. |
8 |
* |
9 |
* When distributing Covered Code, include this CDDL Header Notice in each file |
10 |
* and include the License file at http://www.netbeans.org/cddl.txt. |
11 |
* If applicable, add the following below the CDDL Header, with the fields |
12 |
* enclosed by brackets [] replaced by your own identifying information: |
13 |
* "Portions Copyrighted [year] [name of copyright owner]" |
14 |
* |
15 |
* The Original Software is NetBeans. The Initial Developer of the Original |
16 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
17 |
* Microsystems, Inc. All Rights Reserved. |
18 |
*/ |
19 |
|
20 |
package org.netbeans.modules.j2ee.deployment.common.api; |
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.devmodules.spi.J2eeModuleProvider; |
28 |
|
29 |
/** |
30 |
* An utility class for working with data sources. |
31 |
* |
32 |
* @author Andrei Badea |
33 |
* |
34 |
* @since 1.24 |
35 |
*/ |
36 |
public class DatasourceHelper { |
37 |
|
38 |
private DatasourceHelper() { |
39 |
} |
40 |
|
41 |
/** |
42 |
* Finds the database connections whose database URL and user name equal |
43 |
* the database URL and the user name of the passed data source. |
44 |
* |
45 |
* @param datasource the data source. |
46 |
* |
47 |
* @return the list of database connections; never null. |
48 |
* |
49 |
* @throws NullPointerException if the datasource parameter was null. |
50 |
*/ |
51 |
public static List<DatabaseConnection> findDatabaseConnections(Datasource datasource) { |
52 |
if (datasource == null) { |
53 |
throw new NullPointerException("The datasource parameter cannot be null."); // NOI18N |
54 |
} |
55 |
String databaseUrl = datasource.getUrl(); |
56 |
String user = datasource.getUsername(); |
57 |
if (databaseUrl == null || user == null) { |
58 |
return Collections.emptyList(); |
59 |
} |
60 |
List<DatabaseConnection> result = new ArrayList<DatabaseConnection>(); |
61 |
for (DatabaseConnection dbconn : ConnectionManager.getDefault().getConnections()) { |
62 |
if (databaseUrl.equals(dbconn.getDatabaseURL()) && user.equals(dbconn.getUser())) { |
63 |
result.add(dbconn); |
64 |
} |
65 |
} |
66 |
if (result.size() > 0) { |
67 |
return Collections.unmodifiableList(result); |
68 |
} else { |
69 |
return Collections.emptyList(); |
70 |
} |
71 |
} |
72 |
|
73 |
/** |
74 |
* Finds the data source with the given JNDI name in the module and |
75 |
* project data sources of the given provider. |
76 |
* |
77 |
* @param provider the {@link J2eeModuleProvider provider} whose data sources |
78 |
* are to be searched; cannot be null. |
79 |
* @param jndiName the JNDI name to search for; cannot be null. |
80 |
* |
81 |
* @return the found data source or null if no data source was found. |
82 |
* |
83 |
* @throws NullPointerException if either the <code>provider</code> |
84 |
* or the <code>jndiName</code> parameter was null. |
85 |
*/ |
86 |
public static Datasource findDatasource(J2eeModuleProvider provider, String jndiName) |
87 |
throws ConfigurationException { |
88 |
if (provider == null) { |
89 |
throw new NullPointerException("The provider parameter cannot be null."); // NOI18N |
90 |
} |
91 |
if (jndiName == null) { |
92 |
throw new NullPointerException("The jndiName parameter cannot be null."); // NOI18N |
93 |
} |
94 |
for (Datasource datasource : provider.getServerDatasources()) { |
95 |
if (jndiName.equals(datasource.getJndiName())) { |
96 |
return datasource; |
97 |
} |
98 |
} |
99 |
for (Datasource datasource : provider.getModuleDatasources()) { |
100 |
if (jndiName.equals(datasource.getJndiName())) { |
101 |
return datasource; |
102 |
} |
103 |
} |
104 |
return null; |
105 |
} |
106 |
} |