001    /**
002     * Copyright (c) 2000-2011 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.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            public void closeSession(Session session) throws ORMException {
035                    if (session != null) {
036                            session.close();
037                    }
038            }
039    
040            public Dialect getDialect() throws ORMException {
041                    return new DialectImpl();
042            }
043    
044            public Session openNewSession(Connection connection) throws ORMException {
045                    EntityManager entityManager =
046                            _entityManagerFactory.createEntityManager();
047    
048                    return new NewSessionImpl(entityManager);
049            }
050    
051            public Session openSession() throws ORMException {
052                    return _session;
053            }
054    
055            @PersistenceUnit
056            public void setEntityManagerFactory(
057                    EntityManagerFactory entityManagerFactory) {
058    
059                    _entityManagerFactory = entityManagerFactory;
060            }
061    
062            public void setSession(Session session) {
063                    _session = session;
064            }
065    
066            private EntityManagerFactory _entityManagerFactory;
067            private Session _session;
068    
069    }