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    import org.springframework.beans.factory.BeanFactory;
030    import org.springframework.beans.factory.BeanFactoryAware;
031    
032    /**
033     * @author Michael Young
034     * @author Alexander Chow
035     */
036    public class ShardSessionFactoryTargetSource
037            implements BeanFactoryAware, 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.setBeanFactory(_beanFactory);
050                            portalHibernateConfiguration.setDataSource(dataSource);
051    
052                            SessionFactory sessionFactory =
053                                    portalHibernateConfiguration.buildSessionFactory();
054    
055                            _sessionFactories.put(shardName, sessionFactory);
056                    }
057            }
058    
059            public Map<String, SessionFactory> getSessionFactories() {
060                    return _sessionFactories;
061            }
062    
063            public SessionFactory 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 setBeanFactory(BeanFactory beanFactory) {
088                    _beanFactory = beanFactory;
089            }
090    
091            public void setSessionFactory(String shardName) {
092                    _sessionFactory.set(_sessionFactories.get(shardName));
093            }
094    
095            public void setShardDataSourceTargetSource(
096                    ShardDataSourceTargetSource shardDataSourceTargetSource) {
097    
098                    _shardDataSourceTargetSource = shardDataSourceTargetSource;
099            }
100    
101            private static final Map<String, SessionFactory> _sessionFactories =
102                    new HashMap<String, SessionFactory>();
103    
104            private static final ThreadLocal<SessionFactory> _sessionFactory =
105                    new CentralizedThreadLocal<SessionFactory>(false) {
106    
107                    @Override
108                    protected SessionFactory initialValue() {
109                            return _sessionFactories.get(PropsValues.SHARD_DEFAULT_NAME);
110                    }
111    
112            };
113    
114            private BeanFactory _beanFactory;
115            private ShardDataSourceTargetSource _shardDataSourceTargetSource;
116    
117    }