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 public Object invoke(MethodInvocation methodInvocation) throws Throwable {
039 Method method = methodInvocation.getMethod();
040 String methodName = method.getName();
041
042 Object[] arguments = methodInvocation.getArguments();
043
044 String shardName = PropsValues.SHARD_DEFAULT_NAME;
045
046 if (methodName.equals("addCompany")) {
047 String webId = (String)arguments[0];
048 String virtualHostname = (String)arguments[1];
049 String mx = (String)arguments[2];
050 shardName = (String)arguments[3];
051
052 shardName = _shardAdvice.getCompanyShardName(
053 webId, virtualHostname, mx, shardName);
054
055 arguments[3] = shardName;
056 }
057 else if (methodName.equals("checkCompany")) {
058 String webId = (String)arguments[0];
059
060 if (!webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
061 if (arguments.length == 3) {
062 String mx = (String)arguments[1];
063 shardName = (String)arguments[2];
064
065 shardName = _shardAdvice.getCompanyShardName(
066 webId, null, mx, shardName);
067
068 arguments[2] = shardName;
069 }
070
071 try {
072 Company company = CompanyLocalServiceUtil.getCompanyByWebId(
073 webId);
074
075 shardName = company.getShardName();
076 }
077 catch (NoSuchCompanyException nsce) {
078 }
079 }
080 }
081 else if (methodName.startsWith("update")) {
082 long companyId = (Long)arguments[0];
083
084 Shard shard = ShardLocalServiceUtil.getShard(
085 Company.class.getName(), companyId);
086
087 shardName = shard.getName();
088 }
089 else {
090 return methodInvocation.proceed();
091 }
092
093 if (_log.isInfoEnabled()) {
094 _log.info(
095 "Setting company service to shard " + shardName + " for " +
096 methodInvocation.toString());
097 }
098
099 Object returnValue = null;
100
101 _shardAdvice.pushCompanyService(shardName);
102
103 try {
104 returnValue = method.invoke(methodInvocation.getThis(), arguments);
105 }
106 finally {
107 _shardAdvice.popCompanyService();
108 }
109
110 return returnValue;
111 }
112
113 public void setShardAdvice(ShardAdvice shardAdvice) {
114 _shardAdvice = shardAdvice;
115 }
116
117 private static Log _log = LogFactoryUtil.getLog(ShardCompanyAdvice.class);
118
119 private ShardAdvice _shardAdvice;
120
121 }