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.Portlet;
020    
021    import java.lang.reflect.Method;
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 ShardPortletAdvice implements MethodInterceptor {
032    
033            @Override
034            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
035                    Method method = methodInvocation.getMethod();
036                    String methodName = method.getName();
037    
038                    Object[] arguments = methodInvocation.getArguments();
039    
040                    if ((arguments == null) || (arguments.length == 0)) {
041                            return methodInvocation.proceed();
042                    }
043    
044                    Object argument = arguments[0];
045    
046                    long companyId = -1;
047    
048                    if (argument instanceof Long) {
049                            if (methodName.equals("checkPortlets") ||
050                                    methodName.equals("clonePortlet") ||
051                                    methodName.equals("getPortletById") ||
052                                    methodName.equals("getPortletByStrutsPath") ||
053                                    methodName.equals("getPortlets") ||
054                                    methodName.equals("hasPortlet") ||
055                                    methodName.equals("updatePortlet")) {
056    
057                                    companyId = (Long)argument;
058                            }
059                    }
060                    else if (argument instanceof Portlet) {
061                            if (methodName.equals("checkPortlet") ||
062                                    methodName.equals("deployRemotePortlet") ||
063                                    methodName.equals("destroyPortlet") ||
064                                    methodName.equals("destroyRemotePortlet")) {
065    
066                                    Portlet portlet = (Portlet)argument;
067    
068                                    companyId = portlet.getCompanyId();
069                            }
070                    }
071    
072                    if (companyId <= 0) {
073                            return methodInvocation.proceed();
074                    }
075    
076                    if (_log.isInfoEnabled()) {
077                            _log.info(
078                                    "Setting company service to shard of companyId " + companyId +
079                                            " for " + methodInvocation.toString());
080                    }
081    
082                    Object returnValue = null;
083    
084                    _shardAdvice.pushCompanyService(companyId);
085    
086                    try {
087                            returnValue = methodInvocation.proceed();
088                    }
089                    finally {
090                            _shardAdvice.popCompanyService();
091                    }
092    
093                    return returnValue;
094            }
095    
096            public void setShardAdvice(ShardAdvice shardAdvice) {
097                    _shardAdvice = shardAdvice;
098            }
099    
100            private static Log _log = LogFactoryUtil.getLog(ShardPortletAdvice.class);
101    
102            private ShardAdvice _shardAdvice;
103    
104    }