001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 java.util.Map;
022    import java.util.Set;
023    
024    import javax.sql.DataSource;
025    
026    import org.springframework.aop.TargetSource;
027    
028    /**
029     * @author Michael Young
030     */
031    public class ShardDataSourceTargetSource implements TargetSource {
032    
033            public String[] getAvailableShardNames() {
034                    return _availableShardNames;
035            }
036    
037            public DataSource getDataSource() {
038                    return _dataSource.get();
039            }
040    
041            public Map<String, DataSource> getDataSources() {
042                    return _dataSources;
043            }
044    
045            public Object getTarget() throws Exception {
046                    return getDataSource();
047            }
048    
049            public Class<DataSource> getTargetClass() {
050                    return DataSource.class;
051            }
052    
053            public boolean isStatic() {
054                    return false;
055            }
056    
057            public void releaseTarget(Object target) throws Exception {
058            }
059    
060            public void setDataSource(String shardName) {
061                    _dataSource.set(_dataSources.get(shardName));
062            }
063    
064            public void setDataSources(Map<String, DataSource> dataSources) {
065                    _dataSources = dataSources;
066    
067                    Set<String> shardNames = _dataSources.keySet();
068    
069                    _availableShardNames = shardNames.toArray(
070                            new String[shardNames.size()]);
071    
072                    if (_log.isInfoEnabled()) {
073                            _log.info(
074                                    "Sharding configured with " + _availableShardNames.length +
075                                            " data sources");
076                    }
077            }
078    
079            private static ThreadLocal<DataSource> _dataSource =
080                    new ThreadLocal<DataSource>() {
081    
082                    @Override
083                    protected DataSource initialValue() {
084                            return _dataSources.get(PropsValues.SHARD_DEFAULT_NAME);
085                    }
086    
087            };
088    
089            private static Log _log = LogFactoryUtil.getLog(
090                    ShardDataSourceTargetSource.class);
091    
092            private static String[] _availableShardNames;
093            private static Map<String, DataSource> _dataSources;
094    
095    }