001
014
015 package com.liferay.portal.dao.shard.advice;
016
017 import com.liferay.portal.NoSuchCompanyException;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.model.Company;
021 import com.liferay.portal.model.Shard;
022 import com.liferay.portal.service.CompanyLocalServiceUtil;
023 import com.liferay.portal.service.ShardLocalServiceUtil;
024 import com.liferay.portal.util.PropsValues;
025
026 import java.lang.reflect.Method;
027
028 import org.aopalliance.intercept.MethodInterceptor;
029 import org.aopalliance.intercept.MethodInvocation;
030
031
036 public class ShardCompanyAdvice implements MethodInterceptor {
037
038 @Override
039 public Object invoke(MethodInvocation methodInvocation) throws Throwable {
040 Method method = methodInvocation.getMethod();
041 String methodName = method.getName();
042
043 Object[] arguments = methodInvocation.getArguments();
044
045 String shardName = PropsValues.SHARD_DEFAULT_NAME;
046
047 if (methodName.equals("addCompany")) {
048 String webId = (String)arguments[0];
049 String virtualHostname = (String)arguments[1];
050 String mx = (String)arguments[2];
051 shardName = (String)arguments[3];
052
053 shardName = _shardAdvice.getCompanyShardName(
054 webId, virtualHostname, mx, shardName);
055
056 arguments[3] = shardName;
057 }
058 else if (methodName.equals("checkCompany")) {
059 String webId = (String)arguments[0];
060
061 if (!webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
062 if (arguments.length == 3) {
063 String mx = (String)arguments[1];
064 shardName = (String)arguments[2];
065
066 shardName = _shardAdvice.getCompanyShardName(
067 webId, null, mx, shardName);
068
069 arguments[2] = shardName;
070 }
071
072 try {
073 Company company = CompanyLocalServiceUtil.getCompanyByWebId(
074 webId);
075
076 shardName = company.getShardName();
077 }
078 catch (NoSuchCompanyException nsce) {
079 }
080 }
081 }
082 else if (methodName.startsWith("update")) {
083 long companyId = (Long)arguments[0];
084
085 Shard shard = ShardLocalServiceUtil.getShard(
086 Company.class.getName(), companyId);
087
088 shardName = shard.getName();
089 }
090 else {
091 return methodInvocation.proceed();
092 }
093
094 if (_log.isInfoEnabled()) {
095 _log.info(
096 "Setting company service to shard " + shardName + " for " +
097 methodInvocation.toString());
098 }
099
100 Object returnValue = null;
101
102 _shardAdvice.pushCompanyService(shardName);
103
104 try {
105 returnValue = method.invoke(methodInvocation.getThis(), arguments);
106 }
107 finally {
108 _shardAdvice.popCompanyService();
109 }
110
111 return returnValue;
112 }
113
114 public void setShardAdvice(ShardAdvice shardAdvice) {
115 _shardAdvice = shardAdvice;
116 }
117
118 private static Log _log = LogFactoryUtil.getLog(ShardCompanyAdvice.class);
119
120 private ShardAdvice _shardAdvice;
121
122 }