001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.monitoring.jdbc;
019
020
021 import org.apache.commons.monitoring.Role;
022 import org.apache.commons.monitoring.counters.Counter;
023 import org.apache.commons.monitoring.repositories.Repository;
024
025 import javax.sql.DataSource;
026 import java.io.PrintWriter;
027 import java.sql.Connection;
028 import java.sql.SQLException;
029 import java.sql.SQLFeatureNotSupportedException;
030 import java.util.logging.Logger;
031
032
033 /**
034 * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
035 */
036 public class MonitoredDataSource implements DataSource {
037 /**
038 * delegate DataSource
039 */
040 private DataSource dataSource;
041
042 /**
043 * dataSource name
044 */
045 private String dataSourceName = DataSource.class.getName();
046 private Counter counter;
047
048 /**
049 * Constructor
050 *
051 * @param dataSource the datasource to counter
052 */
053 public MonitoredDataSource(final DataSource dataSource) {
054 this.dataSource = dataSource;
055 this.counter = Repository.INSTANCE.getCounter(new Counter.Key(Role.JDBC, dataSourceName));
056 }
057
058 public MonitoredDataSource() {
059 super();
060 }
061
062 public void setDataSource(final DataSource dataSource) {
063 this.dataSource = dataSource;
064 }
065
066 /**
067 * @param dataSourceName the dataSourceName to set
068 */
069 public void setDataSourceName(final String dataSourceName) {
070 this.dataSourceName = dataSourceName;
071 }
072
073 /**
074 * @param counter the counter to set
075 */
076 public void setCounter(final Counter counter) {
077 this.counter = counter;
078 }
079
080 protected Connection monitor(final Connection connection) {
081 return MonitoredConnection.monitor(connection, counter);
082 }
083
084 /**
085 * @return the dataSource
086 */
087 protected DataSource getDataSource() {
088 return dataSource;
089 }
090
091 /**
092 * {@inheritDoc}
093 *
094 * @see javax.sql.DataSource#getConnection()
095 */
096 public Connection getConnection()
097 throws SQLException {
098 return monitor(getDataSource().getConnection());
099 }
100
101 /**
102 * {@inheritDoc}
103 *
104 * @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String)
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 // --- jdbc4 ----
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 }