PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/c_jdbc-2.0.2/src/org/objectweb/cjdbc/driver/DataSource.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 308 lines | 123 code | 27 blank | 158 comment | 4 complexity | a5905e23e678e2ddd96577f098b53734 MD5 | raw file
  1. /**
  2. * C-JDBC: Clustered JDBC.
  3. * Copyright (C) 2002-2004 French National Institute For Research In Computer
  4. * Science And Control (INRIA).
  5. * Contact: c-jdbc@objectweb.org
  6. *
  7. * This library is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as published by the
  9. * Free Software Foundation; either version 2.1 of the License, or any later
  10. * version.
  11. *
  12. * This library is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  15. * for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software Foundation,
  19. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  20. *
  21. * Initial developer(s): Marek Prochazka.
  22. * Contributor(s):
  23. */
  24. package org.objectweb.cjdbc.driver;
  25. import java.io.PrintWriter;
  26. import java.io.Serializable;
  27. import java.sql.SQLException;
  28. import java.util.Properties;
  29. import javax.naming.NamingException;
  30. import javax.naming.Reference;
  31. import javax.naming.Referenceable;
  32. import javax.naming.StringRefAddr;
  33. /**
  34. * An implementation of the JDBC 2.0 optional package <code>DataSource</code>
  35. * interface. It allows to set the URL, user name, and password to its
  36. * properties. It can be bound via JNDI so that the properties can be set by an
  37. * "application server" and a "ready-to-use" reference to <code>DataSource</code>
  38. * can be retrieved via JNDI.
  39. *
  40. * @author <a href="mailto:Marek.Prochazka@inrialpes.fr">Marek Prochazka </a>
  41. * @version 1.0
  42. */
  43. public class DataSource
  44. implements
  45. javax.sql.DataSource,
  46. Referenceable,
  47. Serializable
  48. {
  49. /** DataSource properties */
  50. protected static final String URL_PROPERTY = "url";
  51. protected static final String USER_PROPERTY = Driver.USER_PROPERTY;
  52. protected static final String PASSWORD_PROPERTY = Driver.PASSWORD_PROPERTY;
  53. protected static final String DRIVER_CLASSNAME = "org.objectweb.cjdbc.driver.Driver";
  54. protected static final String FACTORY_CLASSNAME = "org.objectweb.cjdbc.driver.DataSourceFactory";
  55. protected static final String DESCRIPTION_PROPERTY = "description";
  56. /** Wrapped driver for to get connections. */
  57. protected static Driver driver = null;
  58. static
  59. {
  60. try
  61. {
  62. driver = (Driver) Class.forName(DRIVER_CLASSNAME).newInstance();
  63. }
  64. catch (Exception e)
  65. {
  66. throw new RuntimeException("Can't load " + DRIVER_CLASSNAME);
  67. }
  68. }
  69. /** DataSource properties */
  70. protected String url = null;
  71. protected String user = null;
  72. protected String password = null;
  73. protected PrintWriter logWriter = null;
  74. /**
  75. * Default constructor.
  76. */
  77. public DataSource()
  78. {
  79. }
  80. //---------------------------------
  81. //--- DataSource methods
  82. //---------------------------------
  83. /**
  84. * Gets connection. Retrieves a new connection using the user name and
  85. * password that have been already set.
  86. *
  87. * @throws SQLException if an error occurs.
  88. * @return a new connection.
  89. */
  90. public java.sql.Connection getConnection() throws SQLException
  91. {
  92. return getConnection(user, password);
  93. }
  94. /**
  95. * Gets connection. Retrieves a new connection using the user name and
  96. * password specified.
  97. *
  98. * @param user user name.
  99. * @param password password.
  100. * @return a new connection.
  101. * @throws SQLException if an error occurs.
  102. */
  103. public java.sql.Connection getConnection(String user, String password)
  104. throws SQLException
  105. {
  106. if (user == null)
  107. {
  108. user = "";
  109. }
  110. if (password == null)
  111. {
  112. password = "";
  113. }
  114. Properties props = new Properties();
  115. props.put(USER_PROPERTY, user);
  116. props.put(PASSWORD_PROPERTY, password);
  117. return getConnection(props);
  118. }
  119. /**
  120. * Sets the log writer for this data source.
  121. *
  122. * @param output print writer.
  123. * @throws SQLException in case of an error occurs.
  124. */
  125. public void setLogWriter(PrintWriter output) throws SQLException
  126. {
  127. logWriter = output;
  128. }
  129. /**
  130. * Gets the log writer.
  131. *
  132. * @return log writer.
  133. */
  134. public java.io.PrintWriter getLogWriter()
  135. {
  136. return logWriter;
  137. }
  138. /**
  139. * Sets the timeout. Actually does nothing.
  140. *
  141. * @param seconds timeout in seconds.
  142. * @throws SQLException in case of an error occurs.
  143. */
  144. public void setLoginTimeout(int seconds) throws SQLException
  145. {
  146. }
  147. /**
  148. * Gets the login timeout.
  149. *
  150. * @return login timeout
  151. * @throws SQLException in case of an error occurs.
  152. */
  153. public int getLoginTimeout() throws SQLException
  154. {
  155. return 0;
  156. }
  157. //---------------------------------
  158. //--- Referenceable methods
  159. //---------------------------------
  160. /**
  161. * Gets a reference to this. The factory used for this class is the
  162. * {@link DataSourceFactory}class.
  163. *
  164. * @return a reference to this.
  165. * @throws NamingException if <code>DataSourceFactory</code> not found.
  166. */
  167. public Reference getReference() throws NamingException
  168. {
  169. Reference ref = new Reference(getClass().getName(), FACTORY_CLASSNAME, null);
  170. ref.add(new StringRefAddr(DESCRIPTION_PROPERTY, getDescription()));
  171. ref.add(new StringRefAddr(USER_PROPERTY, getUser()));
  172. ref.add(new StringRefAddr(PASSWORD_PROPERTY, password));
  173. ref.add(new StringRefAddr(URL_PROPERTY, getUrl()));
  174. return ref;
  175. }
  176. //---------------------------------
  177. //--- Properties methods
  178. //---------------------------------
  179. /**
  180. * Return the description of this Datasource with the Driver version number.
  181. *
  182. * @return Datasource description
  183. */
  184. public String getDescription()
  185. {
  186. return "C-JDBC " + driver.getMajorVersion() + "."
  187. + driver.getMinorVersion() + " Datasource";
  188. }
  189. /**
  190. * Sets url of the C-JDBC controller(s) to connect. The method is used by the
  191. * "application server" to set the URL (potentially according a deployment
  192. * descriptor). The url is stored in the {@link #URL_PROPERTY}property.
  193. *
  194. * @param url URL to be used to connect C-JDBC controller(s)
  195. */
  196. public void setUrl(String url)
  197. {
  198. this.url = url;
  199. }
  200. /**
  201. * Sets URL of the C-JDBC controller(s) to connect. The method is used by the
  202. * "application server" to set the URL (potentially according a deployment
  203. * descriptor). The URL is stored in the "url" property.
  204. *
  205. * @param url URL to be used to connect C-JDBC controller(s).
  206. */
  207. public void setURL(String url)
  208. {
  209. setUrl(url);
  210. }
  211. /**
  212. * Gets url of the C-JDBC controller(s) to connect. The URL is stored in the
  213. * {@link #URL_PROPERTY}property.
  214. *
  215. * @return URL to be used to connect C-JDBC controller(s).
  216. */
  217. public String getUrl()
  218. {
  219. return url;
  220. }
  221. /**
  222. * Gets URL of the C-JDBC controller(s) to connect. The URL is stored in the
  223. * {@link #URL_PROPERTY}property.
  224. *
  225. * @return URL to be used to connect C-JDBC controller(s).
  226. */
  227. public String getURL()
  228. {
  229. return getUrl();
  230. }
  231. /**
  232. * Sets user name to be used to connect the C-JDBC controller(s). The method
  233. * can be used by the "application server" to set the user name (potentially
  234. * according a deployment descriptor). The user name is stored in the
  235. * {@link #USER_PROPERTY}property.
  236. *
  237. * @param userName user name to be used to connect C-JDBC controller(s).
  238. */
  239. public void setUser(String userName)
  240. {
  241. user = userName;
  242. }
  243. /**
  244. * Gets user name to be used to connect the C-JDBC controller(s). The user
  245. * name is stored in the {@link #USER_PROPERTY}property.
  246. *
  247. * @return user name to be used to connect C-JDBC controller(s).
  248. */
  249. public String getUser()
  250. {
  251. return user;
  252. }
  253. /**
  254. * Sets password to be used to connect the C-JDBC controller(s). The method
  255. * can be used by the "application server" to set the password (potentially
  256. * according a deployment descriptor). The password is stored in the
  257. * {@link #PASSWORD_PROPERTY}property. Note that there is not a <code>getPassword</code>
  258. * method.
  259. *
  260. * @param pwd password to be used to connect C-JDBC controller(s).
  261. */
  262. public void setPassword(String pwd)
  263. {
  264. password = pwd;
  265. }
  266. //---------------------------------
  267. //--- Protected methods
  268. //---------------------------------
  269. /**
  270. * Creates a connection using the specified properties.
  271. *
  272. * @param props connection properties.
  273. * @throws SQLException if an error occurs.
  274. * @return a new connection.
  275. */
  276. protected java.sql.Connection getConnection(Properties props)
  277. throws SQLException
  278. {
  279. return driver.connect(url, props);
  280. }
  281. }