001
014
015 package com.liferay.portal.dao.shard.advice;
016
017 import com.liferay.counter.service.persistence.CounterFinder;
018 import com.liferay.counter.service.persistence.CounterPersistence;
019 import com.liferay.portal.dao.shard.ShardDataSourceTargetSource;
020 import com.liferay.portal.dao.shard.ShardSessionFactoryTargetSource;
021 import com.liferay.portal.kernel.dao.shard.ShardUtil;
022 import com.liferay.portal.kernel.log.Log;
023 import com.liferay.portal.kernel.log.LogFactoryUtil;
024 import com.liferay.portal.service.persistence.ClassNamePersistence;
025 import com.liferay.portal.service.persistence.CompanyPersistence;
026 import com.liferay.portal.service.persistence.PortalPreferencesPersistence;
027 import com.liferay.portal.service.persistence.ReleasePersistence;
028 import com.liferay.portal.service.persistence.ResourceActionPersistence;
029 import com.liferay.portal.service.persistence.ServiceComponentPersistence;
030 import com.liferay.portal.service.persistence.ShardPersistence;
031 import com.liferay.portal.service.persistence.VirtualHostPersistence;
032 import com.liferay.portal.util.PropsValues;
033
034 import org.aopalliance.intercept.MethodInterceptor;
035 import org.aopalliance.intercept.MethodInvocation;
036
037
042 public class ShardPersistenceAdvice implements MethodInterceptor {
043
044 @Override
045 public Object invoke(MethodInvocation methodInvocation) throws Throwable {
046 ShardDataSourceTargetSource shardDataSourceTargetSource =
047 _shardAdvice.getShardDataSourceTargetSource();
048 ShardSessionFactoryTargetSource shardSessionFactoryTargetSource =
049 _shardAdvice.getShardSessionFactoryTargetSource();
050
051 if ((shardDataSourceTargetSource == null) ||
052 (shardSessionFactoryTargetSource == null)) {
053
054 return methodInvocation.proceed();
055 }
056
057 Object target = methodInvocation.getThis();
058
059 if (target instanceof ClassNamePersistence ||
060 target instanceof CompanyPersistence ||
061 target instanceof CounterFinder ||
062 target instanceof CounterPersistence ||
063 target instanceof PortalPreferencesPersistence ||
064 target instanceof ReleasePersistence ||
065 target instanceof ResourceActionPersistence ||
066 target instanceof ServiceComponentPersistence ||
067 target instanceof ShardPersistence ||
068 target instanceof VirtualHostPersistence) {
069
070 String currentShardName = ShardUtil.setTargetSource(
071 PropsValues.SHARD_DEFAULT_NAME);
072
073 if (_log.isDebugEnabled()) {
074 _log.debug(
075 "Using default shard for " + methodInvocation.toString());
076 }
077
078 _shardAdvice.pushCompanyService(PropsValues.SHARD_DEFAULT_NAME);
079
080 try {
081 return methodInvocation.proceed();
082 }
083 finally {
084 _shardAdvice.popCompanyService();
085
086 ShardUtil.setTargetSource(currentShardName);
087 }
088 }
089
090 if (_shardAdvice.getGlobalCall() == null) {
091 String shardName = _shardAdvice.setShardNameByCompany();
092
093 ShardUtil.setTargetSource(shardName);
094
095 if (_log.isInfoEnabled()) {
096 _log.info(
097 "Using shard name " + shardName + " for " +
098 methodInvocation.toString());
099 }
100
101 return methodInvocation.proceed();
102 }
103 else {
104 return methodInvocation.proceed();
105 }
106 }
107
108 public void setShardAdvice(ShardAdvice shardAdvice) {
109 _shardAdvice = shardAdvice;
110 }
111
112 private static Log _log = LogFactoryUtil.getLog(
113 ShardPersistenceAdvice.class);
114
115 private ShardAdvice _shardAdvice;
116
117 }