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.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            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
045                    ShardDataSourceTargetSource shardDataSourceTargetSource =
046                            _shardAdvice.getShardDataSourceTargetSource();
047                    ShardSessionFactoryTargetSource shardSessionFactoryTargetSource =
048                            _shardAdvice.getShardSessionFactoryTargetSource();
049    
050                    if ((shardDataSourceTargetSource == null) ||
051                            (shardSessionFactoryTargetSource == null)) {
052    
053                            return methodInvocation.proceed();
054                    }
055    
056                    Object target = methodInvocation.getThis();
057    
058                    if (target instanceof ClassNamePersistence ||
059                            target instanceof CompanyPersistence ||
060                            target instanceof CounterFinder ||
061                            target instanceof CounterPersistence ||
062                            target instanceof PortalPreferencesPersistence ||
063                            target instanceof ReleasePersistence ||
064                            target instanceof ResourceActionPersistence ||
065                            target instanceof ServiceComponentPersistence ||
066                            target instanceof ShardPersistence ||
067                            target instanceof VirtualHostPersistence) {
068    
069                            String currentShardName = ShardUtil.setTargetSource(
070                                    PropsValues.SHARD_DEFAULT_NAME);
071    
072                            if (_log.isDebugEnabled()) {
073                                    _log.debug(
074                                            "Using default shard for " + methodInvocation.toString());
075                            }
076    
077                            _shardAdvice.pushCompanyService(PropsValues.SHARD_DEFAULT_NAME);
078    
079                            try {
080                                    return methodInvocation.proceed();
081                            }
082                            finally {
083                                    _shardAdvice.popCompanyService();
084    
085                                    ShardUtil.setTargetSource(currentShardName);
086                            }
087                    }
088    
089                    if (_shardAdvice.getGlobalCall() == null) {
090                            String shardName = _shardAdvice.setShardNameByCompany();
091    
092                            ShardUtil.setTargetSource(shardName);
093    
094                            if (_log.isInfoEnabled()) {
095                                    _log.info(
096                                            "Using shard name " + shardName + " for " +
097                                                    methodInvocation.toString());
098                            }
099    
100                            return methodInvocation.proceed();
101                    }
102                    else {
103                            return methodInvocation.proceed();
104                    }
105            }
106    
107            public void setShardAdvice(ShardAdvice shardAdvice) {
108                    _shardAdvice = shardAdvice;
109            }
110    
111            private static Log _log = LogFactoryUtil.getLog(
112                    ShardPersistenceAdvice.class);
113    
114            private ShardAdvice _shardAdvice;
115    
116    }