KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tomcat5 > config > TomcatDatasourceManager


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.tomcat5.config;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
29 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
30 import org.netbeans.modules.j2ee.deployment.common.api.Datasource;
31 import org.netbeans.modules.j2ee.deployment.common.api.DatasourceAlreadyExistsException;
32 import org.netbeans.modules.j2ee.deployment.plugins.api.DatasourceManager;
33 import org.netbeans.modules.tomcat5.TomcatManager;
34 import org.netbeans.modules.tomcat5.TomcatManager.TomcatVersion;
35 import org.netbeans.modules.tomcat5.config.gen.GlobalNamingResources;
36 import org.netbeans.modules.tomcat5.config.gen.Parameter;
37 import org.netbeans.modules.tomcat5.config.gen.ResourceParams;
38 import org.netbeans.modules.tomcat5.config.gen.Server;
39 import org.openide.ErrorManager;
40
41 /**
42  * DataSourceManager implementation
43  *
44  * @author sherold
45  */

46 public class TomcatDatasourceManager implements DatasourceManager {
47     
48     private final TomcatManager tm;
49     
50     /**
51      * Creates a new instance of TomcatDatasourceManager
52      */

53     public TomcatDatasourceManager(DeploymentManager JavaDoc dm) {
54         tm = (TomcatManager) dm;
55     }
56
57     /**
58      * Get the global datasources defined in the GlobalNamingResources element
59      * in the server.xml configuration file.
60      */

61     public Set JavaDoc<Datasource> getDatasources() {
62         HashSet JavaDoc<Datasource> result = new HashSet JavaDoc<Datasource>();
63         File JavaDoc serverXml = tm.getTomcatProperties().getServerXml();
64         Server server;
65         try {
66             server = Server.createGraph(serverXml);
67         } catch (IOException JavaDoc e) {
68             // ok, log it and give up
69
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
70             return Collections.<Datasource>emptySet();
71         } catch (RuntimeException JavaDoc e) {
72             // server.xml file is most likely not parseable, log it and give up
73
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
74             return Collections.<Datasource>emptySet();
75         }
76         GlobalNamingResources[] globalNamingResources = server.getGlobalNamingResources();
77         if (globalNamingResources.length > 0) {
78             // only one GlobalNamingResources element is allowed
79
GlobalNamingResources globalNR = globalNamingResources[0];
80             if (tm.getTomcatVersion() != TomcatVersion.TOMCAT_50) {
81                 // Tomcat 5.5.x or Tomcat 6.0.x
82
int length = globalNR.getResource().length;
83                 for (int i = 0; i < length; i++) {
84                     String JavaDoc type = globalNR.getResourceType(i);
85                     if ("javax.sql.DataSource".equals(type)) { // NOI18N
86
String JavaDoc name = globalNR.getResourceName(i);
87                         String JavaDoc username = globalNR.getResourceUsername(i);
88                         String JavaDoc url = globalNR.getResourceUrl(i);
89                         String JavaDoc password = globalNR.getResourcePassword(i);
90                         String JavaDoc driverClassName = globalNR.getResourceDriverClassName(i);
91                         if (name != null && username != null && url != null && driverClassName != null) {
92                             // return the datasource only if all the needed params are non-null except the password param
93
result.add(new TomcatDatasource(username, url, password, name, driverClassName));
94                         }
95                     }
96                 }
97             } else {
98                 // Tomcat 5.0.x
99
int length = globalNR.getResource().length;
100                 ResourceParams[] resourceParams = globalNR.getResourceParams();
101                 for (int i = 0; i < length; i++) {
102                     String JavaDoc type = globalNR.getResourceType(i);
103                     if ("javax.sql.DataSource".equals(type)) { // NOI18N
104
String JavaDoc name = globalNR.getResourceName(i);
105                         // find the resource params for the selected resource
106
for (int j = 0; j < resourceParams.length; j++) {
107                             if (name.equals(resourceParams[j].getName())) {
108                                 Parameter[] params = resourceParams[j].getParameter();
109                                 HashMap JavaDoc paramNameValueMap = new HashMap JavaDoc(params.length);
110                                 for (Parameter parameter : params) {
111                                     paramNameValueMap.put(parameter.getName(), parameter.getValue());
112                                 }
113                                 String JavaDoc username = (String JavaDoc) paramNameValueMap.get("username"); // NOI18N
114
String JavaDoc url = (String JavaDoc) paramNameValueMap.get("url"); // NOI18N
115
String JavaDoc password = (String JavaDoc) paramNameValueMap.get("password"); // NOI18N
116
String JavaDoc driverClassName = (String JavaDoc) paramNameValueMap.get("driverClassName"); // NOI18N
117
if (username != null && url != null && driverClassName != null) {
118                                     // return the datasource only if all the needed params are non-null except the password param
119
result.add(new TomcatDatasource(username, url, password, name, driverClassName));
120                                 }
121                             }
122                         }
123                     }
124                 }
125             }
126         }
127         return result;
128     }
129
130     public void deployDatasources(Set JavaDoc<Datasource> datasources)
131     throws ConfigurationException JavaDoc, DatasourceAlreadyExistsException {
132         // nothing needs to be done here
133
}
134     
135 }
136
Popular Tags