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.dao.shard.ShardDataSourceTargetSource;
018    import com.liferay.portal.dao.shard.ShardSessionFactoryTargetSource;
019    import com.liferay.portal.kernel.dao.shard.ShardUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    
023    import org.aopalliance.intercept.MethodInterceptor;
024    import org.aopalliance.intercept.MethodInvocation;
025    
026    /**
027     * @author Michael Young
028     * @author Alexander Chow
029     * @author Shuyang Zhou
030     */
031    public class ShardGloballyAdvice implements MethodInterceptor {
032    
033            /**
034             * Invoke a join point across all shards while ignoring the company service
035             * stack.
036             *
037             * @see ShardIterativelyAdvice
038             */
039            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
040                    _shardAdvice.setGlobalCall(new Object());
041    
042                    try {
043                            if (_log.isInfoEnabled()) {
044                                    _log.info(
045                                            "All shards invoked for " + methodInvocation.toString());
046                            }
047    
048                            for (String shardName : ShardUtil.getAvailableShardNames()) {
049                                    ShardDataSourceTargetSource dataSourceTargetSource =
050                                            _shardAdvice.getShardDataSourceTargetSource();
051    
052                                    dataSourceTargetSource.setDataSource(shardName);
053    
054                                    ShardSessionFactoryTargetSource shardSessionFactoryTargetSource
055                                            = _shardAdvice.getShardSessionFactoryTargetSource();
056    
057                                    shardSessionFactoryTargetSource.setSessionFactory(shardName);
058    
059                                    methodInvocation.proceed();
060                            }
061                    }
062                    finally {
063                            _shardAdvice.setGlobalCall(null);
064                    }
065    
066                    return null;
067            }
068    
069            public void setShardAdvice(ShardAdvice shardAdvice) {
070                    _shardAdvice = shardAdvice;
071            }
072    
073            private static Log _log = LogFactoryUtil.getLog(
074                    ShardGloballyAdvice.class);
075    
076            private ShardAdvice _shardAdvice;
077    
078    }