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