Source Code Cross Referenced for JpaUtil.java in  » Ajax » zk » org » zkoss » zkplus » jpa » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Ajax » zk » org.zkoss.zkplus.jpa 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:             *&lt;preference&gt;
096:             *	&lt;name&gt;JPA.PersistenceUnitName&lt;/name&gt;
097:             *	&lt;value&gt;PERSISTENCE_UNIT_NAME&lt;/value&gt;
098:             *&lt;/preference&gt;
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:             *&lt;preference&gt;
146:             *	&lt;name&gt;JPA.PersistenceUnitName&lt;/name&gt;
147:             *	&lt;value&gt;PERSISTENCE_UNIT_NAME&lt;/value&gt;
148:             *&lt;/preference&gt;
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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.