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