001    /**
002     * Copyright (c) 2000-present 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.bean.PortalBeanLocatorUtil;
018    import com.liferay.portal.kernel.dao.shard.ShardSessionFactoryTargetSource;
019    
020    import org.hibernate.HibernateException;
021    import org.hibernate.SessionFactory;
022    import org.hibernate.classic.Session;
023    import org.hibernate.engine.SessionFactoryImplementor;
024    
025    import org.springframework.orm.hibernate3.SessionFactoryUtils;
026    import org.springframework.orm.hibernate3.SpringSessionContext;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class ShardSpringSessionContext extends SpringSessionContext {
032    
033            public ShardSpringSessionContext(SessionFactoryImplementor sessionFactory) {
034                    super(null);
035            }
036    
037            @Override
038            public Session currentSession() throws HibernateException {
039                    try {
040                            if (_shardSessionFactoryTargetSource == null) {
041                                    _shardSessionFactoryTargetSource =
042                                            (ShardSessionFactoryTargetSource)
043                                                    PortalBeanLocatorUtil.locate(
044                                                            "shardSessionFactoryTargetSource");
045                            }
046    
047                            return (Session)SessionFactoryUtils.doGetSession(
048                                    (SessionFactory)
049                                            _shardSessionFactoryTargetSource.getSessionFactory(),
050                                    false);
051                    }
052                    catch (IllegalStateException ise) {
053                            throw new HibernateException(ise);
054                    }
055            }
056    
057            private ShardSessionFactoryTargetSource _shardSessionFactoryTargetSource;
058    
059    }