001
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
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 }