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