001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Marek Prochazka.
020: * Contributor(s):
021: */package org.continuent.sequoia.driver;
022:
023: import java.io.PrintWriter;
024: import java.io.Serializable;
025: import java.sql.SQLException;
026: import java.util.Properties;
027:
028: import javax.naming.NamingException;
029: import javax.naming.Reference;
030: import javax.naming.Referenceable;
031: import javax.naming.StringRefAddr;
032:
033: /**
034: * An implementation of the JDBC 2.0 optional package <code>DataSource</code>
035: * interface. It allows to set the URL, user name, and password to its
036: * properties. It can be bound via JNDI so that the properties can be set by an
037: * "application server" and a "ready-to-use" reference to
038: * <code>DataSource</code> can be retrieved via JNDI.
039: *
040: * @author <a href="mailto:Marek.Prochazka@inrialpes.fr">Marek Prochazka </a>
041: * @version 1.0
042: */
043: public class DataSource implements javax.sql.DataSource, Referenceable,
044: Serializable {
045: private static final long serialVersionUID = 7103360001817804513L;
046:
047: /** DataSource properties */
048: protected static final String URL_PROPERTY = "url";
049: protected static final String USER_PROPERTY = Driver.USER_PROPERTY;
050: protected static final String PASSWORD_PROPERTY = Driver.PASSWORD_PROPERTY;
051: protected static final String DRIVER_CLASSNAME = "org.continuent.sequoia.driver.Driver";
052: protected static final String FACTORY_CLASSNAME = "org.continuent.sequoia.driver.DataSourceFactory";
053: protected static final String DESCRIPTION_PROPERTY = "description";
054:
055: /** Wrapped driver for to get connections. */
056: protected static Driver driver = null;
057: static {
058: try {
059: driver = (Driver) Class.forName(DRIVER_CLASSNAME)
060: .newInstance();
061: } catch (Exception e) {
062: throw new RuntimeException("Can't load " + DRIVER_CLASSNAME);
063: }
064: }
065:
066: /** DataSource properties */
067: protected String url = null;
068: protected String user = null;
069: protected String password = null;
070: protected PrintWriter logWriter = null;
071:
072: /**
073: * Default constructor.
074: */
075: public DataSource() {
076: }
077:
078: // ---------------------------------
079: // --- DataSource methods
080: // ---------------------------------
081: /**
082: * Gets connection. Retrieves a new connection using the user name and
083: * password that have been already set.
084: *
085: * @throws SQLException if an error occurs.
086: * @return a new connection.
087: */
088: public java.sql.Connection getConnection() throws SQLException {
089: return getConnection(user, password);
090: }
091:
092: /**
093: * Gets connection. Retrieves a new connection using the user name and
094: * password specified.
095: *
096: * @param user user name.
097: * @param password password.
098: * @return a new connection.
099: * @throws SQLException if an error occurs.
100: */
101: public java.sql.Connection getConnection(String user,
102: String password) throws SQLException {
103: if (user == null) {
104: user = "";
105: }
106: if (password == null) {
107: password = "";
108: }
109: Properties props = new Properties();
110: props.put(USER_PROPERTY, user);
111: props.put(PASSWORD_PROPERTY, password);
112:
113: return getConnection(props);
114: }
115:
116: /**
117: * Sets the log writer for this data source.
118: *
119: * @param output print writer.
120: * @throws SQLException in case of an error occurs.
121: */
122: public void setLogWriter(PrintWriter output) throws SQLException {
123: logWriter = output;
124: }
125:
126: /**
127: * Gets the log writer.
128: *
129: * @return log writer.
130: */
131: public java.io.PrintWriter getLogWriter() {
132: return logWriter;
133: }
134:
135: /**
136: * Sets the timeout. Actually does nothing.
137: *
138: * @param seconds timeout in seconds.
139: * @throws SQLException in case of an error occurs.
140: */
141: public void setLoginTimeout(int seconds) throws SQLException {
142: }
143:
144: /**
145: * Gets the login timeout.
146: *
147: * @return login timeout
148: * @throws SQLException in case of an error occurs.
149: */
150: public int getLoginTimeout() throws SQLException {
151: return 0;
152: }
153:
154: // ---------------------------------
155: // --- Referenceable methods
156: // ---------------------------------
157: /**
158: * Gets a reference to this. The factory used for this class is the
159: * {@link DataSourceFactory}class.
160: *
161: * @return a reference to this.
162: * @throws NamingException if <code>DataSourceFactory</code> not found.
163: */
164: public Reference getReference() throws NamingException {
165: Reference ref = new Reference(getClass().getName(),
166: FACTORY_CLASSNAME, null);
167: ref.add(new StringRefAddr(DESCRIPTION_PROPERTY,
168: getDescription()));
169: ref.add(new StringRefAddr(USER_PROPERTY, getUser()));
170: ref.add(new StringRefAddr(PASSWORD_PROPERTY, password));
171: ref.add(new StringRefAddr(URL_PROPERTY, getUrl()));
172: return ref;
173: }
174:
175: // ---------------------------------
176: // --- Properties methods
177: // ---------------------------------
178:
179: /**
180: * Return the description of this Datasource with the Driver version number.
181: *
182: * @return Datasource description
183: */
184: public String getDescription() {
185: return "Sequoia " + driver.getMajorVersion() + "."
186: + driver.getMinorVersion() + " Datasource";
187: }
188:
189: /**
190: * Sets url of the Sequoia controller(s) to connect. The method is used by the
191: * "application server" to set the URL (potentially according a deployment
192: * descriptor). The url is stored in the {@link #URL_PROPERTY}property.
193: *
194: * @param url URL to be used to connect Sequoia controller(s)
195: */
196: public void setUrl(String url) {
197: this .url = url;
198: }
199:
200: /**
201: * Sets URL of the Sequoia controller(s) to connect. The method is used by the
202: * "application server" to set the URL (potentially according a deployment
203: * descriptor). The URL is stored in the "url" property.
204: *
205: * @param url URL to be used to connect Sequoia controller(s).
206: */
207: public void setURL(String url) {
208: setUrl(url);
209: }
210:
211: /**
212: * Gets url of the Sequoia controller(s) to connect. The URL is stored in the
213: * {@link #URL_PROPERTY}property.
214: *
215: * @return URL to be used to connect Sequoia controller(s).
216: */
217: public String getUrl() {
218: return url;
219: }
220:
221: /**
222: * Gets URL of the Sequoia controller(s) to connect. The URL is stored in the
223: * {@link #URL_PROPERTY}property.
224: *
225: * @return URL to be used to connect Sequoia controller(s).
226: */
227: public String getURL() {
228: return getUrl();
229: }
230:
231: /**
232: * Sets user name to be used to connect the Sequoia controller(s). The method
233: * can be used by the "application server" to set the user name (potentially
234: * according a deployment descriptor). The user name is stored in the
235: * {@link #USER_PROPERTY}property.
236: *
237: * @param userName user name to be used to connect Sequoia controller(s).
238: */
239: public void setUser(String userName) {
240: user = userName;
241: }
242:
243: /**
244: * Gets user name to be used to connect the Sequoia controller(s). The user
245: * name is stored in the {@link #USER_PROPERTY}property.
246: *
247: * @return user name to be used to connect Sequoia controller(s).
248: */
249: public String getUser() {
250: return user;
251: }
252:
253: /**
254: * Sets password to be used to connect the Sequoia controller(s). The method
255: * can be used by the "application server" to set the password (potentially
256: * according a deployment descriptor). The password is stored in the
257: * {@link #PASSWORD_PROPERTY}property. Note that there is not a
258: * <code>getPassword</code> method.
259: *
260: * @param pwd password to be used to connect Sequoia controller(s).
261: */
262: public void setPassword(String pwd) {
263: password = pwd;
264: }
265:
266: // ---------------------------------
267: // --- Protected methods
268: // ---------------------------------
269: /**
270: * Creates a connection using the specified properties.
271: *
272: * @param props connection properties.
273: * @throws SQLException if an error occurs.
274: * @return a new connection.
275: */
276: protected java.sql.Connection getConnection(Properties props)
277: throws SQLException {
278: return driver.connect(url, props);
279: }
280:
281: }
|