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