001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JPersistenceContext.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.persistence;
025:
026: import javax.persistence.EntityManager;
027: import javax.persistence.EntityManagerFactory;
028:
029: import org.ow2.easybeans.persistence.xml.JPersistenceUnitInfo;
030:
031: /**
032: * This class manages persistence contexts associated to a persistence unit.
033: * @author Florent Benoit
034: */
035: public class JPersistenceContext {
036:
037: /**
038: * Persistence unit info used by this persistence context.
039: */
040: private JPersistenceUnitInfo jPersistenceUnitInfo;
041:
042: /**
043: * EntityManager factory.
044: */
045: private EntityManagerFactory entityManagerFactory = null;
046:
047: /**
048: * Tx entity manager handler.
049: */
050: private TxEntityManagerHandler txEntityManagerHandler = null;
051:
052: /**
053: * Tx entity manager.
054: */
055: private TxEntityManager txEntityManager = null;
056:
057: /**
058: * Build a new persistence context based on a given persistence-unit info.
059: * @param jPersistenceUnitInfo information on the persistence unit.
060: */
061: public JPersistenceContext(
062: final JPersistenceUnitInfo jPersistenceUnitInfo) {
063: this .jPersistenceUnitInfo = jPersistenceUnitInfo;
064: init();
065: }
066:
067: /**
068: * Initialize entity manager (and some factoriese) used by Java EE components.
069: */
070: private void init() {
071: this .entityManagerFactory = jPersistenceUnitInfo
072: .getPersistenceProvider()
073: .createContainerEntityManagerFactory(
074: jPersistenceUnitInfo, null);
075: this .txEntityManagerHandler = new TxEntityManagerHandler(
076: entityManagerFactory);
077: this .txEntityManager = new TxEntityManager(
078: txEntityManagerHandler);
079: }
080:
081: /**
082: * Gets the EntityManager used for Transaction-Scoped.
083: * @return the EntityManager used for Transaction-Scoped
084: */
085: public EntityManager getTxEntityManager() {
086: return txEntityManager;
087: }
088:
089: /**
090: * Gets the EntityManager factory.
091: * @return the EntityManager factory
092: */
093: public EntityManagerFactory getEntityManagerFactory() {
094: return entityManagerFactory;
095: }
096:
097: /**
098: * Sets the current entity manager (used when to transaction is active).
099: */
100: public void addCurrent() {
101: txEntityManagerHandler.addCurrent();
102: }
103:
104: /**
105: * Sets back to the previous entity manager and close the current entity manager.
106: */
107: public void closeCurrentAndReturnToPrevious() {
108: txEntityManagerHandler.closeCurrentAndReturnToPrevious();
109: }
110:
111: }
|