KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > hibernate > timers > TimersFactory


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.hibernate.timers;
23
24 import java.util.List JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26
27 import org.jboss.logging.Logger;
28 import org.hibernate.SessionFactory;
29 import org.hibernate.LockMode;
30 import org.hibernate.HibernateException;
31
32 /**
33  @author Scott.Stark@jboss.org
34  @version $Revision: 37406 $
35  */

36 public class TimersFactory
37 {
38    private static final Logger log = Logger.getLogger(Timers.class);
39
40    private final SessionFactory sessionFactory = getSessionFactory();
41
42    protected SessionFactory getSessionFactory()
43    {
44       try
45       {
46          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
47          SessionFactory factory = (SessionFactory) ctx.lookup("java:/hib-timers/SessionFactory");
48          ctx.close();
49          return factory;
50       }
51       catch (Exception JavaDoc e)
52       {
53          log.error("Could not locate SessionFactory in JNDI", e);
54          throw new IllegalStateException JavaDoc("Could not locate SessionFactory in JNDI");
55       }
56    }
57
58    public void persist(Timers transientInstance)
59    {
60       log.debug("persisting Timers instance");
61       try
62       {
63          sessionFactory.getCurrentSession().persist(transientInstance);
64          log.debug("persist successful");
65       }
66       catch (RuntimeException JavaDoc re)
67       {
68          log.error("persist failed", re);
69          throw re;
70       }
71    }
72
73    public void attachDirty(Timers instance)
74    {
75       log.debug("attaching dirty Timers instance");
76       try
77       {
78          sessionFactory.getCurrentSession().saveOrUpdate(instance);
79          log.debug("attach successful");
80       }
81       catch (RuntimeException JavaDoc re)
82       {
83          log.error("attach failed", re);
84          throw re;
85       }
86    }
87
88    public void attachClean(Timers instance)
89    {
90       log.debug("attaching clean Timers instance");
91       try
92       {
93          sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
94          log.debug("attach successful");
95       }
96       catch (RuntimeException JavaDoc re)
97       {
98          log.error("attach failed", re);
99          throw re;
100       }
101    }
102
103    public void delete(Timers persistentInstance)
104    {
105       log.debug("deleting Timers instance");
106       try
107       {
108          sessionFactory.getCurrentSession().delete(persistentInstance);
109          log.debug("delete successful");
110       }
111       catch (RuntimeException JavaDoc re)
112       {
113          log.error("delete failed", re);
114          throw re;
115       }
116    }
117
118    public Timers merge(Timers detachedInstance)
119    {
120       log.debug("merging Timers instance");
121       try
122       {
123          Timers result = (Timers) sessionFactory.getCurrentSession()
124             .merge(detachedInstance);
125          log.debug("merge successful");
126          return result;
127       }
128       catch (RuntimeException JavaDoc re)
129       {
130          log.error("merge failed", re);
131          throw re;
132       }
133    }
134
135    public List JavaDoc listUsers() throws HibernateException
136    {
137       return sessionFactory.getCurrentSession()
138             .createQuery("from Timers")
139             .list();
140    }
141
142    public Timers findById(TimersID id)
143    {
144       log.debug("getting Timers instance with id: " + id);
145       try
146       {
147          Timers instance = (Timers) sessionFactory.getCurrentSession()
148             .get("org.jboss.ejb.txtimer.data.Timers", id);
149          if (instance == null)
150          {
151             log.debug("get successful, no instance found");
152          }
153          else
154          {
155             log.debug("get successful, instance found");
156          }
157          return instance;
158       }
159       catch (RuntimeException JavaDoc re)
160       {
161          throw re;
162       }
163    }
164
165 }
Popular Tags