00001 /** 00002 * C-JDBC: Clustered JDBC. 00003 * Copyright (C) 2002-2004 French National Institute For Research In Computer 00004 * Science And Control (INRIA). 00005 * Contact: c-jdbc@objectweb.org 00006 * 00007 * This library is free software; you can redistribute it and/or modify it 00008 * under the terms of the GNU Lesser General Public License as published by the 00009 * Free Software Foundation; either version 2.1 of the License, or any later 00010 * version. 00011 * 00012 * This library is distributed in the hope that it will be useful, but WITHOUT 00013 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00014 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 00015 * for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public License 00018 * along with this library; if not, write to the Free Software Foundation, 00019 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00020 * 00021 * Initial developer(s): Marek Prochazka. 00022 * Contributor(s): 00023 */ 00024 00025 package org.objectweb.cjdbc.driver; 00026 00027 import java.io.PrintWriter; 00028 import java.io.Serializable; 00029 import java.sql.SQLException; 00030 import java.util.Properties; 00031 00032 import javax.naming.NamingException; 00033 import javax.naming.Reference; 00034 import javax.naming.Referenceable; 00035 import javax.naming.StringRefAddr; 00036 00037 /** 00038 * An implementation of the JDBC 2.0 optional package <code>DataSource</code> 00039 * interface. It allows to set the URL, user name, and password to its 00040 * properties. It can be bound via JNDI so that the properties can be set by an 00041 * "application server" and a "ready-to-use" reference to <code>DataSource</code> 00042 * can be retrieved via JNDI. 00043 * 00044 * @author <a href="mailto:Marek.Prochazka@inrialpes.fr">Marek Prochazka </a> 00045 * @version 1.0 00046 */ 00047 public class DataSource 00048 implements 00049 javax.sql.DataSource, 00050 Referenceable, 00051 Serializable 00052 { 00053 00054 /** DataSource properties */ 00055 protected static final String URL_PROPERTY = "url"; 00056 protected static final String USER_PROPERTY = Driver.USER_PROPERTY; 00057 protected static final String PASSWORD_PROPERTY = Driver.PASSWORD_PROPERTY; 00058 protected static final String DRIVER_CLASSNAME = "org.objectweb.cjdbc.driver.Driver"; 00059 protected static final String FACTORY_CLASSNAME = "org.objectweb.cjdbc.driver.DataSourceFactory"; 00060 protected static final String DESCRIPTION_PROPERTY = "description"; 00061 00062 /** Wrapped driver for to get connections. */ 00063 protected static Driver driver = null; 00064 static 00065 { 00066 try 00067 { 00068 driver = (Driver) Class.forName(DRIVER_CLASSNAME).newInstance(); 00069 } 00070 catch (Exception e) 00071 { 00072 throw new RuntimeException("Can't load " + DRIVER_CLASSNAME); 00073 } 00074 } 00075 00076 /** DataSource properties */ 00077 protected String url = null; 00078 protected String user = null; 00079 protected String password = null; 00080 protected PrintWriter logWriter = null; 00081 00082 /** 00083 * Default constructor. 00084 */ 00085 public DataSource() 00086 { 00087 } 00088 00089 //--------------------------------- 00090 //--- DataSource methods 00091 //--------------------------------- 00092 /** 00093 * Gets connection. Retrieves a new connection using the user name and 00094 * password that have been already set. 00095 * 00096 * @throws SQLException if an error occurs. 00097 * @return a new connection. 00098 */ 00099 public java.sql.Connection getConnection() throws SQLException 00100 { 00101 return getConnection(user, password); 00102 } 00103 00104 /** 00105 * Gets connection. Retrieves a new connection using the user name and 00106 * password specified. 00107 * 00108 * @param user user name. 00109 * @param password password. 00110 * @return a new connection. 00111 * @throws SQLException if an error occurs. 00112 */ 00113 public java.sql.Connection getConnection(String user, String password) 00114 throws SQLException 00115 { 00116 if (user == null) 00117 { 00118 user = ""; 00119 } 00120 if (password == null) 00121 { 00122 password = ""; 00123 } 00124 Properties props = new Properties(); 00125 props.put(USER_PROPERTY, user); 00126 props.put(PASSWORD_PROPERTY, password); 00127 00128 return getConnection(props); 00129 } 00130 00131 /** 00132 * Sets the log writer for this data source. 00133 * 00134 * @param output print writer. 00135 * @throws SQLException in case of an error occurs. 00136 */ 00137 public void setLogWriter(PrintWriter output) throws SQLException 00138 { 00139 logWriter = output; 00140 } 00141 00142 /** 00143 * Gets the log writer. 00144 * 00145 * @return log writer. 00146 */ 00147 public java.io.PrintWriter getLogWriter() 00148 { 00149 return logWriter; 00150 } 00151 00152 /** 00153 * Sets the timeout. Actually does nothing. 00154 * 00155 * @param seconds timeout in seconds. 00156 * @throws SQLException in case of an error occurs. 00157 */ 00158 public void setLoginTimeout(int seconds) throws SQLException 00159 { 00160 } 00161 00162 /** 00163 * Gets the login timeout. 00164 * 00165 * @return login timeout 00166 * @throws SQLException in case of an error occurs. 00167 */ 00168 public int getLoginTimeout() throws SQLException 00169 { 00170 return 0; 00171 } 00172 00173 //--------------------------------- 00174 //--- Referenceable methods 00175 //--------------------------------- 00176 /** 00177 * Gets a reference to this. The factory used for this class is the 00178 * {@link DataSourceFactory}class. 00179 * 00180 * @return a reference to this. 00181 * @throws NamingException if <code>DataSourceFactory</code> not found. 00182 */ 00183 public Reference getReference() throws NamingException 00184 { 00185 Reference ref = new Reference(getClass().getName(), FACTORY_CLASSNAME, null); 00186 ref.add(new StringRefAddr(DESCRIPTION_PROPERTY, getDescription())); 00187 ref.add(new StringRefAddr(USER_PROPERTY, getUser())); 00188 ref.add(new StringRefAddr(PASSWORD_PROPERTY, password)); 00189 ref.add(new StringRefAddr(URL_PROPERTY, getUrl())); 00190 return ref; 00191 } 00192 00193 //--------------------------------- 00194 //--- Properties methods 00195 //--------------------------------- 00196 00197 /** 00198 * Return the description of this Datasource with the Driver version number. 00199 * 00200 * @return Datasource description 00201 */ 00202 public String getDescription() 00203 { 00204 return "C-JDBC " + driver.getMajorVersion() + "." 00205 + driver.getMinorVersion() + " Datasource"; 00206 } 00207 00208 /** 00209 * Sets url of the C-JDBC controller(s) to connect. The method is used by the 00210 * "application server" to set the URL (potentially according a deployment 00211 * descriptor). The url is stored in the {@link #URL_PROPERTY}property. 00212 * 00213 * @param url URL to be used to connect C-JDBC controller(s) 00214 */ 00215 public void setUrl(String url) 00216 { 00217 this.url = url; 00218 } 00219 00220 /** 00221 * Sets URL of the C-JDBC controller(s) to connect. The method is used by the 00222 * "application server" to set the URL (potentially according a deployment 00223 * descriptor). The URL is stored in the "url" property. 00224 * 00225 * @param url URL to be used to connect C-JDBC controller(s). 00226 */ 00227 public void setURL(String url) 00228 { 00229 setUrl(url); 00230 } 00231 00232 /** 00233 * Gets url of the C-JDBC controller(s) to connect. The URL is stored in the 00234 * {@link #URL_PROPERTY}property. 00235 * 00236 * @return URL to be used to connect C-JDBC controller(s). 00237 */ 00238 public String getUrl() 00239 { 00240 return url; 00241 } 00242 00243 /** 00244 * Gets URL of the C-JDBC controller(s) to connect. The URL is stored in the 00245 * {@link #URL_PROPERTY}property. 00246 * 00247 * @return URL to be used to connect C-JDBC controller(s). 00248 */ 00249 public String getURL() 00250 { 00251 return getUrl(); 00252 } 00253 00254 /** 00255 * Sets user name to be used to connect the C-JDBC controller(s). The method 00256 * can be used by the "application server" to set the user name (potentially 00257 * according a deployment descriptor). The user name is stored in the 00258 * {@link #USER_PROPERTY}property. 00259 * 00260 * @param userName user name to be used to connect C-JDBC controller(s). 00261 */ 00262 public void setUser(String userName) 00263 { 00264 user = userName; 00265 } 00266 00267 /** 00268 * Gets user name to be used to connect the C-JDBC controller(s). The user 00269 * name is stored in the {@link #USER_PROPERTY}property. 00270 * 00271 * @return user name to be used to connect C-JDBC controller(s). 00272 */ 00273 public String getUser() 00274 { 00275 return user; 00276 } 00277 00278 /** 00279 * Sets password to be used to connect the C-JDBC controller(s). The method 00280 * can be used by the "application server" to set the password (potentially 00281 * according a deployment descriptor). The password is stored in the 00282 * {@link #PASSWORD_PROPERTY}property. Note that there is not a <code>getPassword</code> 00283 * method. 00284 * 00285 * @param pwd password to be used to connect C-JDBC controller(s). 00286 */ 00287 public void setPassword(String pwd) 00288 { 00289 password = pwd; 00290 } 00291 00292 //--------------------------------- 00293 //--- Protected methods 00294 //--------------------------------- 00295 /** 00296 * Creates a connection using the specified properties. 00297 * 00298 * @param props connection properties. 00299 * @throws SQLException if an error occurs. 00300 * @return a new connection. 00301 */ 00302 protected java.sql.Connection getConnection(Properties props) 00303 throws SQLException 00304 { 00305 return driver.connect(url, props); 00306 } 00307 00308 }