View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
35   */
36  public class MonitoredDataSource implements DataSource {
37      /**
38       * delegate DataSource
39       */
40      private DataSource dataSource;
41  
42      /**
43       * dataSource name
44       */
45      private String dataSourceName = DataSource.class.getName();
46      private Counter counter;
47  
48      /**
49       * Constructor
50       *
51       * @param dataSource the datasource to counter
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       * @param dataSourceName the dataSourceName to set
68       */
69      public void setDataSourceName(final String dataSourceName) {
70          this.dataSourceName = dataSourceName;
71      }
72  
73      /**
74       * @param counter the counter to set
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       * @return the dataSource
86       */
87      protected DataSource getDataSource() {
88          return dataSource;
89      }
90  
91      /**
92       * {@inheritDoc}
93       *
94       * @see javax.sql.DataSource#getConnection()
95       */
96      public Connection getConnection()
97          throws SQLException {
98          return monitor(getDataSource().getConnection());
99      }
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 }