001    /**
002     * Copyright (c) 2000-2010 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;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.util.PropsValues;
020    
021    import javax.sql.DataSource;
022    
023    /**
024     * @author Alexander Chow
025     */
026    public class ShardUtil {
027    
028            public static String COMPANY_SCOPE = "COMPANY_SCOPE";
029    
030            public static DataSource getDataSource() {
031                    return _shardAdvice.getDataSource();
032            }
033    
034            public static ShardSelector getShardSelector() {
035                    return _shardSelector;
036            }
037    
038            public static boolean isEnabled() {
039                    if (_shardAdvice != null) {
040                            return true;
041                    }
042                    else {
043                            return false;
044                    }
045            }
046    
047            public static String popCompanyService() {
048                    String value = null;
049    
050                    if (_shardAdvice != null) {
051                            value = _shardAdvice.popCompanyService();
052                    }
053    
054                    return value;
055            }
056    
057            public static void pushCompanyService(long companyId) {
058                    if (_shardAdvice != null) {
059                            _shardAdvice.pushCompanyService(companyId);
060                    }
061            }
062    
063            public static void pushCompanyService(String shardName) {
064                    if (_shardAdvice != null) {
065                            _shardAdvice.pushCompanyService(shardName);
066                    }
067            }
068    
069            public void setShardAdvice(ShardAdvice shardAdvice) {
070                    _shardAdvice = shardAdvice;
071            }
072    
073            private static Log _log = LogFactoryUtil.getLog(ShardUtil.class);
074    
075            private static ShardAdvice _shardAdvice;
076            private static ShardSelector _shardSelector;
077    
078            static {
079                    try {
080                            _shardSelector = (ShardSelector)Class.forName(
081                                    PropsValues.SHARD_SELECTOR).newInstance();
082                    }
083                    catch (Exception e) {
084                            _log.error(e, e);
085                    }
086            }
087    
088    }