001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.dao.jdbc.pool.metrics;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import com.zaxxer.hikari.HikariPoolMXBean;
021    
022    import java.lang.management.ManagementFactory;
023    import java.lang.reflect.Method;
024    
025    import javax.management.JMX;
026    import javax.management.MBeanServer;
027    import javax.management.ObjectName;
028    
029    /**
030     * @author Mladen Cikara
031     */
032    public class HikariConnectionPoolMetrics extends BaseConnectionPoolMetrics {
033    
034            public HikariConnectionPoolMetrics(Object dataSource) {
035                    _dataSource = dataSource;
036            }
037    
038            @Override
039            public int getNumActive() {
040                    if (!_initializationFailed && (_connectionPool == null)) {
041                            initializeConnectionPool();
042                    }
043    
044                    if (_initializationFailed) {
045                            return -1;
046                    }
047    
048                    return _connectionPool.getActiveConnections();
049            }
050    
051            @Override
052            public int getNumIdle() {
053                    if (!_initializationFailed && (_connectionPool == null)) {
054                            initializeConnectionPool();
055                    }
056    
057                    if (_initializationFailed) {
058                            return -1;
059                    }
060    
061                    return _connectionPool.getIdleConnections();
062            }
063    
064            @Override
065            protected Object getDataSource() {
066                    if (_initializationFailed) {
067                            return null;
068                    }
069    
070                    return _dataSource;
071            }
072    
073            @Override
074            protected String getPoolName() {
075                    Thread currentThread = Thread.currentThread();
076    
077                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
078    
079                    try {
080                            Class<?> clazz = contextClassLoader.loadClass(
081                                    "com.zaxxer.hikari.HikariDataSource");
082    
083                            Method method = clazz.getMethod("getPoolName");
084    
085                            return (String)method.invoke(_dataSource);
086                    }
087                    catch (Exception e) {
088                            if (_log.isWarnEnabled()) {
089                                    _log.warn(e.getMessage(), e);
090                            }
091                    }
092    
093                    return null;
094            }
095    
096            @Override
097            protected void initializeConnectionPool() {
098                    if (getPoolName() == null ) {
099                            _initializationFailed = true;
100    
101                            return;
102                    }
103    
104                    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
105    
106                    try {
107                            ObjectName objectName = new ObjectName(
108                                    "com.zaxxer.hikari:type=Pool (" + getPoolName() + ")");
109    
110                            _connectionPool = JMX.newMXBeanProxy(
111                                    mBeanServer, objectName, HikariPoolMXBean.class);
112                    }
113                    catch (Exception e) {
114                            _initializationFailed = true;
115    
116                            if (_log.isDebugEnabled()) {
117                                    _log.debug(e.getMessage());
118                            }
119                    }
120    
121                    super.initializeConnectionPool();
122            }
123    
124            private static final Log _log = LogFactoryUtil.getLog(
125                    HikariConnectionPoolMetrics.class);
126    
127            private HikariPoolMXBean _connectionPool;
128            private final Object _dataSource;
129            private boolean _initializationFailed = false;
130    
131    }