KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > persistence > PersistenceUnitManager


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: PersistenceUnitManager.java 572 2006-06-04 21:23:27Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.persistence;
27
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import javax.persistence.EntityManager;
33 import javax.persistence.EntityManagerFactory;
34 import javax.persistence.PersistenceContextType;
35
36 import org.objectweb.easybeans.persistence.api.EZBPersistenceUnitManager;
37 import org.objectweb.easybeans.persistence.xml.JPersistenceUnitInfo;
38
39 /**
40  * This class manages persistence units ands allow to return EntityManager or
41  * EntityManagerFactory.
42  * @author Florent Benoit
43  */

44 public class PersistenceUnitManager implements EZBPersistenceUnitManager {
45
46     /**
47      * List of persistence unit objects managed by their name.
48      */

49     private Map JavaDoc<String JavaDoc, JPersistenceContext> persistenceContexts = null;
50
51     /**
52      * Build a new manager with given persistence units.
53      * @param persistenceUnitInfos a list of persistence unit infos.
54      */

55     public PersistenceUnitManager(final JPersistenceUnitInfo[] persistenceUnitInfos) {
56         this.persistenceContexts = new HashMap JavaDoc<String JavaDoc, JPersistenceContext>();
57         // Add each object to the map
58
if (persistenceUnitInfos != null) {
59             for (JPersistenceUnitInfo pUnitInfo : persistenceUnitInfos) {
60                 persistenceContexts.put(pUnitInfo.getPersistenceUnitName(), new JPersistenceContext(pUnitInfo));
61             }
62         }
63     }
64
65     /**
66      * Gets the persistence context associated to a given persistence unit name.
67      * @param unitName the name of the persistence unit object.
68      * @return the object which is found by matching the expected name.
69      */

70     private JPersistenceContext getPersistenceContext(final String JavaDoc unitName) {
71         if (unitName == null || unitName.equals("")) {
72             if (persistenceContexts.size() == 0) {
73                 throw new IllegalArgumentException JavaDoc("No persistence-unit defined");
74             } else if (persistenceContexts.size() > 1) {
75                 throw new IllegalArgumentException JavaDoc("Too many persistence-unit defined, cannot take the default one.");
76             }
77             return persistenceContexts.values().iterator().next();
78         }
79         // else, return the unit associated to the given name
80
JPersistenceContext persistenceContext = persistenceContexts.get(unitName);
81         if (persistenceContext == null) {
82             throw new IllegalArgumentException JavaDoc("No persistence-unit with name '" + unitName + "' defined.");
83         }
84         return persistenceContext;
85     }
86
87     /**
88      * Gets an entity manager for the given unit name and the extra attributes.
89      * @param unitName the name of the persistence unit
90      * @param type the type of the persistence context
91      * @return entity manager corresponding to arguments
92      */

93     public EntityManager getEntityManager(final String JavaDoc unitName, final PersistenceContextType type) {
94         // TODO: Manage also extended persistence context;
95
return getPersistenceContext(unitName).getTxEntityManager();
96     }
97
98     /**
99      * Gets an entity manager factory for the given unit name.
100      * @param unitName the name of the persistence unit
101      * @return entity manager factory.
102      */

103     public EntityManagerFactory getEntityManagerFactory(final String JavaDoc unitName) {
104         return getPersistenceContext(unitName).getEntityManagerFactory();
105     }
106
107     /**
108      * Create a new EntityManager on each PersistenceContext. (Will be used for
109      * the method lifecycle)
110      */

111     public void addCurrent() {
112         for (Iterator JavaDoc<JPersistenceContext> itContext = persistenceContexts.values().iterator(); itContext.hasNext();) {
113             JPersistenceContext pContext = itContext.next();
114             pContext.addCurrent();
115         }
116     }
117
118     /**
119      * Sets back to the previous entity manager and close the current entity
120      * manager for each persistence context.
121      */

122     public void closeCurrentAndReturnToPrevious() {
123         for (Iterator JavaDoc<JPersistenceContext> itContext = persistenceContexts.values().iterator(); itContext.hasNext();) {
124             JPersistenceContext pContext = itContext.next();
125             pContext.closeCurrentAndReturnToPrevious();
126         }
127     }
128
129     /**
130      * Merge the persistence context of a an other persistent unit manager in
131      * this one. Note that as specified in chapter 6.2.2 (persistence unit
132      * scope), an EAR level component level will only be seen by a subcomponent
133      * if it was not redefined. In our case : don't merge the given unit-name if
134      * the current manager defines this unit-name.
135      * @param otherEZBPersistenceUnitManager the other persistence unit manager
136      * that will be merged into this one.
137      */

138     public void merge(final EZBPersistenceUnitManager otherEZBPersistenceUnitManager) {
139         // do nothing if the given manager is null
140
if (otherEZBPersistenceUnitManager == null) {
141             return;
142         }
143         PersistenceUnitManager otherPersistenceUnitManager = null;
144         if (otherEZBPersistenceUnitManager instanceof PersistenceUnitManager) {
145             otherPersistenceUnitManager = (PersistenceUnitManager) otherEZBPersistenceUnitManager;
146         } else {
147             return;
148         }
149
150         // get all persistence contexts of the given manager
151
for (Iterator JavaDoc<String JavaDoc> itContext = otherPersistenceUnitManager.persistenceContexts.keySet().iterator(); itContext
152                 .hasNext();) {
153             String JavaDoc unitName = itContext.next();
154             // existing in our manager?
155
JPersistenceContext pContext = persistenceContexts.get(unitName);
156             if (pContext == null) {
157                 // add it (not existing)
158
persistenceContexts.put(unitName, otherPersistenceUnitManager.persistenceContexts.get(unitName));
159             }
160         }
161
162     }
163
164 }
165
Popular Tags