001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }