001    /**
002     * Copyright (c) 2000-2012 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.InfrastructureUtil;
025    import com.liferay.portal.kernel.util.InitialThreadLocal;
026    import com.liferay.portal.model.Company;
027    import com.liferay.portal.model.Shard;
028    import com.liferay.portal.security.auth.CompanyThreadLocal;
029    import com.liferay.portal.service.ShardLocalServiceUtil;
030    import com.liferay.portal.util.PropsValues;
031    
032    import java.util.EmptyStackException;
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                    String shardName = null;
080    
081                    try {
082                            shardName = _getCompanyServiceStack().peek();
083                    }
084                    catch (EmptyStackException ese) {
085                    }
086    
087                    if (shardName == null) {
088                            shardName = PropsValues.SHARD_DEFAULT_NAME;
089                    }
090    
091                    return shardName;
092            }
093    
094            public DataSource getDataSource() {
095                    return _shardDataSourceTargetSource.getDataSource();
096            }
097    
098            public Object getGlobalCall() {
099                    return _globalCall.get();
100            }
101    
102            public ShardDataSourceTargetSource getShardDataSourceTargetSource() {
103                    return _shardDataSourceTargetSource;
104            }
105    
106            public String getShardName() {
107                    return _shardName.get();
108            }
109    
110            public ShardSessionFactoryTargetSource
111                    getShardSessionFactoryTargetSource() {
112    
113                    return _shardSessionFactoryTargetSource;
114            }
115    
116            public String popCompanyService() {
117                    return _getCompanyServiceStack().pop();
118            }
119    
120            public void pushCompanyService(long companyId) {
121                    try {
122                            Shard shard = ShardLocalServiceUtil.getShard(
123                                    Company.class.getName(), companyId);
124    
125                            String shardName = shard.getName();
126    
127                            pushCompanyService(shardName);
128                    }
129                    catch (Exception e) {
130                            _log.error(e, e);
131                    }
132            }
133    
134            public void pushCompanyService(String shardName) {
135                    _getCompanyServiceStack().push(shardName);
136            }
137    
138            public void setGlobalCall(Object obj) {
139                    _globalCall.set(obj);
140            }
141    
142            public void setShardDataSourceTargetSource(
143                    ShardDataSourceTargetSource shardDataSourceTargetSource) {
144    
145                    _shardDataSourceTargetSource = shardDataSourceTargetSource;
146            }
147    
148            public void setShardNameByCompany() throws Throwable {
149                    Stack<String> companyServiceStack = _getCompanyServiceStack();
150    
151                    if (companyServiceStack.isEmpty()) {
152                            long companyId = CompanyThreadLocal.getCompanyId();
153    
154                            _setShardNameByCompanyId(companyId);
155                    }
156                    else {
157                            String shardName = companyServiceStack.peek();
158    
159                            _setShardName(shardName);
160                    }
161            }
162    
163            public void setShardSessionFactoryTargetSource(
164                    ShardSessionFactoryTargetSource shardSessionFactoryTargetSource) {
165    
166                    _shardSessionFactoryTargetSource = shardSessionFactoryTargetSource;
167            }
168    
169            private Stack<String> _getCompanyServiceStack() {
170                    Stack<String> companyServiceStack = _companyServiceStack.get();
171    
172                    if (companyServiceStack == null) {
173                            companyServiceStack = new Stack<String>();
174    
175                            _companyServiceStack.set(companyServiceStack);
176                    }
177    
178                    return companyServiceStack;
179            }
180    
181            private void _setShardName(String shardName) {
182                    _shardName.set(shardName);
183            }
184    
185            private void _setShardNameByCompanyId(long companyId)
186                    throws PortalException, SystemException {
187    
188                    if (companyId == 0) {
189                            _setShardName(PropsValues.SHARD_DEFAULT_NAME);
190                    }
191                    else {
192                            Shard shard = ShardLocalServiceUtil.getShard(
193                                    Company.class.getName(), companyId);
194    
195                            String shardName = shard.getName();
196    
197                            _setShardName(shardName);
198                    }
199            }
200    
201            private static Log _log = LogFactoryUtil.getLog(ShardAdvice.class);
202    
203            private static ThreadLocal<Stack<String>> _companyServiceStack =
204                    new ThreadLocal<Stack<String>>();
205            private static ThreadLocal<Object> _globalCall = new ThreadLocal<Object>();
206            private static ThreadLocal<String> _shardName =
207                    new InitialThreadLocal<String>(
208                            ShardAdvice.class + "._shardName", PropsValues.SHARD_DEFAULT_NAME);
209            private static ShardSelector _shardSelector;
210    
211            private ShardDataSourceTargetSource _shardDataSourceTargetSource;
212            private ShardSessionFactoryTargetSource _shardSessionFactoryTargetSource;
213    
214            static {
215                    try {
216                            _shardSelector = (ShardSelector)Class.forName(
217                                    PropsValues.SHARD_SELECTOR).newInstance();
218                    }
219                    catch (Exception e) {
220                            _log.error(e, e);
221                    }
222            }
223    
224    }