Source Code Cross Referenced for HibernateSessionFactory.java in  » J2EE » Sofia » com » salmonllc » hibernate » 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 » J2EE » Sofia » com.salmonllc.hibernate 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.