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 Dialect getDialect() throws ORMException {
043                    return new DialectImpl();
044            }
045    
046            @Override
047            public Session openNewSession(Connection connection) throws ORMException {
048                    EntityManager entityManager =
049                            _entityManagerFactory.createEntityManager();
050    
051                    return new NewSessionImpl(entityManager);
052            }
053    
054            @Override
055            public Session openSession() throws ORMException {
056                    return _session;
057            }
058    
059            @PersistenceUnit
060            public void setEntityManagerFactory(
061                    EntityManagerFactory entityManagerFactory) {
062    
063                    _entityManagerFactory = entityManagerFactory;
064            }
065    
066            public void setSession(Session session) {
067                    _session = session;
068            }
069    
070            private EntityManagerFactory _entityManagerFactory;
071            private Session _session;
072    
073    }