1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.openjpa.lib.jdbc;
20
21 import java.io.PrintWriter;
22 import java.sql.Connection;
23 import java.sql.SQLException;
24 import javax.sql.DataSource;
25
26 import org.apache.openjpa.lib.util.Closeable;
27
28 /**
29 * Wrapper around an existing data source. Subclasses can override the
30 * methods whose behavior they mean to change. The <code>equals</code> and
31 * <code>hashCode</code> methods pass through to the base underlying data store.
32 *
33 * @author Abe White
34 */
35 public class DelegatingDataSource implements DataSource, Closeable {
36
37 private final DataSource _ds;
38 private final DelegatingDataSource _del;
39
40 /**
41 * Constructor. Supply wrapped data source.
42 */
43 public DelegatingDataSource(DataSource ds) {
44 _ds = ds;
45
46 if (_ds instanceof DelegatingDataSource)
47 _del = (DelegatingDataSource) _ds;
48 else
49 _del = null;
50 }
51
52 /**
53 * Return the wrapped data source.
54 */
55 public DataSource getDelegate() {
56 return _ds;
57 }
58
59 /**
60 * Return the inner-most wrapped delegate.
61 */
62 public DataSource getInnermostDelegate() {
63 return (_del == null) ? _ds : _del.getInnermostDelegate();
64 }
65
66 public int hashCode() {
67 return getInnermostDelegate().hashCode();
68 }
69
70 public boolean equals(Object other) {
71 if (other == this)
72 return true;
73 if (other instanceof DelegatingDataSource)
74 other = ((DelegatingDataSource) other).getInnermostDelegate();
75 return getInnermostDelegate().equals(other);
76 }
77
78 public String toString() {
79 StringBuffer buf = new StringBuffer("datasource "). append(hashCode());
80 appendInfo(buf);
81 return buf.toString();
82 }
83
84 protected void appendInfo(StringBuffer buf) {
85 if (_del != null)
86 _del.appendInfo(buf);
87 }
88
89 public PrintWriter getLogWriter() throws SQLException {
90 return _ds.getLogWriter();
91 }
92
93 public void setLogWriter(PrintWriter out) throws SQLException {
94 _ds.setLogWriter(out);
95 }
96
97 public int getLoginTimeout() throws SQLException {
98 return _ds.getLoginTimeout();
99 }
100
101 public void setLoginTimeout(int timeout) throws SQLException {
102 _ds.setLoginTimeout(timeout);
103 }
104
105 public Connection getConnection() throws SQLException {
106 return _ds.getConnection();
107 }
108
109 public Connection getConnection(String user, String pass)
110 throws SQLException {
111 if (user == null && pass == null)
112 return _ds.getConnection();
113 return _ds.getConnection(user, pass);
114 }
115
116 public void close() throws Exception {
117 if (_ds instanceof Closeable)
118 ((Closeable) _ds).close();
119 }
120 }