001    /**
002     * Copyright (c) 2000-2012 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.kernel.util.CentralizedThreadLocal;
020    import com.liferay.portal.util.PropsValues;
021    
022    import java.util.Map;
023    import java.util.Set;
024    
025    import javax.sql.DataSource;
026    
027    import org.springframework.aop.TargetSource;
028    
029    /**
030     * @author Michael Young
031     */
032    public class ShardDataSourceTargetSource implements TargetSource {
033    
034            public String[] getAvailableShardNames() {
035                    return _availableShardNames;
036            }
037    
038            public DataSource getDataSource() {
039                    return _dataSource.get();
040            }
041    
042            public Map<String, DataSource> getDataSources() {
043                    return _dataSources;
044            }
045    
046            public Object getTarget() throws Exception {
047                    return getDataSource();
048            }
049    
050            public Class<DataSource> getTargetClass() {
051                    return DataSource.class;
052            }
053    
054            public boolean isStatic() {
055                    return false;
056            }
057    
058            public void releaseTarget(Object target) throws Exception {
059            }
060    
061            public void resetDataSource() {
062                    DataSource dataSource = _dataSources.get(
063                            PropsValues.SHARD_DEFAULT_NAME);
064    
065                    _dataSource.set(dataSource);
066            }
067    
068            public void setDataSource(String shardName) {
069                    DataSource dataSource = _dataSources.get(shardName);
070    
071                    _dataSource.set(dataSource);
072            }
073    
074            public void setDataSources(Map<String, DataSource> dataSources) {
075                    _dataSources = dataSources;
076    
077                    Set<String> shardNames = _dataSources.keySet();
078    
079                    _availableShardNames = shardNames.toArray(
080                            new String[shardNames.size()]);
081    
082                    if (_log.isInfoEnabled()) {
083                            _log.info(
084                                    "Sharding configured with " + _availableShardNames.length +
085                                            " data sources");
086                    }
087            }
088    
089            private static Log _log = LogFactoryUtil.getLog(
090                    ShardDataSourceTargetSource.class);
091    
092            private static String[] _availableShardNames;
093    
094            private static ThreadLocal<DataSource> _dataSource =
095                    new CentralizedThreadLocal<DataSource>(false) {
096    
097                    @Override
098                    protected DataSource initialValue() {
099                            return _dataSources.get(PropsValues.SHARD_DEFAULT_NAME);
100                    }
101    
102            };
103    
104            private static Map<String, DataSource> _dataSources;
105    
106    }