1
2
3
4
5
6
7
8
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
101
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 }