001    /**
002     * Copyright (c) 2000-2012 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            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    }