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.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    }