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