View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.beans.accessors;
12  
13  import com.mchange.v2.c3p0.ComboPooledDataSource;
14  
15  import java.sql.SQLException;
16  
17  import psiprobe.model.DataSourceInfo;
18  
19  /**
20   * Abstraction layer for c3p0. Maps c3p0 datasource properties on our generic DataSourceInfo bean.
21   */
22  public class C3P0DatasourceAccessor implements DatasourceAccessor {
23  
24    @Override
25    public DataSourceInfo getInfo(Object resource) throws SQLException {
26      DataSourceInfo dataSourceInfo = null;
27      if (canMap(resource)) {
28        ComboPooledDataSource source = (ComboPooledDataSource) resource;
29  
30        dataSourceInfo = new DataSourceInfo();
31        dataSourceInfo.setBusyConnections(source.getNumBusyConnections());
32        dataSourceInfo.setEstablishedConnections(source.getNumConnections());
33        dataSourceInfo.setMaxConnections(source.getMaxPoolSize());
34        dataSourceInfo.setJdbcUrl(source.getJdbcUrl());
35        dataSourceInfo.setUsername(source.getUser());
36        dataSourceInfo.setResettable(true);
37        dataSourceInfo.setType("c3p0");
38      }
39      return dataSourceInfo;
40    }
41  
42    @Override
43    public boolean reset(Object resource) {
44      if (canMap(resource)) {
45        ((ComboPooledDataSource) resource).hardReset();
46        return true;
47      }
48      return false;
49    }
50  
51    @Override
52    public boolean canMap(Object resource) {
53      return "com.mchange.v2.c3p0.ComboPooledDataSource".equals(resource.getClass().getName())
54          && resource instanceof ComboPooledDataSource;
55    }
56  }