001: package com.quadcap.services;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.PrintWriter;
042:
043: import java.util.Properties;
044:
045: import java.sql.Driver;
046: import java.sql.DriverManager;
047: import java.sql.SQLException;
048:
049: import javax.naming.Reference;
050: import javax.naming.Referenceable;
051: import javax.naming.StringRefAddr;
052:
053: /**
054: * This class implements the <code>javax.sql.DataSource</code> interface,
055: * which provides a mechanism for obtaining <code>Connection</code>
056: * objects.
057: *
058: * @author Stan Bailes
059: */
060: public class DataSource implements javax.sql.DataSource, Referenceable {
061: String name;
062: String driverClass = null;
063: PrintWriter logWriter = null;
064: int loginTimeout = 0;
065: String url;
066: Properties props = new Properties();
067: Driver driver = null;
068:
069: /**
070: * Default conctructor
071: */
072: public DataSource() {
073: }
074:
075: public void setName(String name) {
076: this .name = name;
077: }
078:
079: public String getName() {
080: return name;
081: }
082:
083: public void setDriverClass(String s) {
084: this .driverClass = s;
085: try {
086: Class.forName(driverClass);
087: } catch (Throwable t) {
088: }
089: }
090:
091: public String getDriverClass() {
092: return driverClass;
093: }
094:
095: /**
096: * Return the URL to which this data source is bound.
097: */
098: public String getUrl() {
099: return url;
100: }
101:
102: /**
103: * Setter method for specifing this data source's URL.
104: */
105: public void setUrl(String url) {
106: this .url = url;
107: }
108:
109: /**
110: * Add a connection property for this data source.
111: *
112: * @param name the connection property's name
113: * @param val the connection property's value
114: */
115: public void addConnectionProperty(String name, String val) {
116: props.setProperty(name, val);
117: }
118:
119: /**
120: * Get a new Connection bound to this data source.
121: *
122: * @return a new Connection
123: * @exception SQLException may be thrown
124: */
125: public java.sql.Connection getConnection() throws SQLException {
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 getConnection(String username,
140: String password) throws SQLException {
141: Properties p = new Properties(props);
142: p.setProperty("user", username);
143: p.setProperty("password", password);
144: return getDriver().connect(url, p);
145: }
146:
147: /**
148: * Get the log writer for messages associated with this data source
149: */
150: public PrintWriter getLogWriter() {
151: return logWriter;
152: }
153:
154: /**
155: * Set the log writer for debugging messages associated with this
156: * data source.
157: *
158: * <p>Note: this writer isn't used for anything in this release
159: * of QED.
160: *
161: * @param w the writer
162: */
163: public void setLogWriter(PrintWriter w) {
164: this .logWriter = w;
165: }
166:
167: /**
168: * Get the login timeout.
169: *
170: * <p>Not implemented in this QED release.
171: */
172: public int getLoginTimeout() {
173: return loginTimeout;
174: }
175:
176: /**
177: * Set the login timeout.
178: *
179: * <p>Not implemented in this QED release.
180: *
181: * @param timeout the timeout in seconds
182: */
183: public void setLoginTimeout(int timeout) {
184: this .loginTimeout = timeout;
185: }
186:
187: public Reference getReference() {
188: Reference ref = new Reference(DataSource.class.getName(),
189: "com.quadcap.services.DataSources", null);
190: ref.add(new StringRefAddr("name", name));
191: return ref;
192: }
193:
194: final private Driver getDriver() throws SQLException {
195: if (driver == null) {
196: driver = DriverManager.getDriver(url);
197: }
198: return driver;
199: }
200: }
|