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