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: EntityManagerHelper.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.injection;
025:
026: import javax.persistence.EntityManager;
027: import javax.persistence.EntityManagerFactory;
028: import javax.persistence.PersistenceContextType;
029:
030: import org.ow2.easybeans.api.Factory;
031: import org.ow2.easybeans.api.container.EZBEJBContext;
032: import org.ow2.easybeans.persistence.api.EZBPersistenceUnitManager;
033: import org.ow2.util.log.Log;
034: import org.ow2.util.log.LogFactory;
035:
036: /**
037: * Helper class for injecting EntityManager instance in the bean.
038: * @author Florent Benoit
039: */
040: public final class EntityManagerHelper {
041:
042: /**
043: * Logger.
044: */
045: private static Log logger = LogFactory
046: .getLog(EntityManagerHelper.class);
047:
048: /**
049: * Utility class, no public constructor.
050: */
051: private EntityManagerHelper() {
052:
053: }
054:
055: /**
056: * Gets an entity manager for the given session Context.
057: * @param ejbContext on which we should provide an entity manager.
058: * @param unitName name of the persistence unit.
059: * @param type the persistence context type.
060: * @return instance of an entity manager which will be used by the
061: * bean/interceptor.
062: */
063: public static EntityManager getEntityManager(
064: final EZBEJBContext ejbContext, final String unitName,
065: final PersistenceContextType type) {
066: // get bean's factory
067: Factory factory = ejbContext.getBean().getEasyBeansFactory();
068: EZBPersistenceUnitManager persistenceUnitManager = factory
069: .getContainer().getPersistenceUnitManager();
070: if (persistenceUnitManager != null) {
071: return persistenceUnitManager.getEntityManager(unitName,
072: type);
073: }
074: logger
075: .warn(
076: "Requested an EntityManager object but there is no persistenceUnitManager associated"
077: + " to this bean/interceptor : {0}",
078: factory);
079: return null;
080: }
081:
082: /**
083: * Gets an entity manager factory for the given session Context.
084: * @param ejbContext on which we should provide an entity manager
085: * factory.
086: * @param unitName name of the persistence unit.
087: * @return instance of an entity manager factory which will be used by the
088: * bean/interceptor.
089: */
090: public static EntityManagerFactory getEntityManagerFactory(
091: final EZBEJBContext ejbContext, final String unitName) {
092: // get bean's factory
093: Factory factory = ejbContext.getBean().getEasyBeansFactory();
094: EZBPersistenceUnitManager persistenceUnitManager = factory
095: .getContainer().getPersistenceUnitManager();
096: if (persistenceUnitManager != null) {
097: return persistenceUnitManager
098: .getEntityManagerFactory(unitName);
099: }
100: logger
101: .warn(
102: "Requested an EntityManagerFactory but there is no persistenceUnitManager associated"
103: + " to this bean/interceptor : {0}",
104: factory);
105: return null;
106: }
107:
108: }
|