KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > driver > DataSource


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
25 package org.objectweb.cjdbc.driver;
26
27 import java.io.PrintWriter JavaDoc;
28 import java.io.Serializable JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 import javax.naming.NamingException JavaDoc;
33 import javax.naming.Reference JavaDoc;
34 import javax.naming.Referenceable JavaDoc;
35 import javax.naming.StringRefAddr JavaDoc;
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  */

47 public class DataSource
48     implements
49       javax.sql.DataSource JavaDoc,
50       Referenceable JavaDoc,
51       Serializable JavaDoc
52 {
53
54   /** DataSource properties */
55   protected static final String JavaDoc URL_PROPERTY = "url";
56   protected static final String JavaDoc USER_PROPERTY = Driver.USER_PROPERTY;
57   protected static final String JavaDoc PASSWORD_PROPERTY = Driver.PASSWORD_PROPERTY;
58   protected static final String JavaDoc DRIVER_CLASSNAME = "org.objectweb.cjdbc.driver.Driver";
59   protected static final String JavaDoc FACTORY_CLASSNAME = "org.objectweb.cjdbc.driver.DataSourceFactory";
60   protected static final String JavaDoc 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 JavaDoc e)
71     {
72       throw new RuntimeException JavaDoc("Can't load " + DRIVER_CLASSNAME);
73     }
74   }
75
76   /** DataSource properties */
77   protected String JavaDoc url = null;
78   protected String JavaDoc user = null;
79   protected String JavaDoc password = null;
80   protected PrintWriter JavaDoc 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 JavaDoc getConnection() throws SQLException JavaDoc
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 JavaDoc getConnection(String JavaDoc user, String JavaDoc password)
114       throws SQLException JavaDoc
115   {
116     if (user == null)
117     {
118       user = "";
119     }
120     if (password == null)
121     {
122       password = "";
123     }
124     Properties JavaDoc props = new Properties JavaDoc();
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 JavaDoc output) throws SQLException JavaDoc
138   {
139     logWriter = output;
140   }
141
142   /**
143    * Gets the log writer.
144    *
145    * @return log writer.
146    */

147   public java.io.PrintWriter JavaDoc 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 JavaDoc
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 JavaDoc
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 JavaDoc getReference() throws NamingException JavaDoc
184   {
185     Reference JavaDoc ref = new Reference JavaDoc(getClass().getName(), FACTORY_CLASSNAME, null);
186     ref.add(new StringRefAddr JavaDoc(DESCRIPTION_PROPERTY, getDescription()));
187     ref.add(new StringRefAddr JavaDoc(USER_PROPERTY, getUser()));
188     ref.add(new StringRefAddr JavaDoc(PASSWORD_PROPERTY, password));
189     ref.add(new StringRefAddr JavaDoc(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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getConnection(Properties JavaDoc props)
303       throws SQLException JavaDoc
304   {
305     return driver.connect(url, props);
306   }
307
308 }
309
Popular Tags