01: /* HibernateSessionFactoryListener.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Tue Sep 5 10:11:55 2006, Created by henrichen
10: }}IS_NOTE
11:
12: Copyright (C) 2006 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: This program is distributed under GPL Version 2.0 in the hope that
16: it will be useful, but WITHOUT ANY WARRANTY.
17: }}IS_RIGHT
18: */
19: package org.zkoss.zkplus.hibernate;
20:
21: import javax.servlet.ServletContext;
22: import javax.servlet.ServletContextEvent;
23: import javax.servlet.ServletContextListener;
24:
25: import org.zkoss.zk.ui.WebApp;
26: import org.zkoss.zk.ui.UiException;
27: import org.zkoss.zk.ui.util.WebAppInit;
28: import org.zkoss.zk.ui.util.WebAppCleanup;
29:
30: import org.hibernate.SessionFactory;
31:
32: /**
33: * <p>Listener to init and cleanup the hibernate session factory automatically.
34: * This listener is used with {@link OpenSessionInViewListener} and
35: * {@link HibernateUtil}, or it will not work.</p>
36: *
37: * <p>In WEB-INF/zk.xml, add following lines:
38: * <pre><code>
39: * <listener>
40: * <description>Hibernate SessionFactory Lifecycle</description>
41: * <listener-class>org.zkoss.zkplus.hibernate.HibernateSessionFactoryListener</listener-class>
42: * </listener>
43: * </code></pre>
44: * <p>Since ZK 3.0.1, if your hibernate configuration file name is not the default "hibernate.cfg.xml", you can
45: * specify it in WEB-INF/zk.xml. Just add following lines:</p>
46: * <pre><code>
47: * <preference>
48: * <name>HibernateUtil.config</name>
49: * <value>YOUR-HIBERNATE-CONFIG-FILENAME</value>
50: * </code></pre>
51: * </p>
52: * <p>Sometimes, when using ZK with other backend framework, the Hibernate's SessionFactory
53: * might has to be created earlier. Therefore, since ZK 3.0.1, we have implemented this
54: * listener to be also a {@link javax.servlet.ServletContextListener} that you can
55: * choose to specify it in web.xml instead and make it initialized right when your applcation
56: * is deployed.</p>
57: * <p>In WEB-INF/web.xml, add following lines:
58: * <pre><code>
59: * <context-param>
60: * <param-name>HibernateUtil.config</param-name>
61: * <param-value>YOUR-HIBERNATE-CONFIG-FILENAME</param-value>
62: * </context-param>
63: * <listener>
64: * <listener-class>org.zkoss.zkplus.hibernate.HibernateSessionFactoryListener</listener-class>
65: * </listener>
66: * </code></pre>
67: *
68: * @author henrichen
69: */
70: public class HibernateSessionFactoryListener implements WebAppInit,
71: WebAppCleanup, ServletContextListener {
72: //WebAppInit//
73: public void init(WebApp app) {
74: HibernateUtil.initSessionFactory(app);
75: }
76:
77: //WebAppCleanup//
78: public void cleanup(WebApp wapp) {
79: HibernateUtil.cleanupSessionFactory();
80: }
81:
82: //ServletContextListener//
83: /**
84: *@since 3.0.1
85: */
86: public void contextDestroyed(ServletContextEvent sce) {
87: HibernateUtil.cleanupSessionFactory();
88: }
89:
90: /**
91: *@since 3.0.1
92: */
93: public void contextInitialized(ServletContextEvent sce) {
94: final ServletContext ctx = sce.getServletContext();
95: String resource = ctx.getInitParameter(HibernateUtil.CONFIG);
96: HibernateUtil.initSessionFactory(resource);
97: }
98: }
|