001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            @Override
044            public Object getTarget() throws Exception {
045                    return getSessionFactory();
046            }
047    
048            @Override
049            public Class<?> getTargetClass() {
050                    return _sessionFactories.get(PropsValues.SHARD_DEFAULT_NAME).getClass();
051            }
052    
053            @Override
054            public boolean isStatic() {
055                    return false;
056            }
057    
058            @Override
059            public void releaseTarget(Object target) throws Exception {
060            }
061    
062            public void setSessionFactory(String shardName) {
063                    _sessionFactory.set(_sessionFactories.get(shardName));
064            }
065    
066            public void setShardDataSourceTargetSource(
067                            ShardDataSourceTargetSource shardDataSourceTargetSource)
068                    throws Exception {
069    
070                    Map<String, DataSource> dataSources =
071                            shardDataSourceTargetSource.getDataSources();
072    
073                    for (String shardName : dataSources.keySet()) {
074                            DataSource dataSource = dataSources.get(shardName);
075    
076                            PortalHibernateConfiguration portalHibernateConfiguration =
077                                    new PortalHibernateConfiguration();
078    
079                            portalHibernateConfiguration.setDataSource(dataSource);
080    
081                            SessionFactory sessionFactory =
082                                    portalHibernateConfiguration.buildSessionFactory();
083    
084                            _sessionFactories.put(shardName, sessionFactory);
085                    }
086            }
087    
088            private static Map<String, SessionFactory> _sessionFactories =
089                    new HashMap<String, SessionFactory>();
090    
091            private static ThreadLocal<SessionFactory> _sessionFactory =
092                    new ThreadLocal<SessionFactory>() {
093    
094                    @Override
095                    protected SessionFactory initialValue() {
096                            return _sessionFactories.get(PropsValues.SHARD_DEFAULT_NAME);
097                    }
098    
099            };
100    
101    }