001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
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    /**
026     * @author Michael Young
027     * @author Alexander Chow
028     * @author Shuyang Zhou
029     */
030    public class ShardIterativelyAdvice implements MethodInterceptor {
031    
032            /**
033             * Invoke a join point across all shards while using the company service
034             * stack.
035             *
036             * @see ShardGloballyAdvice
037             */
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                            _shardAdvice.pushCompanyService(shardName);
050    
051                            try {
052                                    Object value = methodInvocation.proceed();
053    
054                                    if (shardName.equals(ShardUtil.getDefaultShardName())) {
055                                            returnValue = value;
056                                    }
057                            }
058                            finally {
059                                    _shardAdvice.popCompanyService();
060    
061                                    CacheRegistryUtil.clear();
062                            }
063                    }
064    
065                    return returnValue;
066            }
067    
068            public void setShardAdvice(ShardAdvice shardAdvice) {
069                    _shardAdvice = shardAdvice;
070            }
071    
072            private static Log _log = LogFactoryUtil.getLog(
073                    ShardIterativelyAdvice.class);
074    
075            private ShardAdvice _shardAdvice;
076    
077    }