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.counter.service.persistence.CounterFinder;
018    import com.liferay.counter.service.persistence.CounterPersistence;
019    import com.liferay.portal.dao.shard.ShardDataSourceTargetSource;
020    import com.liferay.portal.dao.shard.ShardSessionFactoryTargetSource;
021    import com.liferay.portal.kernel.dao.shard.ShardUtil;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.service.persistence.ClassNamePersistence;
025    import com.liferay.portal.service.persistence.CompanyPersistence;
026    import com.liferay.portal.service.persistence.PortalPreferencesPersistence;
027    import com.liferay.portal.service.persistence.ReleasePersistence;
028    import com.liferay.portal.service.persistence.ResourceActionPersistence;
029    import com.liferay.portal.service.persistence.ServiceComponentPersistence;
030    import com.liferay.portal.service.persistence.ShardPersistence;
031    import com.liferay.portal.service.persistence.VirtualHostPersistence;
032    import com.liferay.portal.util.PropsValues;
033    
034    import org.aopalliance.intercept.MethodInterceptor;
035    import org.aopalliance.intercept.MethodInvocation;
036    
037    /**
038     * @author Michael Young
039     * @author Alexander Chow
040     * @author Shuyang Zhou
041     */
042    public class ShardPersistenceAdvice implements MethodInterceptor {
043    
044            @Override
045            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
046                    ShardDataSourceTargetSource shardDataSourceTargetSource =
047                            _shardAdvice.getShardDataSourceTargetSource();
048                    ShardSessionFactoryTargetSource shardSessionFactoryTargetSource =
049                            _shardAdvice.getShardSessionFactoryTargetSource();
050    
051                    if ((shardDataSourceTargetSource == null) ||
052                            (shardSessionFactoryTargetSource == null)) {
053    
054                            return methodInvocation.proceed();
055                    }
056    
057                    Object target = methodInvocation.getThis();
058    
059                    if (target instanceof ClassNamePersistence ||
060                            target instanceof CompanyPersistence ||
061                            target instanceof CounterFinder ||
062                            target instanceof CounterPersistence ||
063                            target instanceof PortalPreferencesPersistence ||
064                            target instanceof ReleasePersistence ||
065                            target instanceof ResourceActionPersistence ||
066                            target instanceof ServiceComponentPersistence ||
067                            target instanceof ShardPersistence ||
068                            target instanceof VirtualHostPersistence) {
069    
070                            String currentShardName = ShardUtil.setTargetSource(
071                                    PropsValues.SHARD_DEFAULT_NAME);
072    
073                            if (_log.isDebugEnabled()) {
074                                    _log.debug(
075                                            "Using default shard for " + methodInvocation.toString());
076                            }
077    
078                            _shardAdvice.pushCompanyService(PropsValues.SHARD_DEFAULT_NAME);
079    
080                            try {
081                                    return methodInvocation.proceed();
082                            }
083                            finally {
084                                    _shardAdvice.popCompanyService();
085    
086                                    ShardUtil.setTargetSource(currentShardName);
087                            }
088                    }
089    
090                    if (_shardAdvice.getGlobalCall() == null) {
091                            String shardName = _shardAdvice.setShardNameByCompany();
092    
093                            ShardUtil.setTargetSource(shardName);
094    
095                            if (_log.isInfoEnabled()) {
096                                    _log.info(
097                                            "Using shard name " + shardName + " for " +
098                                                    methodInvocation.toString());
099                            }
100    
101                            return methodInvocation.proceed();
102                    }
103                    else {
104                            return methodInvocation.proceed();
105                    }
106            }
107    
108            public void setShardAdvice(ShardAdvice shardAdvice) {
109                    _shardAdvice = shardAdvice;
110            }
111    
112            private static Log _log = LogFactoryUtil.getLog(
113                    ShardPersistenceAdvice.class);
114    
115            private ShardAdvice _shardAdvice;
116    
117    }