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.dao.jdbc.aop.DefaultDynamicDataSourceTargetSource;
018    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    
022    import org.springframework.aop.framework.Advised;
023    import org.springframework.aop.support.AopUtils;
024    import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy;
025    
026    /**
027     * @author Mladen Cikara
028     */
029    public abstract class BaseConnectionPoolMetrics
030            implements ConnectionPoolMetrics {
031    
032            @Override
033            public String getConnectionPoolName() {
034                    if (_name == null) {
035                            initializeConnectionPool();
036                    }
037    
038                    return _name;
039            }
040    
041            protected abstract Object getDataSource();
042    
043            protected abstract String getPoolName();
044    
045            protected void initializeConnectionPool() {
046                    LazyConnectionDataSourceProxy lazyConnectionDataSourceProxy =
047                            (LazyConnectionDataSourceProxy)PortalBeanLocatorUtil.locate(
048                                    "counterDataSource");
049    
050                    Object dataSource = getDataSource();
051    
052                    if (dataSource == null) {
053                            return;
054                    }
055    
056                    if (dataSource.equals(
057                                    lazyConnectionDataSourceProxy.getTargetDataSource())) {
058    
059                            _name = "counterDataSource";
060    
061                            return;
062                    }
063    
064                    lazyConnectionDataSourceProxy =
065                            (LazyConnectionDataSourceProxy)PortalBeanLocatorUtil.locate(
066                                    "liferayDataSource");
067    
068                    Object targetDataSource =
069                            lazyConnectionDataSourceProxy.getTargetDataSource();
070    
071                    if (dataSource.equals(targetDataSource)) {
072                            _name = "liferayDataSource";
073    
074                            return;
075                    }
076                    else if (AopUtils.isAopProxy(targetDataSource) &&
077                                     (targetDataSource instanceof Advised)) {
078    
079                            Advised advised = (Advised)targetDataSource;
080    
081                            targetDataSource = advised.getTargetSource();
082    
083                            if (targetDataSource instanceof
084                                            DefaultDynamicDataSourceTargetSource) {
085    
086                                    try {
087                                            Object readDataSource =
088                                                    ((DefaultDynamicDataSourceTargetSource)
089                                                            targetDataSource).getReadDataSource();
090    
091                                            if (dataSource.equals(readDataSource)) {
092                                                    _name = "readDataSource";
093    
094                                                    return;
095                                            }
096    
097                                            Object writeDataSource =
098                                                    ((DefaultDynamicDataSourceTargetSource)
099                                                            targetDataSource).getWriteDataSource();
100    
101                                            if (dataSource.equals(writeDataSource)) {
102                                                    _name = "writeDataSource";
103    
104                                                    return;
105                                            }
106                                    }
107                                    catch (Exception e) {
108                                            if (_log.isDebugEnabled()) {
109                                                    _log.debug(e.getMessage());
110                                            }
111                                    }
112                            }
113                    }
114    
115                    _name = getPoolName();
116            }
117    
118            private static final Log _log = LogFactoryUtil.getLog(
119                    BaseConnectionPoolMetrics.class);
120    
121            private String _name;
122    
123    }