KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > injection > EntityManagerHelper


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: EntityManagerHelper.java 1121 2006-09-27 08:51:06Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.injection;
27
28 import javax.persistence.EntityManager;
29 import javax.persistence.EntityManagerFactory;
30 import javax.persistence.PersistenceContextType;
31
32 import org.objectweb.easybeans.api.Factory;
33 import org.objectweb.easybeans.api.container.EZBEJBContext;
34 import org.objectweb.easybeans.log.JLog;
35 import org.objectweb.easybeans.log.JLogFactory;
36 import org.objectweb.easybeans.persistence.api.EZBPersistenceUnitManager;
37
38 /**
39  * Helper class for injecting EntityManager instance in the bean.
40  * @author Florent Benoit
41  */

42 public final class EntityManagerHelper {
43
44     /**
45      * Logger.
46      */

47     private static JLog logger = JLogFactory.getLog(EntityManagerHelper.class);
48
49     /**
50      * Utility class, no public constructor.
51      */

52     private EntityManagerHelper() {
53
54     }
55
56     /**
57      * Gets an entity manager for the given session Context.
58      * @param ejbContext on which we should provide an entity manager.
59      * @param unitName name of the persistence unit.
60      * @param type the persistence context type.
61      * @return instance of an entity manager which will be used by the
62      * bean/interceptor.
63      */

64     public static EntityManager getEntityManager(final EZBEJBContext ejbContext, final String JavaDoc unitName,
65             final PersistenceContextType type) {
66         // get bean's factory
67
Factory factory = ejbContext.getBean().getEasyBeansFactory();
68         EZBPersistenceUnitManager persistenceUnitManager = factory.getContainer().getPersistenceUnitManager();
69         if (persistenceUnitManager != null) {
70             return persistenceUnitManager.getEntityManager(unitName, type);
71         }
72         logger.warn("Requested an EntityManager object but there is no persistenceUnitManager associated"
73                 + " to this bean/interceptor : {0}", factory);
74         return null;
75     }
76
77     /**
78      * Gets an entity manager factory for the given session Context.
79      * @param ejbContext on which we should provide an entity manager
80      * factory.
81      * @param unitName name of the persistence unit.
82      * @return instance of an entity manager factory which will be used by the
83      * bean/interceptor.
84      */

85     public static EntityManagerFactory getEntityManagerFactory(final EZBEJBContext ejbContext,
86             final String JavaDoc unitName) {
87         // get bean's factory
88
Factory factory = ejbContext.getBean().getEasyBeansFactory();
89         EZBPersistenceUnitManager persistenceUnitManager = factory.getContainer().getPersistenceUnitManager();
90         if (persistenceUnitManager != null) {
91             return persistenceUnitManager.getEntityManagerFactory(unitName);
92         }
93         logger.warn("Requested an EntityManagerFactory but there is no persistenceUnitManager associated"
94                 + " to this bean/interceptor : {0}", factory);
95         return null;
96     }
97
98 }
99
Popular Tags