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