001
014
015 package com.liferay.portal.dao.shard.advice;
016
017 import com.liferay.portal.kernel.cache.CacheRegistryUtil;
018 import com.liferay.portal.kernel.dao.shard.ShardUtil;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021
022 import org.aopalliance.intercept.MethodInterceptor;
023 import org.aopalliance.intercept.MethodInvocation;
024
025
030 public class ShardIterativelyAdvice implements MethodInterceptor {
031
032
038 @Override
039 public Object invoke(MethodInvocation methodInvocation) throws Throwable {
040 Object returnValue = null;
041
042 for (String shardName : ShardUtil.getAvailableShardNames()) {
043 if (_log.isInfoEnabled()) {
044 _log.info(
045 "Invoking shard " + shardName + " for " +
046 methodInvocation.toString());
047 }
048
049 String currentShardName = ShardUtil.setTargetSource(shardName);
050
051 _shardAdvice.pushCompanyService(shardName);
052
053 try {
054 Object value = methodInvocation.proceed();
055
056 if (shardName.equals(ShardUtil.getDefaultShardName())) {
057 returnValue = value;
058 }
059 }
060 finally {
061 _shardAdvice.popCompanyService();
062
063 ShardUtil.setTargetSource(currentShardName);
064
065 CacheRegistryUtil.clear();
066 }
067 }
068
069 return returnValue;
070 }
071
072 public void setShardAdvice(ShardAdvice shardAdvice) {
073 _shardAdvice = shardAdvice;
074 }
075
076 private static final Log _log = LogFactoryUtil.getLog(
077 ShardIterativelyAdvice.class);
078
079 private ShardAdvice _shardAdvice;
080
081 }