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.openejb.resource.jdbc.managed.local.ManagedDataSource;
14  import org.apache.tomcat.jdbc.pool.DataSourceProxy;
15  import org.apache.tomcat.jdbc.pool.PoolConfiguration;
16  
17  import psiprobe.model.DataSourceInfo;
18  
19  /**
20   * Datasource accessor OpenEJB / TomEE.
21   */
22  public class OpenEjbManagedDatasourceAccessor implements DatasourceAccessor {
23  
24    @Override
25    public DataSourceInfo getInfo(Object resource) {
26      DataSourceInfo dataSourceInfo = null;
27      if (canMap(resource)) {
28        PoolConfiguration conf = (PoolConfiguration) unwrap(resource);
29        DataSourceProxy proxy = (DataSourceProxy) conf;
30  
31        dataSourceInfo = new DataSourceInfo();
32        dataSourceInfo.setBusyConnections(proxy.getNumActive());
33        dataSourceInfo.setEstablishedConnections(proxy.getNumIdle() + proxy.getNumActive());
34        dataSourceInfo.setMaxConnections(conf.getMaxActive());
35        dataSourceInfo.setJdbcUrl(conf.getUrl());
36        dataSourceInfo.setUsername(conf.getUsername());
37        dataSourceInfo.setResettable(false);
38        dataSourceInfo.setType("tomee-jdbc");
39      }
40      return dataSourceInfo;
41    }
42  
43    @Override
44    public boolean reset(Object resource) {
45      return false;
46    }
47  
48    @Override
49    public boolean canMap(Object resource) {
50      if ("org.apache.openejb.resource.jdbc.managed.local.ManagedDataSource"
51          .equals(resource.getClass().getName())) {
52        Object wrapped = unwrap(resource);
53        return wrapped instanceof PoolConfiguration;
54      }
55      return false;
56    }
57  
58    /**
59     * Unwrap.
60     *
61     * @param resource the resource
62     *
63     * @return the object
64     */
65    private Object unwrap(Object resource) {
66      return ((ManagedDataSource) resource).getDelegate();
67    }
68  
69  }