PageRenderTime 34ms CodeModel.GetById 27ms app.highlight 5ms RepoModel.GetById 1ms 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
 25package org.objectweb.cjdbc.driver;
 26
 27import java.io.PrintWriter;
 28import java.io.Serializable;
 29import java.sql.SQLException;
 30import java.util.Properties;
 31
 32import javax.naming.NamingException;
 33import javax.naming.Reference;
 34import javax.naming.Referenceable;
 35import javax.naming.StringRefAddr;
 36
 37/**
 38 * An implementation of the JDBC 2.0 optional package <code>DataSource</code>
 39 * interface. It allows to set the URL, user name, and password to its
 40 * properties. It can be bound via JNDI so that the properties can be set by an
 41 * "application server" and a "ready-to-use" reference to <code>DataSource</code>
 42 * can be retrieved via JNDI.
 43 * 
 44 * @author <a href="mailto:Marek.Prochazka@inrialpes.fr">Marek Prochazka </a>
 45 * @version 1.0
 46 */
 47public class DataSource
 48    implements
 49      javax.sql.DataSource,
 50      Referenceable,
 51      Serializable
 52{
 53
 54  /** DataSource properties */
 55  protected static final String URL_PROPERTY         = "url";
 56  protected static final String USER_PROPERTY        = Driver.USER_PROPERTY;
 57  protected static final String PASSWORD_PROPERTY    = Driver.PASSWORD_PROPERTY;
 58  protected static final String DRIVER_CLASSNAME     = "org.objectweb.cjdbc.driver.Driver";
 59  protected static final String FACTORY_CLASSNAME    = "org.objectweb.cjdbc.driver.DataSourceFactory";
 60  protected static final String DESCRIPTION_PROPERTY = "description";
 61
 62  /** Wrapped driver for to get connections. */
 63  protected static Driver       driver               = null;
 64  static
 65  {
 66    try
 67    {
 68      driver = (Driver) Class.forName(DRIVER_CLASSNAME).newInstance();
 69    }
 70    catch (Exception e)
 71    {
 72      throw new RuntimeException("Can't load " + DRIVER_CLASSNAME);
 73    }
 74  }
 75
 76  /** DataSource properties */
 77  protected String              url                  = null;
 78  protected String              user                 = null;
 79  protected String              password             = null;
 80  protected PrintWriter         logWriter            = null;
 81
 82  /**
 83   * Default constructor.
 84   */
 85  public DataSource()
 86  {
 87  }
 88
 89  //---------------------------------
 90  //--- DataSource methods
 91  //---------------------------------
 92  /**
 93   * Gets connection. Retrieves a new connection using the user name and
 94   * password that have been already set.
 95   * 
 96   * @throws SQLException if an error occurs.
 97   * @return a new connection.
 98   */
 99  public java.sql.Connection getConnection() throws SQLException
100  {
101    return getConnection(user, password);
102  }
103
104  /**
105   * Gets connection. Retrieves a new connection using the user name and
106   * password specified.
107   * 
108   * @param user user name.
109   * @param password password.
110   * @return a new connection.
111   * @throws SQLException if an error occurs.
112   */
113  public java.sql.Connection getConnection(String user, String password)
114      throws SQLException
115  {
116    if (user == null)
117    {
118      user = "";
119    }
120    if (password == null)
121    {
122      password = "";
123    }
124    Properties props = new Properties();
125    props.put(USER_PROPERTY, user);
126    props.put(PASSWORD_PROPERTY, password);
127
128    return getConnection(props);
129  }
130
131  /**
132   * Sets the log writer for this data source.
133   * 
134   * @param output print writer.
135   * @throws SQLException in case of an error occurs.
136   */
137  public void setLogWriter(PrintWriter output) throws SQLException
138  {
139    logWriter = output;
140  }
141
142  /**
143   * Gets the log writer.
144   * 
145   * @return log writer.
146   */
147  public java.io.PrintWriter getLogWriter()
148  {
149    return logWriter;
150  }
151
152  /**
153   * Sets the timeout. Actually does nothing.
154   * 
155   * @param seconds timeout in seconds.
156   * @throws SQLException in case of an error occurs.
157   */
158  public void setLoginTimeout(int seconds) throws SQLException
159  {
160  }
161
162  /**
163   * Gets the login timeout.
164   * 
165   * @return login timeout
166   * @throws SQLException in case of an error occurs.
167   */
168  public int getLoginTimeout() throws SQLException
169  {
170    return 0;
171  }
172
173  //---------------------------------
174  //--- Referenceable methods
175  //---------------------------------
176  /**
177   * Gets a reference to this. The factory used for this class is the
178   * {@link DataSourceFactory}class.
179   * 
180   * @return a reference to this.
181   * @throws NamingException if <code>DataSourceFactory</code> not found.
182   */
183  public Reference getReference() throws NamingException
184  {
185    Reference ref = new Reference(getClass().getName(), FACTORY_CLASSNAME, null);
186    ref.add(new StringRefAddr(DESCRIPTION_PROPERTY, getDescription()));
187    ref.add(new StringRefAddr(USER_PROPERTY, getUser()));
188    ref.add(new StringRefAddr(PASSWORD_PROPERTY, password));
189    ref.add(new StringRefAddr(URL_PROPERTY, getUrl()));
190    return ref;
191  }
192
193  //---------------------------------
194  //--- Properties methods
195  //---------------------------------
196
197  /**
198   * Return the description of this Datasource with the Driver version number.
199   * 
200   * @return Datasource description
201   */
202  public String getDescription()
203  {
204    return "C-JDBC " + driver.getMajorVersion() + "."
205        + driver.getMinorVersion() + " Datasource";
206  }
207
208  /**
209   * Sets url of the C-JDBC controller(s) to connect. The method is used by the
210   * "application server" to set the URL (potentially according a deployment
211   * descriptor). The url is stored in the {@link #URL_PROPERTY}property.
212   * 
213   * @param url URL to be used to connect C-JDBC controller(s)
214   */
215  public void setUrl(String url)
216  {
217    this.url = url;
218  }
219
220  /**
221   * Sets URL of the C-JDBC controller(s) to connect. The method is used by the
222   * "application server" to set the URL (potentially according a deployment
223   * descriptor). The URL is stored in the "url" property.
224   * 
225   * @param url URL to be used to connect C-JDBC controller(s).
226   */
227  public void setURL(String url)
228  {
229    setUrl(url);
230  }
231
232  /**
233   * Gets url of the C-JDBC controller(s) to connect. The URL is stored in the
234   * {@link #URL_PROPERTY}property.
235   * 
236   * @return URL to be used to connect C-JDBC controller(s).
237   */
238  public String getUrl()
239  {
240    return url;
241  }
242
243  /**
244   * Gets URL of the C-JDBC controller(s) to connect. The URL is stored in the
245   * {@link #URL_PROPERTY}property.
246   * 
247   * @return URL to be used to connect C-JDBC controller(s).
248   */
249  public String getURL()
250  {
251    return getUrl();
252  }
253
254  /**
255   * Sets user name to be used to connect the C-JDBC controller(s). The method
256   * can be used by the "application server" to set the user name (potentially
257   * according a deployment descriptor). The user name is stored in the
258   * {@link #USER_PROPERTY}property.
259   * 
260   * @param userName user name to be used to connect C-JDBC controller(s).
261   */
262  public void setUser(String userName)
263  {
264    user = userName;
265  }
266
267  /**
268   * Gets user name to be used to connect the C-JDBC controller(s). The user
269   * name is stored in the {@link #USER_PROPERTY}property.
270   * 
271   * @return user name to be used to connect C-JDBC controller(s).
272   */
273  public String getUser()
274  {
275    return user;
276  }
277
278  /**
279   * Sets password to be used to connect the C-JDBC controller(s). The method
280   * can be used by the "application server" to set the password (potentially
281   * according a deployment descriptor). The password is stored in the
282   * {@link #PASSWORD_PROPERTY}property. Note that there is not a <code>getPassword</code>
283   * method.
284   * 
285   * @param pwd password to be used to connect C-JDBC controller(s).
286   */
287  public void setPassword(String pwd)
288  {
289    password = pwd;
290  }
291
292  //---------------------------------
293  //--- Protected methods
294  //---------------------------------
295  /**
296   * Creates a connection using the specified properties.
297   * 
298   * @param props connection properties.
299   * @throws SQLException if an error occurs.
300   * @return a new connection.
301   */
302  protected java.sql.Connection getConnection(Properties props)
303      throws SQLException
304  {
305    return driver.connect(url, props);
306  }
307
308}