001: //
002: // Informa -- RSS Library for Java
003: // Copyright (c) 2002, 2003 by Niko Schmuck
004: //
005: // Niko Schmuck
006: // http://sourceforge.net/projects/informa
007: // mailto:niko_schmuck@users.sourceforge.net
008: //
009: // This library is free software.
010: //
011: // You may redistribute it and/or modify it under the terms of the GNU
012: // Lesser General Public License as published by the Free Software Foundation.
013: //
014: // Version 2.1 of the license should be included with this distribution in
015: // the file LICENSE. If the license is not included with this distribution,
016: // you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
017: // or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
018: // MA 02139 USA.
019: //
020: // This library is distributed in the hope that it will be useful,
021: // but WITHOUT ANY WARRANTY; without even the implied waranty of
022: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
023: // Lesser General Public License for more details.
024: //
025:
026: // $Id: SessionHandler.java,v 1.17 2005/09/24 22:35:15 niko_schmuck Exp $
027:
028: package de.nava.informa.impl.hibernate;
029:
030: import java.sql.Connection;
031: import java.util.Properties;
032:
033: import org.hibernate.HibernateException;
034: import org.hibernate.Session;
035: import org.hibernate.SessionFactory;
036: import org.hibernate.cfg.Configuration;
037:
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040:
041: /**
042: * Singleton class from which hibernate sesssions may be retrieved needed
043: * for transactions.
044: *
045: * @author Niko Schmuck
046: */
047: public class SessionHandler {
048:
049: private static Log logger = LogFactory.getLog(SessionHandler.class);
050:
051: private static SessionHandler myInstance;
052:
053: private Configuration cfg;
054: private SessionFactory sessFactory;
055: private Connection conn;
056: private Session curSession;
057:
058: /**
059: * Constructor which configures hibernate, in this order:
060: * <ol>
061: * <li>Reads hibernate.cfg.xml or hibernate.properties file from the
062: * CLASSPATH to retrieve information about how the database can be
063: * accessed (JDBC connection properties).</li>
064: * <li>Then reads in the definiton files for all related informa hibernate
065: * classes (*.hbm.xml)</li>
066: * <li>Finally, if supplied, applies a Properties object to do a final
067: * override.</li>
068: * </ol>
069: *
070: * @throws HibernateException In case a problem occurred while configuring
071: * hibernate or creating the session factory.
072: */
073: private SessionHandler(Properties props) throws HibernateException {
074: // reads in hibernate.properties implictly for database connection settings
075: cfg = new Configuration();
076:
077: // attempt to use standard config file named hibernate.cfg.xml
078: try {
079: cfg.configure();
080: } catch (HibernateException he) {
081: logger
082: .info("Can't find \"hibernate.cfg.xml\" in classpath.");
083: }
084:
085: // add base classes
086: cfg.addClass(Channel.class).addClass(Item.class).addClass(
087: ItemGuid.class).addClass(ItemEnclosure.class).addClass(
088: ItemSource.class).addClass(Cloud.class).addClass(
089: Category.class).addClass(ChannelGroup.class).addClass(
090: ChannelSubscription.class).addClass(Image.class)
091: .addClass(ItemMetadata.class).addClass(TextInput.class);
092:
093: // If Properties were supplied then use them as the final override
094: if (props != null)
095: cfg.addProperties(props);
096:
097: // get session factory (expensive)
098: sessFactory = cfg.buildSessionFactory();
099: }
100:
101: /**
102: * Returns the one and only instance which can be used to obtain a
103: * {@link Session} for further operation with the hibernate objects.
104: *
105: * @throws HibernateException In case a problem occurred while configuring
106: * hibernate or creating the session factory.
107: */
108: public static synchronized SessionHandler getInstance(
109: Properties props) throws HibernateException {
110:
111: if (myInstance == null)
112: myInstance = new SessionHandler(props);
113: return myInstance;
114: }
115:
116: /**
117: * Returns the singelton instance, calling
118: * {@link #getInstance(Properties)} with properties set to null.
119: */
120: public static SessionHandler getInstance()
121: throws HibernateException {
122: return getInstance(null);
123: }
124:
125: /**
126: * Gets hibernate session object, if JDBC <code>Connection</code> was
127: * set earlier this will be used for the opening a hibernate session.
128: */
129: public Session getSession() throws HibernateException {
130: if (conn != null)
131: curSession = sessFactory.openSession(conn);
132: else
133: curSession = sessFactory.openSession();
134: return curSession;
135: }
136:
137: /**
138: * Gets a a new session whilst using an existing JDBC connection.
139: *
140: * @param conn JDBC connection
141: */
142: public Session getSession(Connection conn) {
143: this .conn = conn;
144: curSession = sessFactory.openSession(conn);
145: return curSession;
146: }
147:
148: /**
149: * Gets the default JDBC Connection object.
150: */
151: public Connection getConnection() {
152: return conn;
153: }
154:
155: /**
156: * Sets the default JDBC Connection object.
157: */
158: public void setConnection(Connection connection) {
159: conn = connection;
160: }
161:
162: /**
163: * Returns true if session is open.
164: */
165: public boolean isSessionOpen() {
166: return curSession.isOpen();
167: }
168:
169: }
|