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