001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.hibernate.timers;
023:
024: import java.util.List;
025: import javax.naming.InitialContext;
026:
027: import org.jboss.logging.Logger;
028: import org.hibernate.SessionFactory;
029: import org.hibernate.LockMode;
030: import org.hibernate.HibernateException;
031:
032: /**
033: @author Scott.Stark@jboss.org
034: @version $Revision: 57211 $
035: */
036: public class TimersFactory {
037: private static final Logger log = Logger.getLogger(Timers.class);
038:
039: private final SessionFactory sessionFactory = getSessionFactory();
040:
041: protected SessionFactory getSessionFactory() {
042: try {
043: InitialContext ctx = new InitialContext();
044: SessionFactory factory = (SessionFactory) ctx
045: .lookup("java:/hib-timers/SessionFactory");
046: ctx.close();
047: return factory;
048: } catch (Exception e) {
049: log.error("Could not locate SessionFactory in JNDI", e);
050: throw new IllegalStateException(
051: "Could not locate SessionFactory in JNDI");
052: }
053: }
054:
055: public void persist(Timers transientInstance) {
056: log.debug("persisting Timers instance");
057: try {
058: sessionFactory.getCurrentSession().persist(
059: transientInstance);
060: log.debug("persist successful");
061: } catch (RuntimeException re) {
062: log.error("persist failed", re);
063: throw re;
064: }
065: }
066:
067: public void attachDirty(Timers instance) {
068: log.debug("attaching dirty Timers instance");
069: try {
070: sessionFactory.getCurrentSession().saveOrUpdate(instance);
071: log.debug("attach successful");
072: } catch (RuntimeException re) {
073: log.error("attach failed", re);
074: throw re;
075: }
076: }
077:
078: public void attachClean(Timers instance) {
079: log.debug("attaching clean Timers instance");
080: try {
081: sessionFactory.getCurrentSession().lock(instance,
082: LockMode.NONE);
083: log.debug("attach successful");
084: } catch (RuntimeException re) {
085: log.error("attach failed", re);
086: throw re;
087: }
088: }
089:
090: public void delete(Timers persistentInstance) {
091: log.debug("deleting Timers instance");
092: try {
093: sessionFactory.getCurrentSession().delete(
094: persistentInstance);
095: log.debug("delete successful");
096: } catch (RuntimeException re) {
097: log.error("delete failed", re);
098: throw re;
099: }
100: }
101:
102: public Timers merge(Timers detachedInstance) {
103: log.debug("merging Timers instance");
104: try {
105: Timers result = (Timers) sessionFactory.getCurrentSession()
106: .merge(detachedInstance);
107: log.debug("merge successful");
108: return result;
109: } catch (RuntimeException re) {
110: log.error("merge failed", re);
111: throw re;
112: }
113: }
114:
115: public List listUsers() throws HibernateException {
116: return sessionFactory.getCurrentSession().createQuery(
117: "from Timers").list();
118: }
119:
120: public Timers findById(TimersID id) {
121: log.debug("getting Timers instance with id: " + id);
122: try {
123: Timers instance = (Timers) sessionFactory
124: .getCurrentSession().get(
125: "org.jboss.ejb.txtimer.data.Timers", id);
126: if (instance == null) {
127: log.debug("get successful, no instance found");
128: } else {
129: log.debug("get successful, instance found");
130: }
131: return instance;
132: } catch (RuntimeException re) {
133: throw re;
134: }
135: }
136:
137: }
|