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    import com.liferay.portlet.journal.service.JournalArticleLocalService;
022    
023    import java.lang.reflect.Method;
024    
025    import org.aopalliance.intercept.MethodInterceptor;
026    import org.aopalliance.intercept.MethodInvocation;
027    
028    /**
029     * @author Michael Young
030     * @author Alexander Chow
031     * @author Shuyang Zhou
032     */
033    public class ShardGloballyAdvice implements MethodInterceptor {
034    
035            /**
036             * Invoke a join point across all shards while ignoring the company service
037             * stack.
038             *
039             * @see ShardIterativelyAdvice
040             */
041            @Override
042            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
043                    Object returnValue = null;
044    
045                    _shardAdvice.setGlobalCall(new Object());
046    
047                    try {
048                            for (String shardName : ShardUtil.getAvailableShardNames()) {
049                                    if (_log.isInfoEnabled()) {
050                                            _log.info(
051                                                    "Invoking shard " + shardName + " for " +
052                                                            methodInvocation.toString());
053                                    }
054    
055                                    ShardUtil.setTargetSource(shardName);
056    
057                                    _shardAdvice.pushCompanyService(shardName);
058    
059                                    try {
060                                            Object value = methodInvocation.proceed();
061    
062                                            if (shardName.equals(ShardUtil.getDefaultShardName())) {
063                                                    returnValue = value;
064                                            }
065                                    }
066                                    finally {
067                                            _shardAdvice.popCompanyService();
068    
069                                            Method method = methodInvocation.getMethod();
070    
071                                            Class<?> clazz = method.getDeclaringClass();
072    
073                                            String className = clazz.getName();
074    
075                                            String methodName = method.getName();
076    
077                                            if (className.equals(
078                                                            JournalArticleLocalService.class.getName()) &&
079                                                    methodName.equals("checkArticles")) {
080    
081                                                    continue;
082                                            }
083    
084                                            CacheRegistryUtil.clear();
085                                    }
086                            }
087                    }
088                    finally {
089                            _shardAdvice.setGlobalCall(null);
090                    }
091    
092                    return returnValue;
093            }
094    
095            public void setShardAdvice(ShardAdvice shardAdvice) {
096                    _shardAdvice = shardAdvice;
097            }
098    
099            private static Log _log = LogFactoryUtil.getLog(ShardGloballyAdvice.class);
100    
101            private ShardAdvice _shardAdvice;
102    
103    }