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 oracle.jdbc.pool.OracleConnectionCacheManager;
14  import oracle.jdbc.pool.OracleDataSource;
15  import psiprobe.UtilsBase;
16  import psiprobe.model.DataSourceInfo;
17  
18  import java.util.Properties;
19  
20  /**
21   * Accesses oracle.jdbc.pool.OracleDataSource.
22   *
23   * <p>
24   * Oracle connection pool is quite different from any other available for Tomcat. Datasources are
25   * run by static OracleConnectionCacheManager, whereby application context scope datasource would
26   * have a named entry in OracleConnectionCacheManager.
27   * </p>
28   * 
29   * <p>
30   * Datasources do not have information about pool as such, therefore this accessor has to do quite
31   * tedious job of verifying whether the datasource has a record in the cache manager or not. The
32   * pool information is subsequently retrieved from the relevant cache manager entry.
33   * </p>
34   */
35  public class OracleDatasourceAccessor implements DatasourceAccessor {
36  
37    @Override
38    public DataSourceInfo getInfo(Object resource) throws Exception {
39      DataSourceInfo dataSourceInfo = null;
40  
41      if (canMap(resource)) {
42        OracleDataSource source = (OracleDataSource) resource;
43        OracleConnectionCacheManager occm =
44            OracleConnectionCacheManager.getConnectionCacheManagerInstance();
45        Properties cacheProperties = source.getConnectionCacheProperties();
46        String cacheName = source.getConnectionCacheName();
47        cacheName = cacheName != null && occm.existsCache(cacheName) ? cacheName : null;
48  
49        if (cacheProperties != null) {
50  
51          dataSourceInfo = new DataSourceInfo();
52          if (cacheName != null) {
53            dataSourceInfo.setBusyConnections(occm.getNumberOfActiveConnections(cacheName));
54            dataSourceInfo.setEstablishedConnections(occm.getNumberOfAvailableConnections(cacheName)
55                + dataSourceInfo.getBusyConnections());
56          } else {
57            dataSourceInfo.setBusyConnections(0);
58            dataSourceInfo.setEstablishedConnections(0);
59          }
60  
61          dataSourceInfo.setMaxConnections(UtilsBase.toInt(cacheProperties.getProperty("MaxLimit"), -1));
62          dataSourceInfo.setJdbcUrl(source.getURL());
63          dataSourceInfo.setUsername(source.getUser());
64          dataSourceInfo.setResettable(true);
65          dataSourceInfo.setType("oracle-jdbc");
66        }
67      }
68      return dataSourceInfo;
69    }
70  
71    @Override
72    public boolean reset(Object resource) throws Exception {
73      if (canMap(resource)) {
74        ((OracleDataSource) resource).close();
75        return true;
76      }
77      return false;
78    }
79  
80    @Override
81    public boolean canMap(Object resource) {
82      return "oracle.jdbc.pool.OracleDataSource".equals(resource.getClass().getName())
83          && resource instanceof OracleDataSource;
84    }
85  }