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