1 19 20 package org.netbeans.modules.tomcat5.config; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.Set ; 28 import javax.enterprise.deploy.spi.DeploymentManager ; 29 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 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 46 public class TomcatDatasourceManager implements DatasourceManager { 47 48 private final TomcatManager tm; 49 50 53 public TomcatDatasourceManager(DeploymentManager dm) { 54 tm = (TomcatManager) dm; 55 } 56 57 61 public Set <Datasource> getDatasources() { 62 HashSet <Datasource> result = new HashSet <Datasource>(); 63 File serverXml = tm.getTomcatProperties().getServerXml(); 64 Server server; 65 try { 66 server = Server.createGraph(serverXml); 67 } catch (IOException e) { 68 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 70 return Collections.<Datasource>emptySet(); 71 } catch (RuntimeException e) { 72 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 74 return Collections.<Datasource>emptySet(); 75 } 76 GlobalNamingResources[] globalNamingResources = server.getGlobalNamingResources(); 77 if (globalNamingResources.length > 0) { 78 GlobalNamingResources globalNR = globalNamingResources[0]; 80 if (tm.getTomcatVersion() != TomcatVersion.TOMCAT_50) { 81 int length = globalNR.getResource().length; 83 for (int i = 0; i < length; i++) { 84 String type = globalNR.getResourceType(i); 85 if ("javax.sql.DataSource".equals(type)) { String name = globalNR.getResourceName(i); 87 String username = globalNR.getResourceUsername(i); 88 String url = globalNR.getResourceUrl(i); 89 String password = globalNR.getResourcePassword(i); 90 String driverClassName = globalNR.getResourceDriverClassName(i); 91 if (name != null && username != null && url != null && driverClassName != null) { 92 result.add(new TomcatDatasource(username, url, password, name, driverClassName)); 94 } 95 } 96 } 97 } else { 98 int length = globalNR.getResource().length; 100 ResourceParams[] resourceParams = globalNR.getResourceParams(); 101 for (int i = 0; i < length; i++) { 102 String type = globalNR.getResourceType(i); 103 if ("javax.sql.DataSource".equals(type)) { String name = globalNR.getResourceName(i); 105 for (int j = 0; j < resourceParams.length; j++) { 107 if (name.equals(resourceParams[j].getName())) { 108 Parameter[] params = resourceParams[j].getParameter(); 109 HashMap paramNameValueMap = new HashMap (params.length); 110 for (Parameter parameter : params) { 111 paramNameValueMap.put(parameter.getName(), parameter.getValue()); 112 } 113 String username = (String ) paramNameValueMap.get("username"); String url = (String ) paramNameValueMap.get("url"); String password = (String ) paramNameValueMap.get("password"); String driverClassName = (String ) paramNameValueMap.get("driverClassName"); if (username != null && url != null && driverClassName != null) { 118 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 <Datasource> datasources) 131 throws ConfigurationException , DatasourceAlreadyExistsException { 132 } 134 135 } 136 | Popular Tags |