001: /* JpaUtil.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Mon Dec 17 2007, Created by jeffliu
010: }}IS_NOTE
011:
012: Copyright (C) 2006 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: }}IS_RIGHT
016: */
017:
018: package org.zkoss.zkplus.jpa;
019:
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: import javax.persistence.EntityManager;
025: import javax.persistence.EntityManagerFactory;
026: import javax.persistence.Persistence;
027:
028: import org.zkoss.util.logging.Log;
029: import org.zkoss.zk.ui.Desktop;
030: import org.zkoss.zk.ui.Execution;
031: import org.zkoss.zk.ui.Executions;
032: import org.zkoss.zk.ui.WebApp;
033:
034: /**
035: * This class is used to create and hold open EntityManagerFactory objects
036: * within a Java EE environment.
037: *
038: * @author Jeff
039: * @since 3.0.2
040: */
041: public class JpaUtil {
042:
043: private static final Log log = Log.lookup(JpaUtil.class);
044:
045: public static final String CONFIG = "JpaUtil.PersistenceUnitName";
046:
047: public static final String JPA_EMF_MAP = "org.zkoss.zkplus.jpa.EmfMap";
048:
049: public static final String JPA_EM_MAP = "org.zkoss.zkplus.jpa.EmMap";
050:
051: /*
052: * Get the EntityManagerFactories Map from Execution scope
053: */
054: static Map getEmfMap() {
055: return getMapThreadLocal(JPA_EMF_MAP);
056: }
057:
058: /*
059: * Clean the EntityManagerFactories Map
060: */
061: static void cleanEmfMap() {
062: Executions.getCurrent().removeAttribute(JPA_EMF_MAP);
063: }
064:
065: /*
066: * Get the EntityManagers Map from Execution scope
067: */
068: static Map getEmMap() {
069: return getMapThreadLocal(JPA_EM_MAP);
070: }
071:
072: /*
073: * Clean the EntityManagersMap
074: */
075: static void cleanEmMap() {
076: Executions.getCurrent().removeAttribute(JPA_EM_MAP);
077: }
078:
079: private static Map getMapThreadLocal(String key) {
080: Map map = (Map) Executions.getCurrent().getAttribute(key);
081: if (map == null) {
082: map = new HashMap();
083: Executions.getCurrent().setAttribute(key, map);
084: }
085: return map;
086: }
087:
088: /**
089: * Create or return the EntityManager by peference in zk.xml
090: *
091: * <p>
092: * In WEB-INF/zk.xml, add following lines:
093: *
094: * <pre><code>
095: *<preference>
096: * <name>JPA.PersistenceUnitName</name>
097: * <value>PERSISTENCE_UNIT_NAME</value>
098: *</preference>
099: * </code></pre>
100: *
101: * </p>
102: *
103: * @return EntityManager
104: */
105: public static EntityManagerFactory getEntityManagerFactory() {
106: return initEntityManagerFactory(null, null);
107: }
108:
109: /**
110: * Create or return the EntityManagerFactory for the specified persistence
111: * unit name. </br>*Notice:If the EntityManagerFactory with specified
112: * presistence unit is not created before, a new one will be created.
113: *
114: * @param puName -
115: * Persistence unit name
116: * @return EntityManagerFactory
117: */
118: public static EntityManagerFactory getEntityManagerFactory(
119: String puName) {
120: return initEntityManagerFactory(puName, null);
121: }
122:
123: /**
124: * Create the EntityManagerFactory for the specified persistence unit and
125: * defined priorities. </br>*Notice: It always creates a new
126: * EntityManagerFactory.
127: *
128: * @param puName -
129: * Persistence unit name
130: * @param priority -
131: * Defined priorities
132: * @return EntityManagerFactory
133: */
134: public static EntityManagerFactory getEntityManagerFactory(
135: String puName, Map priority) {
136: return initEntityManagerFactory(puName, priority);
137: }
138:
139: /**
140: * Create the EntityManager by peference in zk.xml
141: * <p>
142: * In WEB-INF/zk.xml, add following lines:
143: *
144: * <pre><code>
145: *<preference>
146: * <name>JPA.PersistenceUnitName</name>
147: * <value>PERSISTENCE_UNIT_NAME</value>
148: *</preference>
149: * </code></pre>
150: *
151: * </p>
152: * @return EntityManager
153: */
154: public static EntityManager getEntityManager() {
155: return initEntityManger(null, null);
156: }
157:
158: /**
159: * Create the EntityManager for the specified persistence unit name. </br>*Notice:If
160: * the EntityManagerFactory with specified presistence unit is not created
161: * before, a new one will be created.
162: *
163: * @param puName -
164: * Persistence unit name
165: * @return EntityManager
166: */
167: public static EntityManager getEntityManager(String puName) {
168: return initEntityManger(puName, null);
169: }
170:
171: /**
172: * Create the EntityManager for the specified persistence unit name and
173: * defined priorities. </br>*Notice: It always creates a new EntityManger
174: *
175: * @param puName -
176: * Persistence unit name
177: * @param priority -
178: * Defined priorities
179: * @return EntityManager
180: */
181: public static EntityManager getEntiyManager(String puName,
182: Map priority) {
183: return initEntityManger(puName, priority);
184: }
185:
186: /*
187: * If EntityManagerFactory with persistence name puName is not found in Map,
188: * created a new one and return Else, return the EntityManagerFactory
189: * directly.
190: */
191: /* package */static EntityManagerFactory initEntityManagerFactory(
192: String puName, Map priority) {
193: EntityManagerFactory emf;
194: if (priority == null) {
195: emf = (EntityManagerFactory) getEmfMap().get(puName);
196: if (emf == null) {
197: emf = createEntityManagerFactory(puName, null);
198: getEmfMap().put(puName, emf);
199: }
200: } else {
201: emf = createEntityManagerFactory(puName, priority);
202: getEmfMap().put(puName, emf);
203: }
204: return emf;
205: }
206:
207: /*
208: * If EntityManager with persistence name puName is not found in Map,
209: * created a new one and return Else, return the EntityManager directly.
210: */
211: /* package */static EntityManager initEntityManger(String puName,
212: Map priority) {
213: EntityManager em;
214: if (priority == null) {
215: em = (EntityManager) getEmMap().get(puName);
216: if (em == null) {
217: em = createEntityManager(puName, null);
218: getEmMap().put(puName, em);
219: }
220: } else {
221: em = createEntityManager(puName, priority);
222: getEmMap().put(puName, em);
223: }
224: return em;
225: }
226:
227: /*
228: * Clear up all EntityManagerFactory in map
229: */
230: /* package */static void cleanupAllEmf() {
231: if (!getEmfMap().isEmpty()) {
232:
233: for (Iterator itr = getEmfMap().values().iterator(); itr
234: .hasNext();) {
235: ((EntityManagerFactory) itr.next()).close();
236: }
237: cleanEmfMap();
238: }
239: }
240:
241: /*
242: * Util
243: */
244: /*
245: * Create the EntityManagerFactory by persistence unit name. If persistence
246: * unit name not given, using the one which is defined in zk.xml
247: */
248: private static EntityManagerFactory createEntityManagerFactory(
249: String puName, Map priority) {
250: puName = getPersistenceUnitName(puName);
251: EntityManagerFactory emf;
252: try {
253: if (priority == null) {
254: emf = Persistence.createEntityManagerFactory(puName);
255: log.info("EntityManagerFactory for: " + puName
256: + " is created ");
257: } else
258: emf = Persistence.createEntityManagerFactory(puName,
259: priority);
260: } catch (Exception ex) {
261: log.error("Initial EntityManagerFactory creation failed."
262: + ex);
263: throw new ExceptionInInitializerError(ex);
264: }
265: getEmfMap().put(puName, emf);
266: return emf;
267: }
268:
269: /*
270: * Create the EntityManager by persistence unit name. If persistence unit
271: * name not given, using the one which is defined in zk.xml
272: */
273: private static EntityManager createEntityManager(String puName,
274: Map priority) {
275: puName = getPersistenceUnitName(puName);
276: EntityManager em = createEntityManagerFactory(puName, priority)
277: .createEntityManager();
278: return em;
279: }
280:
281: private static WebApp getWebApp() {
282: WebApp app = null;
283: final Execution exec = Executions.getCurrent();
284: if (exec != null) {
285: final Desktop desktop = exec.getDesktop();
286: if (desktop != null) {
287: app = desktop.getWebApp();
288: }
289: }
290: return app;
291: }
292:
293: private static String getPersistenceUnitName(String pu) {
294: if (pu == null || pu.equals("")) {
295: /*
296: * Create EntityManagerFactory by preference in zk.xml
297: */
298: final org.zkoss.zk.ui.util.Configuration config = getWebApp()
299: .getConfiguration();
300: pu = config.getPreference(CONFIG, null);
301: }
302: return pu;
303: }
304:
305: }
|