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;
016    
017    import com.liferay.portal.dao.shard.advice.ShardAdvice;
018    import com.liferay.portal.kernel.dao.shard.Shard;
019    import com.liferay.portal.kernel.dao.shard.ShardDataSourceTargetSource;
020    import com.liferay.portal.kernel.dao.shard.ShardSessionFactoryTargetSource;
021    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
022    import com.liferay.portal.kernel.util.InfrastructureUtil;
023    import com.liferay.portal.util.PropsValues;
024    
025    import javax.sql.DataSource;
026    
027    /**
028     * @author Alexander Chow
029     */
030    @DoPrivileged
031    public class ShardImpl implements Shard {
032    
033            @Override
034            public String[] getAvailableShardNames() {
035                    ShardDataSourceTargetSource shardDataSourceTargetSource =
036                            InfrastructureUtil.getShardDataSourceTargetSource();
037    
038                    if (shardDataSourceTargetSource != null) {
039                            return shardDataSourceTargetSource.getAvailableShardNames();
040                    }
041    
042                    return null;
043            }
044    
045            @Override
046            public String getCurrentShardName() {
047                    return _shardAdvice.getCurrentShardName();
048            }
049    
050            @Override
051            public DataSource getDataSource() {
052                    return _shardAdvice.getDataSource();
053            }
054    
055            @Override
056            public String getDefaultShardName() {
057                    return PropsValues.SHARD_DEFAULT_NAME;
058            }
059    
060            @Override
061            public boolean isEnabled() {
062                    if (_shardAdvice != null) {
063                            return true;
064                    }
065                    else {
066                            return false;
067                    }
068            }
069    
070            @Override
071            public String popCompanyService() {
072                    String value = null;
073    
074                    if (_shardAdvice != null) {
075                            value = _shardAdvice.popCompanyService();
076                    }
077    
078                    return value;
079            }
080    
081            @Override
082            public void pushCompanyService(long companyId) {
083                    if (_shardAdvice != null) {
084                            _shardAdvice.pushCompanyService(companyId);
085                    }
086            }
087    
088            @Override
089            public void pushCompanyService(String shardName) {
090                    if (_shardAdvice != null) {
091                            _shardAdvice.pushCompanyService(shardName);
092                    }
093            }
094    
095            public void setShardAdvice(ShardAdvice shardAdvice) {
096                    _shardAdvice = shardAdvice;
097            }
098    
099            @Override
100            public String setTargetSource(String shardName) {
101                    if (_shardAdvice == null) {
102                            return null;
103                    }
104    
105                    String currentShardName = _shardAdvice.getCurrentShardName();
106    
107                    ShardDataSourceTargetSource shardDataSourceTargetSource =
108                            _shardAdvice.getShardDataSourceTargetSource();
109    
110                    shardDataSourceTargetSource.setDataSource(shardName);
111    
112                    ShardSessionFactoryTargetSource shardSessionFactoryTargetSource =
113                            _shardAdvice.getShardSessionFactoryTargetSource();
114    
115                    shardSessionFactoryTargetSource.setSessionFactory(shardName);
116    
117                    return currentShardName;
118            }
119    
120            private static ShardAdvice _shardAdvice;
121    
122    }