001    /**
002     * Copyright (c) 2000-2011 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.dao.shard.ShardUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    
021    import org.aopalliance.intercept.MethodInterceptor;
022    import org.aopalliance.intercept.MethodInvocation;
023    
024    /**
025     * @author Michael Young
026     * @author Alexander Chow
027     * @author Shuyang Zhou
028     */
029    public class ShardIterativelyAdvice implements MethodInterceptor {
030    
031            /**
032             * Invoke a join point across all shards while using the company service
033             * stack.
034             *
035             * @see ShardGloballyAdvice
036             */
037            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
038                    if (_log.isInfoEnabled()) {
039                            _log.info(
040                                    "Iterating through all shards for " +
041                                            methodInvocation.toString());
042                    }
043    
044                    for (String shardName : ShardUtil.getAvailableShardNames()) {
045                            _shardAdvice.pushCompanyService(shardName);
046    
047                            try {
048                                    methodInvocation.proceed();
049                            }
050                            finally {
051                                    _shardAdvice.popCompanyService();
052                            }
053                    }
054    
055                    return null;
056            }
057    
058            public void setShardAdvice(ShardAdvice shardAdvice) {
059                    _shardAdvice = shardAdvice;
060            }
061    
062            private static Log _log = LogFactoryUtil.getLog(
063                    ShardIterativelyAdvice.class);
064    
065            private ShardAdvice _shardAdvice;
066    
067    }