KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > common > DatasourceHelper


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.common;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
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 /**
31  * An utility class for working with data sources.
32  *
33  * @author Andrei Badea
34  *
35  * @since 1.7
36  */

37 public class DatasourceHelper {
38
39     private DatasourceHelper() {
40     }
41
42     /**
43      * Finds the database connections whose database URL and user name equal
44      * the database URL and the user name of the passed data source.
45      *
46      * @param datasource the data source.
47      *
48      * @return the list of database connections; never null.
49      *
50      * @throws NullPointerException if the datasource parameter was null.
51      */

52     public static List JavaDoc<DatabaseConnection> findDatabaseConnections(Datasource datasource) {
53         if (datasource == null) {
54             throw new NullPointerException JavaDoc("The datasource parameter cannot be null."); // NOI18N
55
}
56         String JavaDoc databaseUrl = datasource.getUrl();
57         String JavaDoc user = datasource.getUsername();
58         if (databaseUrl == null || user == null) {
59             return Collections.emptyList();
60         }
61         List JavaDoc<DatabaseConnection> result = new ArrayList JavaDoc<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     /**
75      * Finds the data source with the given JNDI name in the module and
76      * project data sources of the given provider.
77      *
78      * @param provider the {@link J2eeModuleProvider provider} whose data sources
79      * are to be searched; cannot be null.
80      * @param jndiName the JNDI name to search for; cannot be null.
81      *
82      * @return the found data source or null if no data source was found.
83      *
84      * @throws NullPointerException if either the <code>provider</code>
85      * or the <code>jndiName</code> parameter was null.
86      *
87      * @since 1.11
88      */

89     public static Datasource findDatasource(J2eeModuleProvider provider, String JavaDoc jndiName) {
90         if (provider == null) {
91             throw new NullPointerException JavaDoc("The provider parameter cannot be null."); // NOI18N
92
}
93         if (jndiName == null) {
94             throw new NullPointerException JavaDoc("The jndiName parameter cannot be null."); // NOI18N
95
}
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