Quadcap Embeddable Server

com/quadcap/services/DataSource.java

Go to the documentation of this file.
00001 package com.quadcap.services; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.PrintWriter; 00042 00043 import java.util.Properties; 00044 00045 import java.sql.Driver; 00046 import java.sql.DriverManager; 00047 import java.sql.SQLException; 00048 00049 import javax.naming.Reference; 00050 import javax.naming.Referenceable; 00051 import javax.naming.StringRefAddr; 00052 00053 /** 00054 * This class implements the <code>javax.sql.DataSource</code> interface, 00055 * which provides a mechanism for obtaining <code>Connection</code> 00056 * objects. 00057 * 00058 * @author Stan Bailes 00059 */ 00060 public class DataSource implements javax.sql.DataSource, Referenceable { 00061 String name; 00062 String driverClass = null; 00063 PrintWriter logWriter = null; 00064 int loginTimeout = 0; 00065 String url; 00066 Properties props = new Properties(); 00067 Driver driver = null; 00068 00069 /** 00070 * Default conctructor 00071 */ 00072 public DataSource() { 00073 } 00074 00075 public void setName(String name) { 00076 this.name = name; 00077 } 00078 00079 public String getName() { 00080 return name; 00081 } 00082 00083 public void setDriverClass(String s) { 00084 this.driverClass = s; 00085 try { 00086 Class.forName(driverClass); 00087 } catch (Throwable t) { 00088 } 00089 } 00090 00091 public String getDriverClass() { 00092 return driverClass; 00093 } 00094 00095 /** 00096 * Return the URL to which this data source is bound. 00097 */ 00098 public String getUrl() { return url; } 00099 00100 /** 00101 * Setter method for specifing this data source's URL. 00102 */ 00103 public void setUrl(String url) { 00104 this.url = url; 00105 } 00106 00107 /** 00108 * Add a connection property for this data source. 00109 * 00110 * @param name the connection property's name 00111 * @param val the connection property's value 00112 */ 00113 public void addConnectionProperty(String name, String val) { 00114 props.setProperty(name, val); 00115 } 00116 00117 /** 00118 * Get a new Connection bound to this data source. 00119 * 00120 * @return a new Connection 00121 * @exception SQLException may be thrown 00122 */ 00123 public java.sql.Connection getConnection() 00124 throws SQLException 00125 { 00126 return getDriver().connect(url, props); 00127 } 00128 00129 /** 00130 * Get a new Connection bound to this data source. 00131 * 00132 * @param username the database user for whom the connection is being 00133 * made. 00134 * @param password the user's password. 00135 * 00136 * @return a new Connection 00137 * @exception SQLException may be thrown 00138 */ 00139 public java.sql.Connection getConnection(String username, String password) 00140 throws SQLException 00141 { 00142 Properties p = new Properties(props); 00143 p.setProperty("user", username); 00144 p.setProperty("password", password); 00145 return getDriver().connect(url, p); 00146 } 00147 00148 /** 00149 * Get the log writer for messages associated with this data source 00150 */ 00151 public PrintWriter getLogWriter() { 00152 return logWriter; 00153 } 00154 00155 /** 00156 * Set the log writer for debugging messages associated with this 00157 * data source. 00158 * 00159 * <p>Note: this writer isn't used for anything in this release 00160 * of QED. 00161 * 00162 * @param w the writer 00163 */ 00164 public void setLogWriter(PrintWriter w) { 00165 this.logWriter = w; 00166 } 00167 00168 /** 00169 * Get the login timeout. 00170 * 00171 * <p>Not implemented in this QED release. 00172 */ 00173 public int getLoginTimeout() { 00174 return loginTimeout; 00175 } 00176 00177 /** 00178 * Set the login timeout. 00179 * 00180 * <p>Not implemented in this QED release. 00181 * 00182 * @param timeout the timeout in seconds 00183 */ 00184 public void setLoginTimeout(int timeout) { 00185 this.loginTimeout = timeout; 00186 } 00187 00188 public Reference getReference() { 00189 Reference ref = new Reference(DataSource.class.getName(), 00190 "com.quadcap.services.DataSources", 00191 null); 00192 ref.add(new StringRefAddr("name", name)); 00193 return ref; 00194 } 00195 00196 final private Driver getDriver() throws SQLException { 00197 if (driver == null) { 00198 driver = DriverManager.getDriver(url); 00199 } 00200 return driver; 00201 } 00202 } 00203