001    /**
002     * Copyright (c) 2000-2013 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 Map<String, SessionFactory> getSessionFactories() {
037                    return _sessionFactories;
038            }
039    
040            public SessionFactory getSessionFactory() {
041                    return _sessionFactory.get();
042            }
043    
044            public Object getTarget() throws Exception {
045                    return getSessionFactory();
046            }
047    
048            public Class<?> getTargetClass() {
049                    return _sessionFactories.get(PropsValues.SHARD_DEFAULT_NAME).getClass();
050            }
051    
052            public boolean isStatic() {
053                    return false;
054            }
055    
056            public void releaseTarget(Object target) throws Exception {
057            }
058    
059            public void setSessionFactory(String shardName) {
060                    _sessionFactory.set(_sessionFactories.get(shardName));
061            }
062    
063            public void setShardDataSourceTargetSource(
064                            ShardDataSourceTargetSource shardDataSourceTargetSource)
065                    throws Exception {
066    
067                    Map<String, DataSource> dataSources =
068                            shardDataSourceTargetSource.getDataSources();
069    
070                    for (String shardName : dataSources.keySet()) {
071                            DataSource dataSource = dataSources.get(shardName);
072    
073                            PortalHibernateConfiguration portalHibernateConfiguration =
074                                    new PortalHibernateConfiguration();
075    
076                            portalHibernateConfiguration.setDataSource(dataSource);
077    
078                            SessionFactory sessionFactory =
079                                    portalHibernateConfiguration.buildSessionFactory();
080    
081                            _sessionFactories.put(shardName, sessionFactory);
082                    }
083            }
084    
085            private static Map<String, SessionFactory> _sessionFactories =
086                    new HashMap<String, SessionFactory>();
087    
088            private static ThreadLocal<SessionFactory> _sessionFactory =
089                    new CentralizedThreadLocal<SessionFactory>(false) {
090    
091                    @Override
092                    protected SessionFactory initialValue() {
093                            return _sessionFactories.get(PropsValues.SHARD_DEFAULT_NAME);
094                    }
095    
096            };
097    
098    }