001    /**
002     * Copyright (c) 2000-2013 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.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    /**
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                                    (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                    else {
152                            String shardName = companyServiceStack.peek();
153    
154                            _setShardName(shardName);
155    
156                            return shardName;
157                    }
158            }
159    
160            public void setShardSessionFactoryTargetSource(
161                    ShardSessionFactoryTargetSource shardSessionFactoryTargetSource) {
162    
163                    _shardSessionFactoryTargetSource = shardSessionFactoryTargetSource;
164            }
165    
166            private Stack<String> _getCompanyServiceStack() {
167                    Stack<String> companyServiceStack = _companyServiceStack.get();
168    
169                    if (companyServiceStack == null) {
170                            companyServiceStack = new Stack<String>();
171    
172                            _companyServiceStack.set(companyServiceStack);
173                    }
174    
175                    return companyServiceStack;
176            }
177    
178            private void _setShardName(String shardName) {
179                    _shardName.set(shardName);
180            }
181    
182            private String _setShardNameByCompanyId(long companyId)
183                    throws PortalException, SystemException {
184    
185                    String shardName = PropsValues.SHARD_DEFAULT_NAME;
186    
187                    if (companyId != 0) {
188                            Shard shard = ShardLocalServiceUtil.getShard(
189                                    Company.class.getName(), companyId);
190    
191                            shardName = shard.getName();
192                    }
193    
194                    _setShardName(shardName);
195    
196                    return shardName;
197            }
198    
199            private static Log _log = LogFactoryUtil.getLog(ShardAdvice.class);
200    
201            private static ThreadLocal<Stack<String>> _companyServiceStack =
202                    new ThreadLocal<Stack<String>>();
203            private static ThreadLocal<Object> _globalCall = new ThreadLocal<Object>();
204            private static ThreadLocal<String> _shardName =
205                    new InitialThreadLocal<String>(
206                            ShardAdvice.class + "._shardName", PropsValues.SHARD_DEFAULT_NAME);
207            private static ShardSelector _shardSelector;
208    
209            private ShardDataSourceTargetSource _shardDataSourceTargetSource;
210            private ShardSessionFactoryTargetSource _shardSessionFactoryTargetSource;
211    
212            static {
213                    try {
214                            _shardSelector = (ShardSelector)Class.forName(
215                                    PropsValues.SHARD_SELECTOR).newInstance();
216                    }
217                    catch (Exception e) {
218                            _log.error(e, e);
219                    }
220            }
221    
222    }