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.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource;
021    
022    import java.sql.SQLException;
023    
024    /**
025     * @author Mladen Cikara
026     */
027    public class C3P0ConnectionPoolMetrics extends BaseConnectionPoolMetrics {
028    
029            public C3P0ConnectionPoolMetrics(
030                    AbstractPoolBackedDataSource abstractPoolBackedDataSource) {
031    
032                    _abstractPoolBackedDataSource = abstractPoolBackedDataSource;
033            }
034    
035            @Override
036            public int getNumActive() {
037                    try {
038                            return _abstractPoolBackedDataSource.getNumBusyConnections();
039                    }
040                    catch (SQLException sqle) {
041                            if (_log.isDebugEnabled()) {
042                                    _log.debug(sqle.getMessage());
043                            }
044    
045                            return -1;
046                    }
047            }
048    
049            @Override
050            public int getNumIdle() {
051                    try {
052                            return _abstractPoolBackedDataSource.getNumIdleConnections();
053                    }
054                    catch (SQLException sqle) {
055                            if (_log.isDebugEnabled()) {
056                                    _log.debug(sqle.getMessage());
057                            }
058    
059                            return -1;
060                    }
061            }
062    
063            @Override
064            protected Object getDataSource() {
065                    return _abstractPoolBackedDataSource;
066            }
067    
068            @Override
069            protected String getPoolName() {
070                    return _abstractPoolBackedDataSource.getDataSourceName();
071            }
072    
073            private static final Log _log = LogFactoryUtil.getLog(
074                    C3P0ConnectionPoolMetrics.class);
075    
076            private final AbstractPoolBackedDataSource _abstractPoolBackedDataSource;
077    
078    }