View Javadoc

1   /*
2    ~ Copyright 2006-2007 Nicolas De Loof.
3    ~
4    ~ Licensed under the Apache License, Version 2.0 (the "License");
5    ~ you may not use this file except in compliance with the License.
6    ~ You may obtain a copy of the License at
7    ~
8    ~      http://www.apache.org/licenses/LICENSE-2.0
9    ~
10   ~ Unless required by applicable law or agreed to in writing, software
11   ~ distributed under the License is distributed on an "AS IS" BASIS,
12   ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   ~ See the License for the specific language governing permissions and
14   ~ limitations under the License.
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   * DataSource wrapper that adds monitoring on JDBC connexions.
30   * 
31   * @author <a href="mailto:ndeloof@sourceforge.net">Nicolas De Loof</a>
32   */
33  public class MonitoredDataSource
34      implements DataSource
35  {
36      /** delegate DataSource */
37      private DataSource dataSource;
38  
39      /** dataSource name */
40      private String dataSourceName = DataSource.class.getName();
41  
42      /** Monitor for JDBC operation on this datasource */
43      private JdbcMonitor monitor;
44  
45      /**
46       * Constructor
47       * 
48       * @param dataSource the datasource to monitor
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       * @param dataSource the dataSource to set
66       */
67      public void setDataSource( DataSource dataSource )
68      {
69          this.dataSource = dataSource;
70      }
71  
72      /**
73       * @param dataSourceName the dataSourceName to set
74       */
75      public void setDataSourceName( String dataSourceName )
76      {
77          this.dataSourceName = dataSourceName;
78      }
79  
80      /**
81       * @param monitor the monitor to set
82       */
83      public void setMonitor( Monitor monitor )
84      {
85          this.monitor = new JdbcMonitor( monitor );
86      }
87  
88      /**
89       * Init
90       * 
91       * @return DataSource ready for use
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      * {@inheritDoc}
104      * 
105      * @see javax.sql.DataSource#getConnection()
106      */
107     public Connection getConnection()
108         throws SQLException
109     {
110         Connection connection = dataSource.getConnection();
111         return new MonitoredConnection( connection, monitor );
112     }
113 
114     /**
115      * {@inheritDoc}
116      * 
117      * @see javax.sql.DataSource#getConnection(java.lang.String,
118      * java.lang.String)
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      * {@inheritDoc}
129      * 
130      * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
131      * java.lang.reflect.Method, java.lang.Object[])
132      */
133     public Object invoke( Object proxy, Method method, Object[] args )
134         throws Throwable
135     {
136 
137         return null;
138     }
139 
140     /**
141      * {@inheritDoc}
142      * 
143      * @see javax.sql.DataSource#getLoginTimeout()
144      */
145     public int getLoginTimeout()
146         throws SQLException
147     {
148         return dataSource.getLoginTimeout();
149     }
150 
151     /**
152      * {@inheritDoc}
153      * 
154      * @see javax.sql.DataSource#getLogWriter()
155      */
156     public PrintWriter getLogWriter()
157         throws SQLException
158     {
159         return dataSource.getLogWriter();
160     }
161 
162     /**
163      * {@inheritDoc}
164      * 
165      * @see javax.sql.DataSource#setLoginTimeout(int)
166      */
167     public void setLoginTimeout( int seconds )
168         throws SQLException
169     {
170         dataSource.setLoginTimeout( seconds );
171     }
172 
173     /**
174      * {@inheritDoc}
175      * 
176      * @see javax.sql.DataSource#setLogWriter(java.io.PrintWriter)
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 }