001: /* HibernateUtil.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Mon Sep 4 17:18:21 2006, Created by henrichen
010: }}IS_NOTE
011:
012: Copyright (C) 2006 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: }}IS_RIGHT
016: */
017: package org.zkoss.zkplus.hibernate;
018:
019: import org.zkoss.zk.ui.WebApp;
020: import org.zkoss.zk.ui.Desktop;
021: import org.zkoss.zk.ui.Execution;
022: import org.zkoss.zk.ui.Executions;
023: import org.zkoss.util.logging.Log;
024: import org.zkoss.lang.JVMs;
025:
026: import org.hibernate.Session;
027: import org.hibernate.SessionFactory;
028: import org.hibernate.ConnectionReleaseMode;
029: import org.hibernate.HibernateException;
030: import org.hibernate.cfg.Configuration;
031: import org.hibernate.cfg.AnnotationConfiguration;
032:
033: /**
034: * <p>Utitlity to access Hibernate Session. This implemenation works with the Hibernate's
035: * thread session context (version 3.1+). That is, you have to specified
036: * hibernate's configuration file "hibernate.cfg.xml" to as follows:</p>
037: *
038: * <pre><code>
039: * <session-factory>
040: * ...
041: * <property name="current_session_context_class">thread</property>
042: * </session-factory>
043: * </code><pre>
044: *
045: * <p>Since ZK 3.0.1, if your hibernate configuration file name is not the default "hibernate.cfg.xml", you can
046: * specify it in WEB-INF/zk.xml. Just add following lines:
047: * <pre><code>
048: * <preference>
049: * <name>HibernateUtil.config</name>
050: * <value>YOUR-HIBERNATE-FILENAME</value>
051: * </code></pre>
052: * </p>
053:
054: * <p> Also notice that the zkplus.jar must be put under application's WEB-INF/lib because
055: * the SessionFactory is stored as a class static member.
056: *
057: * @author henrichen
058: */
059: public class HibernateUtil {
060: public static String CONFIG = "HibernateUtil.config";
061: private static final Log log = Log.lookup(HibernateUtil.class);
062:
063: private static SessionFactory _factory;
064:
065: /**
066: * Get the singleton hibernate Session Factory.
067: */
068: public static SessionFactory getSessionFactory() {
069: return initSessionFactory((WebApp) null);
070: }
071:
072: /**
073: * Wrapping HibernateUtil.getSessionFactory().getCurrentSession() into a simple API.
074: */
075: public static Session currentSession() throws HibernateException {
076: return getSessionFactory().getCurrentSession();
077: }
078:
079: /**
080: * Wrapping HibernateUtil.getSessionFactory().getCurrentSession().close() into a simple API.
081: */
082: public static void closeSession() throws HibernateException {
083: currentSession().close();
084: }
085:
086: /**
087: * Used in {@link HibernateSessionFactoryListener} to init
088: * Hibernate SessionFactory.
089: * @deprecated Use {@link #initSessionFactory(WebApp app)} instead.
090: */
091: /* package */static SessionFactory initSessionFactory() {
092: return initSessionFactory((WebApp) null);
093: }
094:
095: /**
096: * Used in {@link HibernateSessionFactoryListener} to init
097: * Hibernate SessionFactory.
098: * @param app web applicaton, given null will try to get it from current Execution.
099: * @since 3.0.1
100: */
101: /* package */static SessionFactory initSessionFactory(WebApp app) {
102: if (_factory == null) {
103: //read hibernate.config preference
104: if (app == null) {
105: final Execution exec = Executions.getCurrent();
106: if (exec != null) {
107: final Desktop desktop = exec.getDesktop();
108: if (desktop != null) {
109: app = desktop.getWebApp();
110: }
111: }
112: }
113: String resource = null;
114: if (app != null) {
115: final org.zkoss.zk.ui.util.Configuration config = app
116: .getConfiguration();
117: resource = config.getPreference(CONFIG, null);
118: }
119: return initSessionFactory(resource);
120: }
121: return _factory;
122: }
123:
124: /*package*/static SessionFactory initSessionFactory(String resource) {
125: if (_factory == null) {
126: try {
127: // Create the SessionFactory per JavaVM version and allow JDK 1.4 compatibility
128: if (JVMs.isJava5()) {
129: _factory = java5Factory(resource);
130: } else {
131: _factory = java4Factory(resource);
132: }
133: log.info("Hibernate configuration file loaded: "
134: + (resource == null ? "hibernate.cfg.xml"
135: : resource));
136: } catch (Throwable ex) {
137: // Make sure you log the exception, as it might be swallowed
138: log.error("Initial SessionFactory creation failed."
139: + ex);
140: throw new ExceptionInInitializerError(ex);
141: }
142: }
143: return _factory;
144: }
145:
146: //We have to put the instantiation of AnnotationConfiguration
147: //in a separate method. Otherwise, it will be loaded even if
148: //isJava5 is false
149: /*private*/static SessionFactory java5Factory(String resource) {
150: return resource == null ? new AnnotationConfiguration()
151: .configure().buildSessionFactory()
152: : new AnnotationConfiguration().configure(resource)
153: .buildSessionFactory();
154: }
155:
156: /*private */static SessionFactory java4Factory(String resource) {
157: return resource == null ? new Configuration().configure()
158: .buildSessionFactory() : new Configuration().configure(
159: resource).buildSessionFactory();
160: }
161:
162: /**
163: * Used in {@link HibernateSessionFactoryListener} to init
164: * Hibernate SessionFactory.
165: */
166: /* package */static void cleanupSessionFactory() {
167: if (_factory != null) {
168: _factory.close(); // Free all resources
169: _factory = null;
170: }
171: }
172: }
|