1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.monitoring.jdbc;
19
20
21 import org.apache.commons.monitoring.Role;
22 import org.apache.commons.monitoring.counters.Counter;
23 import org.apache.commons.monitoring.repositories.Repository;
24
25 import javax.sql.DataSource;
26 import java.io.PrintWriter;
27 import java.sql.Connection;
28 import java.sql.SQLException;
29 import java.sql.SQLFeatureNotSupportedException;
30 import java.util.logging.Logger;
31
32
33
34
35
36 public class MonitoredDataSource implements DataSource {
37
38
39
40 private DataSource dataSource;
41
42
43
44
45 private String dataSourceName = DataSource.class.getName();
46 private Counter counter;
47
48
49
50
51
52
53 public MonitoredDataSource(final DataSource dataSource) {
54 this.dataSource = dataSource;
55 this.counter = Repository.INSTANCE.getCounter(new Counter.Key(Role.JDBC, dataSourceName));
56 }
57
58 public MonitoredDataSource() {
59 super();
60 }
61
62 public void setDataSource(final DataSource dataSource) {
63 this.dataSource = dataSource;
64 }
65
66
67
68
69 public void setDataSourceName(final String dataSourceName) {
70 this.dataSourceName = dataSourceName;
71 }
72
73
74
75
76 public void setCounter(final Counter counter) {
77 this.counter = counter;
78 }
79
80 protected Connection monitor(final Connection connection) {
81 return MonitoredConnection.monitor(connection, counter);
82 }
83
84
85
86
87 protected DataSource getDataSource() {
88 return dataSource;
89 }
90
91
92
93
94
95
96 public Connection getConnection()
97 throws SQLException {
98 return monitor(getDataSource().getConnection());
99 }
100
101
102
103
104
105
106 public Connection getConnection(String username, String password)
107 throws SQLException {
108 return monitor(getDataSource().getConnection(username, password));
109 }
110
111 public int getLoginTimeout()
112 throws SQLException {
113 return getDataSource().getLoginTimeout();
114 }
115
116 public PrintWriter getLogWriter()
117 throws SQLException {
118 return getDataSource().getLogWriter();
119 }
120
121 public void setLoginTimeout(int seconds)
122 throws SQLException {
123 getDataSource().setLoginTimeout(seconds);
124 }
125
126 public void setLogWriter(PrintWriter out)
127 throws SQLException {
128 getDataSource().setLogWriter(out);
129 }
130
131
132
133 public boolean isWrapperFor(Class<?> iface)
134 throws SQLException {
135 return getDataSource().isWrapperFor(iface);
136 }
137
138 public <T> T unwrap(Class<T> iface)
139 throws SQLException {
140 return getDataSource().unwrap(iface);
141 }
142
143 public Logger getParentLogger()
144 throws SQLFeatureNotSupportedException {
145 return Logger.getLogger("commons-monitoring.datasource");
146 }
147 }