KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > driver > DataSource


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Marek Prochazka.
20  * Contributor(s):
21  */

22
23 package org.continuent.sequoia.driver;
24
25 import java.io.PrintWriter JavaDoc;
26 import java.io.Serializable JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 import javax.naming.NamingException JavaDoc;
31 import javax.naming.Reference JavaDoc;
32 import javax.naming.Referenceable JavaDoc;
33 import javax.naming.StringRefAddr JavaDoc;
34
35 /**
36  * An implementation of the JDBC 2.0 optional package <code>DataSource</code>
37  * interface. It allows to set the URL, user name, and password to its
38  * properties. It can be bound via JNDI so that the properties can be set by an
39  * "application server" and a "ready-to-use" reference to
40  * <code>DataSource</code> can be retrieved via JNDI.
41  *
42  * @author <a HREF="mailto:Marek.Prochazka@inrialpes.fr">Marek Prochazka </a>
43  * @version 1.0
44  */

45 public class DataSource
46     implements
47       javax.sql.DataSource JavaDoc,
48       Referenceable JavaDoc,
49       Serializable JavaDoc
50 {
51   private static final long serialVersionUID = 7103360001817804513L;
52   
53   /** DataSource properties */
54   protected static final String JavaDoc URL_PROPERTY = "url";
55   protected static final String JavaDoc USER_PROPERTY = Driver.USER_PROPERTY;
56   protected static final String JavaDoc PASSWORD_PROPERTY = Driver.PASSWORD_PROPERTY;
57   protected static final String JavaDoc DRIVER_CLASSNAME = "org.continuent.sequoia.driver.Driver";
58   protected static final String JavaDoc FACTORY_CLASSNAME = "org.continuent.sequoia.driver.DataSourceFactory";
59   protected static final String JavaDoc DESCRIPTION_PROPERTY = "description";
60
61   /** Wrapped driver for to get connections. */
62   protected static Driver driver = null;
63   static
64   {
65     try
66     {
67       driver = (Driver) Class.forName(DRIVER_CLASSNAME).newInstance();
68     }
69     catch (Exception JavaDoc e)
70     {
71       throw new RuntimeException JavaDoc("Can't load " + DRIVER_CLASSNAME);
72     }
73   }
74
75   /** DataSource properties */
76   protected String JavaDoc url = null;
77   protected String JavaDoc user = null;
78   protected String JavaDoc password = null;
79   protected PrintWriter JavaDoc logWriter = null;
80
81   /**
82    * Default constructor.
83    */

84   public DataSource()
85   {
86   }
87
88   // ---------------------------------
89
// --- DataSource methods
90
// ---------------------------------
91
/**
92    * Gets connection. Retrieves a new connection using the user name and
93    * password that have been already set.
94    *
95    * @throws SQLException if an error occurs.
96    * @return a new connection.
97    */

98   public java.sql.Connection JavaDoc getConnection() throws SQLException JavaDoc
99   {
100     return getConnection(user, password);
101   }
102
103   /**
104    * Gets connection. Retrieves a new connection using the user name and
105    * password specified.
106    *
107    * @param user user name.
108    * @param password password.
109    * @return a new connection.
110    * @throws SQLException if an error occurs.
111    */

112   public java.sql.Connection JavaDoc getConnection(String JavaDoc user, String JavaDoc password)
113       throws SQLException JavaDoc
114   {
115     if (user == null)
116     {
117       user = "";
118     }
119     if (password == null)
120     {
121       password = "";
122     }
123     Properties JavaDoc props = new Properties JavaDoc();
124     props.put(USER_PROPERTY, user);
125     props.put(PASSWORD_PROPERTY, password);
126
127     return getConnection(props);
128   }
129
130   /**
131    * Sets the log writer for this data source.
132    *
133    * @param output print writer.
134    * @throws SQLException in case of an error occurs.
135    */

136   public void setLogWriter(PrintWriter JavaDoc output) throws SQLException JavaDoc
137   {
138     logWriter = output;
139   }
140
141   /**
142    * Gets the log writer.
143    *
144    * @return log writer.
145    */

146   public java.io.PrintWriter JavaDoc getLogWriter()
147   {
148     return logWriter;
149   }
150
151   /**
152    * Sets the timeout. Actually does nothing.
153    *
154    * @param seconds timeout in seconds.
155    * @throws SQLException in case of an error occurs.
156    */

157   public void setLoginTimeout(int seconds) throws SQLException JavaDoc
158   {
159   }
160
161   /**
162    * Gets the login timeout.
163    *
164    * @return login timeout
165    * @throws SQLException in case of an error occurs.
166    */

167   public int getLoginTimeout() throws SQLException JavaDoc
168   {
169     return 0;
170   }
171
172   // ---------------------------------
173
// --- Referenceable methods
174
// ---------------------------------
175
/**
176    * Gets a reference to this. The factory used for this class is the
177    * {@link DataSourceFactory}class.
178    *
179    * @return a reference to this.
180    * @throws NamingException if <code>DataSourceFactory</code> not found.
181    */

182   public Reference JavaDoc getReference() throws NamingException JavaDoc
183   {
184     Reference JavaDoc ref = new Reference JavaDoc(getClass().getName(), FACTORY_CLASSNAME, null);
185     ref.add(new StringRefAddr JavaDoc(DESCRIPTION_PROPERTY, getDescription()));
186     ref.add(new StringRefAddr JavaDoc(USER_PROPERTY, getUser()));
187     ref.add(new StringRefAddr JavaDoc(PASSWORD_PROPERTY, password));
188     ref.add(new StringRefAddr JavaDoc(URL_PROPERTY, getUrl()));
189     return ref;
190   }
191
192   // ---------------------------------
193
// --- Properties methods
194
// ---------------------------------
195

196   /**
197    * Return the description of this Datasource with the Driver version number.
198    *
199    * @return Datasource description
200    */

201   public String JavaDoc getDescription()
202   {
203     return "Sequoia " + driver.getMajorVersion() + "."
204         + driver.getMinorVersion() + " Datasource";
205   }
206
207   /**
208    * Sets url of the Sequoia controller(s) to connect. The method is used by the
209    * "application server" to set the URL (potentially according a deployment
210    * descriptor). The url is stored in the {@link #URL_PROPERTY}property.
211    *
212    * @param url URL to be used to connect Sequoia controller(s)
213    */

214   public void setUrl(String JavaDoc url)
215   {
216     this.url = url;
217   }
218
219   /**
220    * Sets URL of the Sequoia controller(s) to connect. The method is used by the
221    * "application server" to set the URL (potentially according a deployment
222    * descriptor). The URL is stored in the "url" property.
223    *
224    * @param url URL to be used to connect Sequoia controller(s).
225    */

226   public void setURL(String JavaDoc url)
227   {
228     setUrl(url);
229   }
230
231   /**
232    * Gets url of the Sequoia controller(s) to connect. The URL is stored in the
233    * {@link #URL_PROPERTY}property.
234    *
235    * @return URL to be used to connect Sequoia controller(s).
236    */

237   public String JavaDoc getUrl()
238   {
239     return url;
240   }
241
242   /**
243    * Gets URL of the Sequoia controller(s) to connect. The URL is stored in the
244    * {@link #URL_PROPERTY}property.
245    *
246    * @return URL to be used to connect Sequoia controller(s).
247    */

248   public String JavaDoc getURL()
249   {
250     return getUrl();
251   }
252
253   /**
254    * Sets user name to be used to connect the Sequoia controller(s). The method
255    * can be used by the "application server" to set the user name (potentially
256    * according a deployment descriptor). The user name is stored in the
257    * {@link #USER_PROPERTY}property.
258    *
259    * @param userName user name to be used to connect Sequoia controller(s).
260    */

261   public void setUser(String JavaDoc userName)
262   {
263     user = userName;
264   }
265
266   /**
267    * Gets user name to be used to connect the Sequoia controller(s). The user
268    * name is stored in the {@link #USER_PROPERTY}property.
269    *
270    * @return user name to be used to connect Sequoia controller(s).
271    */

272   public String JavaDoc getUser()
273   {
274     return user;
275   }
276
277   /**
278    * Sets password to be used to connect the Sequoia controller(s). The method
279    * can be used by the "application server" to set the password (potentially
280    * according a deployment descriptor). The password is stored in the
281    * {@link #PASSWORD_PROPERTY}property. Note that there is not a
282    * <code>getPassword</code> method.
283    *
284    * @param pwd password to be used to connect Sequoia controller(s).
285    */

286   public void setPassword(String JavaDoc pwd)
287   {
288     password = pwd;
289   }
290
291   // ---------------------------------
292
// --- Protected methods
293
// ---------------------------------
294
/**
295    * Creates a connection using the specified properties.
296    *
297    * @param props connection properties.
298    * @throws SQLException if an error occurs.
299    * @return a new connection.
300    */

301   protected java.sql.Connection JavaDoc getConnection(Properties JavaDoc props)
302       throws SQLException JavaDoc
303   {
304     return driver.connect(url, props);
305   }
306
307 }
308
Popular Tags