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.dao.shard.ShardSessionFactoryTargetSource;
019    import com.liferay.portal.kernel.util.CentralizedThreadLocal;
020    import com.liferay.portal.spring.hibernate.PortalHibernateConfiguration;
021    import com.liferay.portal.util.PropsValues;
022    
023    import java.util.HashMap;
024    import java.util.Map;
025    
026    import javax.sql.DataSource;
027    
028    import org.hibernate.SessionFactory;
029    
030    import org.springframework.aop.TargetSource;
031    
032    /**
033     * @author Michael Young
034     * @author Alexander Chow
035     */
036    public class DefaultShardSessionFactoryTargetSource
037            implements ShardSessionFactoryTargetSource, TargetSource {
038    
039            public void afterPropertiesSet() throws Exception {
040                    Map<String, DataSource> dataSources =
041                            _shardDataSourceTargetSource.getDataSources();
042    
043                    for (String shardName : dataSources.keySet()) {
044                            DataSource dataSource = dataSources.get(shardName);
045    
046                            PortalHibernateConfiguration portalHibernateConfiguration =
047                                    new PortalHibernateConfiguration();
048    
049                            portalHibernateConfiguration.setDataSource(dataSource);
050    
051                            SessionFactory sessionFactory =
052                                    portalHibernateConfiguration.buildSessionFactory();
053    
054                            _sessionFactories.put(shardName, sessionFactory);
055                    }
056            }
057    
058            public Map<String, SessionFactory> getSessionFactories() {
059                    return _sessionFactories;
060            }
061    
062            @Override
063            public Object getSessionFactory() {
064                    return _sessionFactory.get();
065            }
066    
067            @Override
068            public Object getTarget() throws Exception {
069                    return getSessionFactory();
070            }
071    
072            @Override
073            public Class<?> getTargetClass() {
074                    return _sessionFactories.get(PropsValues.SHARD_DEFAULT_NAME).getClass();
075            }
076    
077            @Override
078            public boolean isStatic() {
079                    return false;
080            }
081    
082            @Override
083            public void releaseTarget(Object target) throws Exception {
084            }
085    
086            @Override
087            public void setSessionFactory(String shardName) {
088                    _sessionFactory.set(_sessionFactories.get(shardName));
089            }
090    
091            public void setShardDataSourceTargetSource(
092                    ShardDataSourceTargetSource shardDataSourceTargetSource) {
093    
094                    _shardDataSourceTargetSource = shardDataSourceTargetSource;
095            }
096    
097            private static final Map<String, SessionFactory> _sessionFactories =
098                    new HashMap<>();
099    
100            private static final ThreadLocal<SessionFactory> _sessionFactory =
101                    new CentralizedThreadLocal<SessionFactory>(false) {
102    
103                    @Override
104                    protected SessionFactory initialValue() {
105                            return _sessionFactories.get(PropsValues.SHARD_DEFAULT_NAME);
106                    }
107    
108            };
109    
110            private ShardDataSourceTargetSource _shardDataSourceTargetSource;
111    
112    }