001    /**
002     * Copyright (c) 2000-2013 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.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 ShardGloballyAdvice implements MethodInterceptor {
031    
032            /**
033             * Invoke a join point across all shards while ignoring the company service
034             * stack.
035             *
036             * @see ShardIterativelyAdvice
037             */
038            @Override
039            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
040                    Object returnValue = null;
041    
042                    _shardAdvice.setGlobalCall(new Object());
043    
044                    try {
045                            for (String shardName : ShardUtil.getAvailableShardNames()) {
046                                    if (_log.isInfoEnabled()) {
047                                            _log.info(
048                                                    "Invoking shard " + shardName + " for " +
049                                                            methodInvocation.toString());
050                                    }
051    
052                                    ShardUtil.setTargetSource(shardName);
053    
054                                    _shardAdvice.pushCompanyService(shardName);
055    
056                                    try {
057                                            Object value = methodInvocation.proceed();
058    
059                                            if (shardName.equals(ShardUtil.getDefaultShardName())) {
060                                                    returnValue = value;
061                                            }
062                                    }
063                                    finally {
064                                            _shardAdvice.popCompanyService();
065    
066                                            CacheRegistryUtil.clear();
067                                    }
068                            }
069                    }
070                    finally {
071                            _shardAdvice.setGlobalCall(null);
072                    }
073    
074                    return returnValue;
075            }
076    
077            public void setShardAdvice(ShardAdvice shardAdvice) {
078                    _shardAdvice = shardAdvice;
079            }
080    
081            private static Log _log = LogFactoryUtil.getLog(ShardGloballyAdvice.class);
082    
083            private ShardAdvice _shardAdvice;
084    
085    }