001
014
015 package com.liferay.portal.dao.shard.advice;
016
017 import com.liferay.portal.dao.shard.ShardDataSourceTargetSource;
018 import com.liferay.portal.dao.shard.ShardSelector;
019 import com.liferay.portal.dao.shard.ShardSessionFactoryTargetSource;
020 import com.liferay.portal.kernel.exception.PortalException;
021 import com.liferay.portal.kernel.exception.SystemException;
022 import com.liferay.portal.kernel.log.Log;
023 import com.liferay.portal.kernel.log.LogFactoryUtil;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.InfrastructureUtil;
026 import com.liferay.portal.kernel.util.InitialThreadLocal;
027 import com.liferay.portal.model.Company;
028 import com.liferay.portal.model.Shard;
029 import com.liferay.portal.security.auth.CompanyThreadLocal;
030 import com.liferay.portal.service.ShardLocalServiceUtil;
031 import com.liferay.portal.util.PropsValues;
032
033 import java.util.HashMap;
034 import java.util.Map;
035 import java.util.Stack;
036
037 import javax.sql.DataSource;
038
039
044 public class ShardAdvice {
045
046 public void afterPropertiesSet() {
047 if (_shardDataSourceTargetSource == null) {
048 _shardDataSourceTargetSource =
049 (ShardDataSourceTargetSource)InfrastructureUtil.
050 getShardDataSourceTargetSource();
051 }
052
053 if (_shardSessionFactoryTargetSource == null) {
054 _shardSessionFactoryTargetSource =
055 (ShardSessionFactoryTargetSource)InfrastructureUtil.
056 getShardSessionFactoryTargetSource();
057 }
058 }
059
060 public String getCompanyShardName(
061 String webId, String virtualHostname, String mx, String shardName) {
062
063 Map<String, String> shardParams = new HashMap<String, String>();
064
065 shardParams.put("webId", webId);
066 shardParams.put("mx", mx);
067
068 if (virtualHostname != null) {
069 shardParams.put("virtualHostname", virtualHostname);
070 }
071
072 shardName = _shardSelector.getShardName(
073 ShardSelector.COMPANY_SCOPE, shardName, shardParams);
074
075 return shardName;
076 }
077
078 public String getCurrentShardName() {
079 Stack<String> stack = _getCompanyServiceStack();
080
081 if (stack.isEmpty()) {
082 return PropsValues.SHARD_DEFAULT_NAME;
083 }
084
085 return GetterUtil.getString(
086 stack.peek(), PropsValues.SHARD_DEFAULT_NAME);
087 }
088
089 public DataSource getDataSource() {
090 return _shardDataSourceTargetSource.getDataSource();
091 }
092
093 public Object getGlobalCall() {
094 return _globalCall.get();
095 }
096
097 public ShardDataSourceTargetSource getShardDataSourceTargetSource() {
098 return _shardDataSourceTargetSource;
099 }
100
101 public String getShardName() {
102 return _shardName.get();
103 }
104
105 public ShardSessionFactoryTargetSource
106 getShardSessionFactoryTargetSource() {
107
108 return _shardSessionFactoryTargetSource;
109 }
110
111 public String popCompanyService() {
112 return _getCompanyServiceStack().pop();
113 }
114
115 public void pushCompanyService(long companyId) {
116 try {
117 Shard shard = ShardLocalServiceUtil.getShard(
118 Company.class.getName(), companyId);
119
120 String shardName = shard.getName();
121
122 pushCompanyService(shardName);
123 }
124 catch (Exception e) {
125 _log.error(e, e);
126 }
127 }
128
129 public void pushCompanyService(String shardName) {
130 _getCompanyServiceStack().push(shardName);
131 }
132
133 public void setGlobalCall(Object obj) {
134 _globalCall.set(obj);
135 }
136
137 public void setShardDataSourceTargetSource(
138 ShardDataSourceTargetSource shardDataSourceTargetSource) {
139
140 _shardDataSourceTargetSource = shardDataSourceTargetSource;
141 }
142
143 public String setShardNameByCompany() throws Throwable {
144 Stack<String> companyServiceStack = _getCompanyServiceStack();
145
146 if (companyServiceStack.isEmpty()) {
147 long companyId = CompanyThreadLocal.getCompanyId();
148
149 return _setShardNameByCompanyId(companyId);
150 }
151
152 String shardName = companyServiceStack.peek();
153
154 _setShardName(shardName);
155
156 return shardName;
157 }
158
159 public void setShardSessionFactoryTargetSource(
160 ShardSessionFactoryTargetSource shardSessionFactoryTargetSource) {
161
162 _shardSessionFactoryTargetSource = shardSessionFactoryTargetSource;
163 }
164
165 private Stack<String> _getCompanyServiceStack() {
166 Stack<String> companyServiceStack = _companyServiceStack.get();
167
168 if (companyServiceStack == null) {
169 companyServiceStack = new Stack<String>();
170
171 _companyServiceStack.set(companyServiceStack);
172 }
173
174 return companyServiceStack;
175 }
176
177 private void _setShardName(String shardName) {
178 _shardName.set(shardName);
179 }
180
181 private String _setShardNameByCompanyId(long companyId)
182 throws PortalException, SystemException {
183
184 String shardName = PropsValues.SHARD_DEFAULT_NAME;
185
186 if (companyId != 0) {
187 Shard shard = ShardLocalServiceUtil.getShard(
188 Company.class.getName(), companyId);
189
190 shardName = shard.getName();
191 }
192
193 _setShardName(shardName);
194
195 return shardName;
196 }
197
198 private static Log _log = LogFactoryUtil.getLog(ShardAdvice.class);
199
200 private static ThreadLocal<Stack<String>> _companyServiceStack =
201 new ThreadLocal<Stack<String>>();
202 private static ThreadLocal<Object> _globalCall = new ThreadLocal<Object>();
203 private static ThreadLocal<String> _shardName =
204 new InitialThreadLocal<String>(
205 ShardAdvice.class + "._shardName", PropsValues.SHARD_DEFAULT_NAME);
206 private static ShardSelector _shardSelector;
207
208 private ShardDataSourceTargetSource _shardDataSourceTargetSource;
209 private ShardSessionFactoryTargetSource _shardSessionFactoryTargetSource;
210
211 static {
212 try {
213 Class<?> clazz = Class.forName(PropsValues.SHARD_SELECTOR);
214
215 _shardSelector = (ShardSelector)clazz.newInstance();
216 }
217 catch (Exception e) {
218 _log.error(e, e);
219 }
220 }
221
222 }