001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.lib.jdbc;
020:
021: import java.io.PrintWriter;
022: import java.sql.Connection;
023: import java.sql.SQLException;
024: import javax.sql.DataSource;
025:
026: import org.apache.openjpa.lib.util.Closeable;
027:
028: /**
029: * Wrapper around an existing data source. Subclasses can override the
030: * methods whose behavior they mean to change. The <code>equals</code> and
031: * <code>hashCode</code> methods pass through to the base underlying data store.
032: *
033: * @author Abe White
034: */
035: public class DelegatingDataSource implements DataSource, Closeable {
036:
037: private final DataSource _ds;
038: private final DelegatingDataSource _del;
039:
040: /**
041: * Constructor. Supply wrapped data source.
042: */
043: public DelegatingDataSource(DataSource ds) {
044: _ds = ds;
045:
046: if (_ds instanceof DelegatingDataSource)
047: _del = (DelegatingDataSource) _ds;
048: else
049: _del = null;
050: }
051:
052: /**
053: * Return the wrapped data source.
054: */
055: public DataSource getDelegate() {
056: return _ds;
057: }
058:
059: /**
060: * Return the inner-most wrapped delegate.
061: */
062: public DataSource getInnermostDelegate() {
063: return (_del == null) ? _ds : _del.getInnermostDelegate();
064: }
065:
066: public int hashCode() {
067: return getInnermostDelegate().hashCode();
068: }
069:
070: public boolean equals(Object other) {
071: if (other == this )
072: return true;
073: if (other instanceof DelegatingDataSource)
074: other = ((DelegatingDataSource) other)
075: .getInnermostDelegate();
076: return getInnermostDelegate().equals(other);
077: }
078:
079: public String toString() {
080: StringBuffer buf = new StringBuffer("datasource ")
081: .append(hashCode());
082: appendInfo(buf);
083: return buf.toString();
084: }
085:
086: protected void appendInfo(StringBuffer buf) {
087: if (_del != null)
088: _del.appendInfo(buf);
089: }
090:
091: public PrintWriter getLogWriter() throws SQLException {
092: return _ds.getLogWriter();
093: }
094:
095: public void setLogWriter(PrintWriter out) throws SQLException {
096: _ds.setLogWriter(out);
097: }
098:
099: public int getLoginTimeout() throws SQLException {
100: return _ds.getLoginTimeout();
101: }
102:
103: public void setLoginTimeout(int timeout) throws SQLException {
104: _ds.setLoginTimeout(timeout);
105: }
106:
107: public Connection getConnection() throws SQLException {
108: return _ds.getConnection();
109: }
110:
111: public Connection getConnection(String user, String pass)
112: throws SQLException {
113: if (user == null && pass == null)
114: return _ds.getConnection();
115: return _ds.getConnection(user, pass);
116: }
117:
118: public void close() throws Exception {
119: if (_ds instanceof Closeable)
120: ((Closeable) _ds).close();
121: }
122: }
|