01: /*
02: * Timer: The timer class
03: * Copyright (C) 2006-2007 Rift IT Contracting
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library; if not, write to the Free Software
17: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18: *
19: * HibernateUtil.java
20: */
21:
22: package com.rift.coad.daemon.timer.db.util;
23:
24: //coadunation imports
25: import com.rift.coad.daemon.timer.TimerException;
26: import com.rift.coad.lib.configuration.*;
27:
28: // hibernate imports
29: import org.hibernate.*;
30: import org.hibernate.cfg.*;
31:
32: /**
33: * This class sets up a hibernate SessionFactory.
34: */
35: public class HibernateUtil {
36:
37: private static SessionFactory sessionFactory;
38:
39: /**
40: * Configures up hibernate programmatically using Coadunations configuration
41: * file.
42: */
43: public static void init() throws TimerException {
44: try {
45: // Create the SessionFactory from hibernate.cfg.xml
46: com.rift.coad.lib.configuration.Configuration coadConfig = com.rift.coad.lib.configuration.ConfigurationFactory
47: .getInstance().getConfig(
48: com.rift.coad.daemon.timer.TimerImpl.class);
49: org.hibernate.cfg.Configuration config = new org.hibernate.cfg.Configuration()
50: .addResource(
51: "com/rift/coad/daemon/timer/db/Schedule.hbm.xml")
52: .setProperty("hibernate.dialect",
53: coadConfig.getString("db_dialect"))
54: .setProperty("hibernate.connection.datasource",
55: coadConfig.getString("db_datasource"))
56: .setProperty(
57: "hibernate.current_session_context_class",
58: "jta")
59: .setProperty("hibernate.transaction.factory_class",
60: "org.hibernate.transaction.JTATransactionFactory")
61: .setProperty(
62: "hibernate.transaction.manager_lookup_class",
63: "org.hibernate.transaction.JOTMTransactionManagerLookup")
64: .setProperty("hibernate.cache.provider_class",
65: "org.hibernate.cache.NoCacheProvider")
66: .setProperty(
67: "hibernate.show_sql",
68: coadConfig.getString("hibernate_sql",
69: "false"))
70: .setProperty(
71: "hibernate.hbm2ddl.auto",
72: coadConfig.getString(
73: "hibernate_hbm2ddl_auto", "update"));
74: sessionFactory = config.buildSessionFactory();
75: } catch (Throwable ex) {
76: // Make sure you log the exception, as it might be swallowed
77: throw new TimerException(
78: "Initial SessionFactory creation failed: " + ex);
79: }
80: }
81:
82: /**
83: * @return Returns the Hibernate SessionFactory.
84: */
85: public static SessionFactory getSessionFactory() {
86: return sessionFactory;
87: }
88:
89: }
|