KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > services > DataSource


1 package com.quadcap.services;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.PrintWriter JavaDoc;
42
43 import java.util.Properties JavaDoc;
44
45 import java.sql.Driver JavaDoc;
46 import java.sql.DriverManager JavaDoc;
47 import java.sql.SQLException JavaDoc;
48
49 import javax.naming.Reference JavaDoc;
50 import javax.naming.Referenceable JavaDoc;
51 import javax.naming.StringRefAddr JavaDoc;
52
53 /**
54  * This class implements the <code>javax.sql.DataSource</code> interface,
55  * which provides a mechanism for obtaining <code>Connection</code>
56  * objects.
57  *
58  * @author Stan Bailes
59  */

60 public class DataSource implements javax.sql.DataSource JavaDoc, Referenceable JavaDoc {
61     String JavaDoc name;
62     String JavaDoc driverClass = null;
63     PrintWriter JavaDoc logWriter = null;
64     int loginTimeout = 0;
65     String JavaDoc url;
66     Properties JavaDoc props = new Properties JavaDoc();
67     Driver JavaDoc driver = null;
68
69     /**
70      * Default conctructor
71      */

72     public DataSource() {
73     }
74
75     public void setName(String JavaDoc name) {
76         this.name = name;
77     }
78
79     public String JavaDoc getName() {
80         return name;
81     }
82
83     public void setDriverClass(String JavaDoc s) {
84         this.driverClass = s;
85         try {
86             Class.forName(driverClass);
87         } catch (Throwable JavaDoc t) {
88         }
89     }
90
91     public String JavaDoc getDriverClass() {
92         return driverClass;
93     }
94
95     /**
96      * Return the URL to which this data source is bound.
97      */

98     public String JavaDoc getUrl() { return url; }
99
100     /**
101      * Setter method for specifing this data source's URL.
102      */

103     public void setUrl(String JavaDoc url) {
104         this.url = url;
105     }
106
107     /**
108      * Add a connection property for this data source.
109      *
110      * @param name the connection property's name
111      * @param val the connection property's value
112      */

113     public void addConnectionProperty(String JavaDoc name, String JavaDoc val) {
114         props.setProperty(name, val);
115     }
116
117     /**
118      * Get a new Connection bound to this data source.
119      *
120      * @return a new Connection
121      * @exception SQLException may be thrown
122      */

123     public java.sql.Connection JavaDoc getConnection()
124         throws SQLException JavaDoc
125     {
126         return getDriver().connect(url, props);
127     }
128
129     /**
130      * Get a new Connection bound to this data source.
131      *
132      * @param username the database user for whom the connection is being
133      * made.
134      * @param password the user's password.
135      *
136      * @return a new Connection
137      * @exception SQLException may be thrown
138      */

139     public java.sql.Connection JavaDoc getConnection(String JavaDoc username, String JavaDoc password)
140         throws SQLException JavaDoc
141     {
142         Properties JavaDoc p = new Properties JavaDoc(props);
143         p.setProperty("user", username);
144         p.setProperty("password", password);
145         return getDriver().connect(url, p);
146     }
147
148     /**
149      * Get the log writer for messages associated with this data source
150      */

151     public PrintWriter JavaDoc getLogWriter() {
152         return logWriter;
153     }
154
155     /**
156      * Set the log writer for debugging messages associated with this
157      * data source.
158      *
159      * <p>Note: this writer isn't used for anything in this release
160      * of QED.
161      *
162      * @param w the writer
163      */

164     public void setLogWriter(PrintWriter JavaDoc w) {
165         this.logWriter = w;
166     }
167
168     /**
169      * Get the login timeout.
170      *
171      * <p>Not implemented in this QED release.
172      */

173     public int getLoginTimeout() {
174         return loginTimeout;
175     }
176
177     /**
178      * Set the login timeout.
179      *
180      * <p>Not implemented in this QED release.
181      *
182      * @param timeout the timeout in seconds
183      */

184     public void setLoginTimeout(int timeout) {
185         this.loginTimeout = timeout;
186     }
187
188     public Reference JavaDoc getReference() {
189         Reference JavaDoc ref = new Reference JavaDoc(DataSource.class.getName(),
190                                       "com.quadcap.services.DataSources",
191                                       null);
192         ref.add(new StringRefAddr JavaDoc("name", name));
193         return ref;
194     }
195
196     final private Driver JavaDoc getDriver() throws SQLException JavaDoc {
197         if (driver == null) {
198             driver = DriverManager.getDriver(url);
199         }
200         return driver;
201     }
202 }
203
204
Popular Tags