View Javadoc

1   /*
2    * HibernatePersistenceFactory.java
3    *
4    * Created on June 15, 2005, 4:42 PM
5    *
6    * To change this template, choose Tools | Options and locate the template under
7    * the Source Creation and Management node. Right-click the template and choose
8    * Open. You can then make changes to the template in the Source Editor.
9    */
10  
11  package gov.bnl.gums.hibernate;
12  
13  import java.util.Enumeration;
14  import java.util.MissingResourceException;
15  import java.util.Properties;
16  import java.util.PropertyResourceBundle;
17  import java.util.ResourceBundle;
18  import net.sf.hibernate.*;
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import gov.bnl.gums.*;
22  
23  /***
24   *
25   * @author carcassi
26   */
27  public class HibernatePersistenceFactory implements PersistenceFactory {
28      private Log log = LogFactory.getLog(HibernatePersistenceFactory.class);
29      
30      private String name;
31      private Properties properties;
32      private SessionFactory sessions;
33      
34      public HibernatePersistenceFactory() {
35          log.trace("HibernatePersistenceFactory instanciated");
36      }
37  
38      public UserGroupDB retrieveUserGroupDB(String name) {
39          return new HibernateUserGroupDB(this, name);
40      }
41  
42      public ManualUserGroupDB retrieveManualUserGroupDB(String name) {
43          return new HibernateUserGroupDB(this, name);
44      }
45  
46      public ManualAccountMapperDB retrieveManualAccountMapperDB(String name) {
47          return new HibernateMapping(this, name);
48      }
49  
50      public AccountPoolMapperDB retrieveAccountPoolMapperDB(String name) {
51          return new HibernateMapping(this, name);
52      }
53      
54      public String getName() {
55          return name;
56      }
57      
58      public void setName(String name) {
59          this.name = name;
60      }
61  
62      public Properties getProperties() {
63          return this.properties;
64      }
65  
66      public void setProperties(Properties properties) {
67          this.properties = properties;
68      }
69      
70      public void setConnectionFromHibernateProperties() {
71          try {
72              setProperties(readHibernateProperties());
73          } catch (MissingResourceException e) {
74              throw new RuntimeException("Couldn't find database configuration file (hibernate.properties)", e);
75          }
76      }
77      
78      private Properties readHibernateProperties() {
79          log.trace("Retrieving hibernate properties from hibernate.properties in the classpath");
80          PropertyResourceBundle prop = (PropertyResourceBundle) ResourceBundle.getBundle("hibernate");
81          Properties prop2 = new Properties();
82          Enumeration keys = prop.getKeys();
83          while (keys.hasMoreElements()) {
84              String key = (String) keys.nextElement();
85              prop2.setProperty(key, prop.getString(key));
86          }
87          return prop2;
88      }
89      
90      synchronized SessionFactory retrieveSessionFactory() {
91          if (sessions != null) return sessions;
92          sessions = buildSessionFactory();
93          return sessions;
94      }
95      
96      private SessionFactory buildSessionFactory() {
97          try {
98              log.trace("Creating Hibernate Session Factory with the following properties: " + getProperties());
99              net.sf.hibernate.cfg.Configuration cfg = new net.sf.hibernate.cfg.Configuration()
100             // Properties for the hibernate session are taken from the properties specified either in the
101             // gums.config file, or set programmatically to the class (when unit testing)
102                 .setProperties(getProperties())
103                 .addClass(gov.bnl.gums.hibernate.Mapping.class)
104                 .addClass(gov.bnl.gums.hibernate.User.class);
105             return cfg.buildSessionFactory();
106         } catch (Exception e) {
107             log.error("Couldn't initialize Hibernate", e);
108             throw new RuntimeException("An error occurred while initializing the database environment (hibernate): "+ e.getMessage(), e);
109         }
110     }
111     
112 }