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