KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > hibernate > HibernateServiceImpl


1 package org.sapia.soto.hibernate;
2
3 import net.sf.hibernate.HibernateException;
4 import net.sf.hibernate.Session;
5 import net.sf.hibernate.SessionFactory;
6 import net.sf.hibernate.cfg.Configuration;
7
8 import org.sapia.soto.Service;
9 import org.sapia.soto.util.Param;
10
11 import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13
14
15 /**
16  * @author Yanick Duchesne
17  * <dl>
18  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
19  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
20  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
21  * </dl>
22  */

23 public class HibernateServiceImpl implements Service, HibernateService {
24   private Configuration _config = new Configuration();
25   private SessionFactory _sessions;
26   private List JavaDoc _props = new ArrayList JavaDoc();
27
28   public HibernateServiceImpl() {
29     super();
30   }
31
32   /**
33    * Configuration method that adds a class to the Hibernate configuration
34    * that is internally used.
35    * <p>
36    * Corresponds to the following Hibernate snippet:
37    *
38    * <pre>
39    * Configuration cfg = new Configuration()
40    * .addClass(eg.Vertex.class)
41    * .addClass(eg.Edge.class);
42    * </pre>
43    *
44    * @param className the name of the class to add.
45    * @throws Exception
46    */

47   public void addClass(String JavaDoc className) throws Exception JavaDoc {
48     _config.addClass(Class.forName(className));
49   }
50
51   /**
52    * Configuration method that adds a property to the Hibernate configuration
53    * that is internally used.
54    *
55    * <pre>
56    * Configuration cfg = new Configuration();
57    * cfg.setProperty("name", "value");
58    * </pre>
59    *
60    * @param className the name of the class to add.
61    * @throws Exception
62    */

63   public Param createProperty() {
64     Param param = new Param();
65     _props.add(param);
66
67     return param;
68   }
69
70   /**
71    * @see org.sapia.soto.Service#init()
72    */

73   public void init() throws Exception JavaDoc {
74   }
75
76   /**
77    * @see org.sapia.soto.Service#start()
78    */

79   public void start() throws Exception JavaDoc {
80     _sessions = _config.buildSessionFactory();
81   }
82
83   /**
84    * @see org.sapia.soto.Service#dispose()
85    */

86   public void dispose() {
87     try {
88       _sessions.close();
89     } catch (HibernateException e) {
90       // noop;
91
}
92   }
93
94   /**
95    * @see org.sapia.soto.hibernate.HibernateService#currentSession()
96    */

97   public Session currentSession() throws HibernateException {
98     return SessionState.currentSession(_sessions);
99   }
100
101   /**
102    * @see org.sapia.soto.hibernate.HibernateService#unregister()
103    */

104   public void unregister() {
105     SessionState.unregister();
106   }
107
108   private Configuration getConfiguration() {
109     Param p;
110
111     for (int i = 0; i < _props.size(); i++) {
112       p = (Param) _props.get(i);
113
114       if ((p.getName() != null) && (p.getValue() != null)) {
115         _config.setProperty(p.getName(), p.getValue().toString());
116       }
117     }
118
119     return _config;
120   }
121 }
122
Popular Tags