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.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.model.Company;
020    import com.liferay.portal.model.Shard;
021    import com.liferay.portal.service.ShardLocalServiceUtil;
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 ShardParameterAdvice implements MethodInterceptor {
032    
033            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
034                    Object[] arguments = methodInvocation.getArguments();
035    
036                    long companyId = (Long)arguments[0];
037    
038                    Shard shard = ShardLocalServiceUtil.getShard(
039                            Company.class.getName(), companyId);
040    
041                    String shardName = shard.getName();
042    
043                    if (_log.isInfoEnabled()) {
044                            _log.info(
045                                    "Setting service to shard " + shardName + " for " +
046                                            methodInvocation.toString());
047                    }
048    
049                    Object returnValue = null;
050    
051                    _shardAdvice.pushCompanyService(shardName);
052    
053                    try {
054                            returnValue = methodInvocation.proceed();
055                    }
056                    finally {
057                            _shardAdvice.popCompanyService();
058                    }
059    
060                    return returnValue;
061            }
062    
063            public void setShardAdvice(ShardAdvice shardAdvice) {
064                    _shardAdvice = shardAdvice;
065            }
066    
067            private static Log _log = LogFactoryUtil.getLog(
068                    ShardParameterAdvice.class);
069    
070            private ShardAdvice _shardAdvice;
071    
072    }