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