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.orm.jpa;
016    
017    import com.liferay.portal.kernel.dao.orm.Dialect;
018    import com.liferay.portal.kernel.dao.orm.ORMException;
019    import com.liferay.portal.kernel.dao.orm.Session;
020    import com.liferay.portal.kernel.dao.orm.SessionFactory;
021    
022    import java.sql.Connection;
023    
024    import javax.persistence.EntityManager;
025    import javax.persistence.EntityManagerFactory;
026    import javax.persistence.PersistenceUnit;
027    
028    /**
029     * @author Prashant Dighe
030     * @author Brian Wing Shun Chan
031     */
032    public class SessionFactoryImpl implements SessionFactory {
033    
034            @Override
035            public void closeSession(Session session) throws ORMException {
036                    if (session != null) {
037                            session.close();
038                    }
039            }
040    
041            @Override
042            public Session getCurrentSession() {
043                    return _session;
044            }
045    
046            @Override
047            public Dialect getDialect() throws ORMException {
048                    return new DialectImpl();
049            }
050    
051            @Override
052            public Session openNewSession(Connection connection) throws ORMException {
053                    EntityManager entityManager =
054                            _entityManagerFactory.createEntityManager();
055    
056                    return new NewSessionImpl(entityManager);
057            }
058    
059            @Override
060            public Session openSession() throws ORMException {
061                    return _session;
062            }
063    
064            @PersistenceUnit
065            public void setEntityManagerFactory(
066                    EntityManagerFactory entityManagerFactory) {
067    
068                    _entityManagerFactory = entityManagerFactory;
069            }
070    
071            public void setSession(Session session) {
072                    _session = session;
073            }
074    
075            private EntityManagerFactory _entityManagerFactory;
076            private Session _session;
077    
078    }