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("loadGetPortletsPool") ||
057                                    methodName.equals("updatePortlet")) {
058    
059                                    companyId = (Long)argument;
060                            }
061                    }
062                    else if (argument instanceof Portlet) {
063                            if (methodName.equals("checkPortlet") ||
064                                    methodName.equals("deployRemotePortlet") ||
065                                    methodName.equals("destroyPortlet") ||
066                                    methodName.equals("destroyRemotePortlet")) {
067    
068                                    Portlet portlet = (Portlet)argument;
069    
070                                    companyId = portlet.getCompanyId();
071                            }
072                    }
073    
074                    if (companyId <= 0) {
075                            return methodInvocation.proceed();
076                    }
077    
078                    if (_log.isInfoEnabled()) {
079                            _log.info(
080                                    "Setting company service to shard of companyId " + companyId +
081                                            " for " + methodInvocation.toString());
082                    }
083    
084                    Object returnValue = null;
085    
086                    _shardAdvice.pushCompanyService(companyId);
087    
088                    try {
089                            returnValue = methodInvocation.proceed();
090                    }
091                    finally {
092                            _shardAdvice.popCompanyService();
093                    }
094    
095                    return returnValue;
096            }
097    
098            public void setShardAdvice(ShardAdvice shardAdvice) {
099                    _shardAdvice = shardAdvice;
100            }
101    
102            private static Log _log = LogFactoryUtil.getLog(ShardPortletAdvice.class);
103    
104            private ShardAdvice _shardAdvice;
105    
106    }