KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > impl > hibernate > SessionHandler


1 //
2
// Informa -- RSS Library for Java
3
// Copyright (c) 2002, 2003 by Niko Schmuck
4
//
5
// Niko Schmuck
6
// http://sourceforge.net/projects/informa
7
// mailto:niko_schmuck@users.sourceforge.net
8
//
9
// This library is free software.
10
//
11
// You may redistribute it and/or modify it under the terms of the GNU
12
// Lesser General Public License as published by the Free Software Foundation.
13
//
14
// Version 2.1 of the license should be included with this distribution in
15
// the file LICENSE. If the license is not included with this distribution,
16
// you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
17
// or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
18
// MA 02139 USA.
19
//
20
// This library is distributed in the hope that it will be useful,
21
// but WITHOUT ANY WARRANTY; without even the implied waranty of
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
// Lesser General Public License for more details.
24
//
25

26 // $Id: SessionHandler.java,v 1.15 2004/05/21 11:26:54 niko_schmuck Exp $
27

28 package de.nava.informa.impl.hibernate;
29
30 import java.sql.Connection JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import net.sf.hibernate.HibernateException;
34 import net.sf.hibernate.Session;
35 import net.sf.hibernate.SessionFactory;
36 import net.sf.hibernate.cfg.Configuration;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41 /**
42  * Singleton class from which hibernate sesssions may be retrieved needed
43  * for transactions.
44  *
45  * @author Niko Schmuck
46  */

47 public class SessionHandler {
48
49   private static Log logger = LogFactory.getLog(SessionHandler.class);
50
51   private static SessionHandler myInstance;
52
53   private Configuration cfg;
54   private SessionFactory sessFactory;
55   private Connection JavaDoc conn;
56   private Session curSession;
57
58   /**
59    * Constructor which configures hibernate, in this order:
60    * <ol>
61    * <li>Reads hibernate.cfg.xml or hibernate.properties file from the
62    * CLASSPATH to retrieve information about how the database can be
63    * accessed (JDBC connection properties).</li>
64    * <li>Then reads in the definiton files for all related informa hibernate
65    * classes (*.hbm.xml)</li>
66    * <li>Finally, if supplied, applies a Properties object to do a final
67    * override.</li>
68    * </ol>
69    *
70    * @throws HibernateException In case a problem occurred while configuring
71    * hibernate or creating the session factory.
72    */

73   private SessionHandler(Properties JavaDoc props) throws HibernateException {
74     // reads in hibernate.properties implictly for database connection settings
75
cfg = new Configuration();
76
77     // attempt to use standard config file named hibernate.cfg.xml
78
try {
79       cfg.configure();
80     } catch (HibernateException he) {
81       logger.info("Can't find \"hibernate.cfg.xml\" in classpath.");
82     }
83
84     // add base classes
85
cfg
86       .addClass(Channel.class)
87       .addClass(Item.class)
88       .addClass(ItemGuid.class)
89       .addClass(ItemEnclosure.class)
90       .addClass(ItemSource.class)
91       .addClass(Cloud.class)
92       .addClass(Category.class)
93       .addClass(ChannelGroup.class)
94       .addClass(ChannelSubscription.class)
95       .addClass(Image.class)
96       .addClass(ItemMetadata.class)
97       .addClass(TextInput.class);
98
99     // If Properties were supplied then use them as the final override
100
if (props != null)
101       cfg.addProperties(props);
102
103     // get session factory (expensive)
104
sessFactory = cfg.buildSessionFactory();
105   }
106
107   /**
108    * Returns the one and only instance which can be used to obtain a
109    * {@link Session} for further operation with the hibernate objects.
110    *
111    * @throws HibernateException In case a problem occurred while configuring
112    * hibernate or creating the session factory.
113    */

114   public static SessionHandler getInstance(Properties JavaDoc props) throws HibernateException {
115     if (myInstance == null) {
116       myInstance = new SessionHandler(props);
117     }
118     return myInstance;
119   }
120
121   /**
122    * Returns the singelton instance, calling
123    * {@link #getInstance(Properties)} with properties set to null.
124    */

125   public static SessionHandler getInstance() throws HibernateException {
126     return getInstance(null);
127   }
128
129   /**
130    * Gets hibernate session object, if JDBC <code>Connection</code> was
131    * set earlier this will be used for the opening a hibernate session.
132    */

133   public Session getSession() throws HibernateException {
134     if (conn != null)
135       curSession = sessFactory.openSession(conn);
136     else
137       curSession = sessFactory.openSession();
138     return curSession;
139   }
140
141   /**
142    * Gets a a new session whilst using an existing JDBC connection.
143    *
144    * @param conn JDBC connection
145    */

146   public Session getSession(Connection JavaDoc conn) {
147     this.conn = conn;
148     curSession = sessFactory.openSession(conn);
149     return curSession;
150   }
151
152   /**
153    * Gets the default JDBC Connection object.
154    */

155   public Connection JavaDoc getConnection() {
156     return conn;
157   }
158
159   /**
160    * Sets the default JDBC Connection object.
161    */

162   public void setConnection(Connection JavaDoc connection) {
163     conn = connection;
164   }
165
166   /**
167    * Returns true if session is open.
168    */

169   public boolean isSessionOpen()
170   {
171     return curSession.isOpen();
172   }
173
174 }
175
Popular Tags