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            @Override
032            public String[] getAvailableShardNames() {
033                    ShardDataSourceTargetSource shardDataSourceTargetSource =
034                            (ShardDataSourceTargetSource)
035                                    InfrastructureUtil.getShardDataSourceTargetSource();
036    
037                    if (shardDataSourceTargetSource != null) {
038                            return shardDataSourceTargetSource.getAvailableShardNames();
039                    }
040    
041                    return null;
042            }
043    
044            @Override
045            public String getCurrentShardName() {
046                    return _shardAdvice.getCurrentShardName();
047            }
048    
049            @Override
050            public DataSource getDataSource() {
051                    return _shardAdvice.getDataSource();
052            }
053    
054            @Override
055            public String getDefaultShardName() {
056                    return PropsValues.SHARD_DEFAULT_NAME;
057            }
058    
059            @Override
060            public boolean isEnabled() {
061                    if (_shardAdvice != null) {
062                            return true;
063                    }
064                    else {
065                            return false;
066                    }
067            }
068    
069            @Override
070            public String popCompanyService() {
071                    String value = null;
072    
073                    if (_shardAdvice != null) {
074                            value = _shardAdvice.popCompanyService();
075                    }
076    
077                    return value;
078            }
079    
080            @Override
081            public void pushCompanyService(long companyId) {
082                    if (_shardAdvice != null) {
083                            _shardAdvice.pushCompanyService(companyId);
084                    }
085            }
086    
087            @Override
088            public void pushCompanyService(String shardName) {
089                    if (_shardAdvice != null) {
090                            _shardAdvice.pushCompanyService(shardName);
091                    }
092            }
093    
094            public void setShardAdvice(ShardAdvice shardAdvice) {
095                    _shardAdvice = shardAdvice;
096            }
097    
098            @Override
099            public String setTargetSource(String shardName) {
100                    if (_shardAdvice == null) {
101                            return null;
102                    }
103    
104                    String currentShardName = _shardAdvice.getShardName();
105    
106                    ShardDataSourceTargetSource shardDataSourceTargetSource =
107                            _shardAdvice.getShardDataSourceTargetSource();
108    
109                    shardDataSourceTargetSource.setDataSource(shardName);
110    
111                    ShardSessionFactoryTargetSource shardSessionFactoryTargetSource =
112                            _shardAdvice.getShardSessionFactoryTargetSource();
113    
114                    shardSessionFactoryTargetSource.setSessionFactory(shardName);
115    
116                    return currentShardName;
117            }
118    
119            private static ShardAdvice _shardAdvice;
120    
121    }