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 org.apache.tomcat.jdbc.pool.DataSource;
14  
15  import psiprobe.model.DataSourceInfo;
16  
17  /**
18   * Datasource accessor for tomcat.
19   */
20  public class TomcatJdbcPoolDatasourceAccessor implements DatasourceAccessor {
21  
22    @Override
23    public DataSourceInfo getInfo(Object resource) {
24      DataSourceInfo dataSourceInfo = null;
25      if (canMap(resource)) {
26        DataSource source = (DataSource) resource;
27        dataSourceInfo = new DataSourceInfo();
28        dataSourceInfo.setBusyConnections(source.getNumActive());
29        dataSourceInfo.setEstablishedConnections(source.getNumIdle() + source.getNumActive());
30        dataSourceInfo.setMaxConnections(source.getMaxActive());
31        dataSourceInfo.setJdbcUrl(source.getUrl());
32        dataSourceInfo.setUsername(source.getUsername());
33        dataSourceInfo.setResettable(false);
34        dataSourceInfo.setType("tomcat-jdbc");
35      }
36      return dataSourceInfo;
37    }
38  
39    @Override
40    public boolean reset(Object resource) {
41      return false;
42    }
43  
44    @Override
45    public boolean canMap(Object resource) {
46      return ("org.apache.tomcat.jdbc.pool.DataSource".equals(resource.getClass().getName())
47          || "org.apache.tomcat.jdbc.pool.XADataSource".equals(resource.getClass().getName()))
48          && resource instanceof DataSource;
49    }
50  
51  }