001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: //Copyright (C) 1999 - 2004, Salmon LLC
004: //
005: //This program is free software; you can redistribute it and/or
006: //modify it under the terms of the GNU General Public License version 2
007: //as published by the Free Software Foundation;
008: //
009: //This program is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: //GNU General Public License for more details.
013: //
014: //You should have received a copy of the GNU General Public License
015: //along with this program; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: //For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.hibernate;
021:
022: import java.io.File;
023: import java.util.Hashtable;
024:
025: import javax.servlet.Servlet;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: import net.sf.hibernate.HibernateException;
030: import net.sf.hibernate.Session;
031: import net.sf.hibernate.SessionFactory;
032: import net.sf.hibernate.cfg.Configuration;
033:
034: import com.salmonllc.html.events.ServletServiceListener;
035: import com.salmonllc.jsp.JspServlet;
036: import com.salmonllc.properties.Props;
037: import com.salmonllc.util.ApplicationContext;
038:
039: /**
040: * A class that allows you to create cached versions of the hibernate session and session factory object
041: * This uses two properties in the SOFIA properties file to configure hibernate
042: *
043: * HibernateFactoryClass is the class name of a subclass of this factory. This is useful if you want
044: * to add your own HibernateFactory funcionality to SOFIA
045: * HibernateConfigFile is the directory and path name for the hibernate configuration file. If
046: * the property is not specified, SOFIA will look for /WEB-INF/properties/hibernate/Hibernate.cfg.xml under your
047: * web application to configure hibernate.
048: */
049: public class HibernateSessionFactory implements ServletServiceListener {
050: private static Hashtable _factories = new Hashtable();
051: private static ThreadLocal _session = new ThreadLocal();
052:
053: /**
054: * Returns the shared hibernate Session object for this page request.
055: */
056: public static Session getSession() throws HibernateException {
057: if (_session.get() == null)
058: _session.set(getSessionFactory().openSession());
059:
060: return (Session) _session.get();
061: }
062:
063: /**
064: * Returns the HibernateSessionFactory for this web application
065: */
066: public static SessionFactory getSessionFactory()
067: throws HibernateException {
068: ApplicationContext cont = ApplicationContext.getContext();
069: String appName = cont.getAppID();
070: SessionFactory fact = (SessionFactory) _factories.get(appName);
071: if (fact != null)
072: return fact;
073:
074: synchronized (_factories) {
075: //try again in case the factory has been created after sync
076: fact = (SessionFactory) _factories.get(appName);
077: if (fact != null)
078: return fact;
079:
080: Props p = Props.getProps(appName, null);
081: String clazz = p.getProperty(Props.HIBERNATE_FACTORY_CLASS);
082:
083: HibernateSessionFactory prov = null;
084: if (clazz != null) {
085: try {
086: Class c = Class.forName(clazz);
087: prov = (HibernateSessionFactory) c.newInstance();
088: } catch (Exception ex) {
089: throw new HibernateException(
090: "Error creating class:" + clazz, ex);
091: }
092: } else {
093: prov = new HibernateSessionFactory();
094: }
095:
096: fact = prov.configureFactory();
097: JspServlet.addServiceListener(prov);
098: _factories.put(appName, fact);
099:
100: return fact;
101: }
102: }
103:
104: /**
105: * Subclasses can override to provide custom Hibernate configuration logic
106: */
107: protected SessionFactory configureFactory()
108: throws HibernateException {
109: ApplicationContext cont = ApplicationContext.getContext();
110: Props p = Props.getProps(cont.getAppID(), null);
111: String configFile = p.getProperty(Props.HIBERNATE_CONFIG_FILE);
112: File f = null;
113: if (configFile == null) {
114: configFile = cont.getAppDocumentRoot() + File.separator
115: + "WEB-INF" + File.separator + "properties"
116: + File.separator + "Hibernate" + File.separator
117: + "hibernate.cfg.xml";
118: f = new File(configFile);
119: } else {
120: f = new File(configFile);
121: if (!f.exists() && configFile.startsWith(File.separator))
122: f = new File(cont.getAppDocumentRoot() + configFile);
123: }
124:
125: Configuration cfg = new Configuration();
126: cfg = cfg.configure(f);
127: return cfg.buildSessionFactory();
128: }
129:
130: public void serviceStarted(Servlet serv, HttpServletRequest req,
131: HttpServletResponse res) throws Exception {
132:
133: }
134:
135: public void serviceEnded(Servlet serv, HttpServletRequest req,
136: HttpServletResponse res) throws Exception {
137: if (_session.get() != null) {
138: Session s = ((Session) _session.get());
139: s.close();
140: _session.set(null);
141: }
142: }
143:
144: }
|