001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.tomcat5.config;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.Collections;
047: import java.util.HashMap;
048: import java.util.HashSet;
049: import java.util.Set;
050: import java.util.logging.Level;
051: import java.util.logging.Logger;
052: import javax.enterprise.deploy.spi.DeploymentManager;
053: import org.netbeans.modules.j2ee.deployment.common.api.ConfigurationException;
054: import org.netbeans.modules.j2ee.deployment.common.api.Datasource;
055: import org.netbeans.modules.j2ee.deployment.common.api.DatasourceAlreadyExistsException;
056: import org.netbeans.modules.j2ee.deployment.plugins.spi.DatasourceManager;
057: import org.netbeans.modules.tomcat5.TomcatManager;
058: import org.netbeans.modules.tomcat5.TomcatManager.TomcatVersion;
059: import org.netbeans.modules.tomcat5.config.gen.GlobalNamingResources;
060: import org.netbeans.modules.tomcat5.config.gen.Parameter;
061: import org.netbeans.modules.tomcat5.config.gen.ResourceParams;
062: import org.netbeans.modules.tomcat5.config.gen.Server;
063:
064: /**
065: * DataSourceManager implementation
066: *
067: * @author sherold
068: */
069: public class TomcatDatasourceManager implements DatasourceManager {
070:
071: private final TomcatManager tm;
072:
073: /**
074: * Creates a new instance of TomcatDatasourceManager
075: */
076: public TomcatDatasourceManager(DeploymentManager dm) {
077: tm = (TomcatManager) dm;
078: }
079:
080: /**
081: * Get the global datasources defined in the GlobalNamingResources element
082: * in the server.xml configuration file.
083: */
084: public Set<Datasource> getDatasources() {
085: HashSet<Datasource> result = new HashSet<Datasource>();
086: File serverXml = tm.getTomcatProperties().getServerXml();
087: Server server;
088: try {
089: server = Server.createGraph(serverXml);
090: } catch (IOException e) {
091: // ok, log it and give up
092: Logger.getLogger(TomcatDatasourceManager.class.getName())
093: .log(Level.INFO, null, e);
094: return Collections.<Datasource> emptySet();
095: } catch (RuntimeException e) {
096: // server.xml file is most likely not parseable, log it and give up
097: Logger.getLogger(TomcatDatasourceManager.class.getName())
098: .log(Level.INFO, null, e);
099: return Collections.<Datasource> emptySet();
100: }
101: GlobalNamingResources[] globalNamingResources = server
102: .getGlobalNamingResources();
103: if (globalNamingResources.length > 0) {
104: // only one GlobalNamingResources element is allowed
105: GlobalNamingResources globalNR = globalNamingResources[0];
106: if (tm.getTomcatVersion() != TomcatVersion.TOMCAT_50) {
107: // Tomcat 5.5.x or Tomcat 6.0.x
108: int length = globalNR.getResource().length;
109: for (int i = 0; i < length; i++) {
110: String type = globalNR.getResourceType(i);
111: if ("javax.sql.DataSource".equals(type)) { // NOI18N
112: String name = globalNR.getResourceName(i);
113: String username = globalNR
114: .getResourceUsername(i);
115: String url = globalNR.getResourceUrl(i);
116: String password = globalNR
117: .getResourcePassword(i);
118: String driverClassName = globalNR
119: .getResourceDriverClassName(i);
120: if (name != null && username != null
121: && url != null
122: && driverClassName != null) {
123: // return the datasource only if all the needed params are non-null except the password param
124: result.add(new TomcatDatasource(username,
125: url, password, name,
126: driverClassName));
127: }
128: }
129: }
130: } else {
131: // Tomcat 5.0.x
132: int length = globalNR.getResource().length;
133: ResourceParams[] resourceParams = globalNR
134: .getResourceParams();
135: for (int i = 0; i < length; i++) {
136: String type = globalNR.getResourceType(i);
137: if ("javax.sql.DataSource".equals(type)) { // NOI18N
138: String name = globalNR.getResourceName(i);
139: // find the resource params for the selected resource
140: for (int j = 0; j < resourceParams.length; j++) {
141: if (name
142: .equals(resourceParams[j].getName())) {
143: Parameter[] params = resourceParams[j]
144: .getParameter();
145: HashMap paramNameValueMap = new HashMap(
146: params.length);
147: for (Parameter parameter : params) {
148: paramNameValueMap.put(parameter
149: .getName(), parameter
150: .getValue());
151: }
152: String username = (String) paramNameValueMap
153: .get("username"); // NOI18N
154: String url = (String) paramNameValueMap
155: .get("url"); // NOI18N
156: String password = (String) paramNameValueMap
157: .get("password"); // NOI18N
158: String driverClassName = (String) paramNameValueMap
159: .get("driverClassName"); // NOI18N
160: if (username != null && url != null
161: && driverClassName != null) {
162: // return the datasource only if all the needed params are non-null except the password param
163: result.add(new TomcatDatasource(
164: username, url, password,
165: name, driverClassName));
166: }
167: }
168: }
169: }
170: }
171: }
172: }
173: return result;
174: }
175:
176: public void deployDatasources(Set<Datasource> datasources)
177: throws ConfigurationException,
178: DatasourceAlreadyExistsException {
179: // nothing needs to be done here
180: }
181:
182: }
|