1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jmonit.support.jdbc;
17
18 import java.io.PrintWriter;
19 import java.lang.reflect.Method;
20 import java.sql.Connection;
21 import java.sql.SQLException;
22
23 import javax.sql.DataSource;
24
25 import org.jmonit.Monitor;
26 import org.jmonit.Monitoring;
27
28
29
30
31
32
33 public class MonitoredDataSource
34 implements DataSource
35 {
36
37 private DataSource dataSource;
38
39
40 private String dataSourceName = DataSource.class.getName();
41
42
43 private JdbcMonitor monitor;
44
45
46
47
48
49
50 public MonitoredDataSource( DataSource dataSource )
51 {
52 super();
53 this.dataSource = dataSource;
54 }
55
56
57
58
59 public MonitoredDataSource()
60 {
61 super();
62 }
63
64
65
66
67 public void setDataSource( DataSource dataSource )
68 {
69 this.dataSource = dataSource;
70 }
71
72
73
74
75 public void setDataSourceName( String dataSourceName )
76 {
77 this.dataSourceName = dataSourceName;
78 }
79
80
81
82
83 public void setMonitor( Monitor monitor )
84 {
85 this.monitor = new JdbcMonitor( monitor );
86 }
87
88
89
90
91
92
93 public DataSource init()
94 {
95 if ( monitor == null )
96 {
97 monitor = new JdbcMonitor( Monitoring.getMonitor( dataSourceName ) );
98 }
99 return this;
100 }
101
102
103
104
105
106
107 public Connection getConnection()
108 throws SQLException
109 {
110 Connection connection = dataSource.getConnection();
111 return new MonitoredConnection( connection, monitor );
112 }
113
114
115
116
117
118
119
120 public Connection getConnection( String username, String password )
121 throws SQLException
122 {
123 Connection connection = dataSource.getConnection( username, password );
124 return new MonitoredConnection( connection, monitor );
125 }
126
127
128
129
130
131
132
133 public Object invoke( Object proxy, Method method, Object[] args )
134 throws Throwable
135 {
136
137 return null;
138 }
139
140
141
142
143
144
145 public int getLoginTimeout()
146 throws SQLException
147 {
148 return dataSource.getLoginTimeout();
149 }
150
151
152
153
154
155
156 public PrintWriter getLogWriter()
157 throws SQLException
158 {
159 return dataSource.getLogWriter();
160 }
161
162
163
164
165
166
167 public void setLoginTimeout( int seconds )
168 throws SQLException
169 {
170 dataSource.setLoginTimeout( seconds );
171 }
172
173
174
175
176
177
178 public void setLogWriter( PrintWriter out )
179 throws SQLException
180 {
181 dataSource.setLogWriter( out );
182 }
183
184 public boolean isWrapperFor( Class<?> iface )
185 throws SQLException
186 {
187 return dataSource.isWrapperFor( iface );
188 }
189
190 public <T> T unwrap( Class<T> iface )
191 throws SQLException
192 {
193 return dataSource.unwrap( iface );
194 }
195
196 }