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.persistence;
12  
13  import gov.bnl.gums.configuration.Configuration;
14  import gov.bnl.gums.db.AccountPoolMapperDB;
15  import gov.bnl.gums.db.HibernateAccountMapperDB;
16  import gov.bnl.gums.db.HibernateUserGroupDB;
17  import gov.bnl.gums.db.ManualAccountMapperDB;
18  import gov.bnl.gums.db.ManualUserGroupDB;
19  import gov.bnl.gums.db.UserGroupDB;
20  
21  import java.util.Enumeration;
22  import java.util.Iterator;
23  import java.util.MissingResourceException;
24  import java.util.Properties;
25  import java.util.PropertyResourceBundle;
26  import java.util.ResourceBundle;
27  
28  import net.sf.hibernate.*;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /**
34   *
35   * @author Gabriele Carcassi, Jay Packard
36   */
37  public class HibernatePersistenceFactory extends PersistenceFactory {
38      static public String getTypeStatic() {
39  		return "hibernate";
40  	}
41  	
42  	private Log log = LogFactory.getLog(HibernatePersistenceFactory.class);
43      public SessionFactory sessions;
44      
45  	/**
46       * Create a new hibernate persistence factory.  This empty constructor is needed by the XML Digester.
47  	 */
48  	public HibernatePersistenceFactory() {
49      	log.trace("HibernatePersistenceFactory instanciated");
50      }
51      
52      /**
53       * Create a new hibernate persistence factory with a configuration.
54       * 
55       * @param configuration
56       */
57      public HibernatePersistenceFactory(Configuration configuration) {
58      	super(configuration);
59      	log.trace("HibernatePersistenceFactory instanciated");
60      }    
61  
62      /**
63       * Create a new ldap persistence factory with a configuration and a name.
64       * 
65       * @param configuration
66       * @param name
67       */
68      public HibernatePersistenceFactory(Configuration configuration, String name) {
69      	super(configuration, name);
70      	log.trace("HibernatePersistenceFactory instanciated");
71      }
72      
73      public PersistenceFactory clone(Configuration configuration) {
74      	HibernatePersistenceFactory persistenceFactory = new HibernatePersistenceFactory(configuration, new String(getName()));
75      	persistenceFactory.setDescription(new String(getDescription()));
76      	persistenceFactory.setProperties((Properties)getProperties().clone());
77      	return persistenceFactory;
78      }
79      
80      public void finalize() {
81      	try {
82      		if (sessions!=null)
83      			sessions.close();
84  		} catch (HibernateException e) {
85  			log.error("Couldn't close hibernate sessions: " + e.getMessage());
86  		}
87      }
88      
89      public String getType() {
90  		return "hibernate";
91  	}
92      
93      public AccountPoolMapperDB retrieveAccountPoolMapperDB(String name) {
94          return new HibernateAccountMapperDB(this, name);
95      }
96  
97      public ManualAccountMapperDB retrieveManualAccountMapperDB(String name) {
98          return new HibernateAccountMapperDB(this, name);
99      }
100 
101     public ManualUserGroupDB retrieveManualUserGroupDB(String name) {
102         return new HibernateUserGroupDB(this, name);
103     }
104     
105     public synchronized SessionFactory retrieveSessionFactory() {
106         if (sessions != null) return sessions;
107         sessions = buildSessionFactory();
108         return sessions;
109     }
110     
111     public UserGroupDB retrieveUserGroupDB(String name) {
112         return new HibernateUserGroupDB(this, name);
113     }
114     
115     public void setConnectionFromHibernateProperties() {
116         try {
117             setProperties(readHibernateProperties());
118         } catch (MissingResourceException e) {
119             throw new RuntimeException("Couldn't find database configuration file (hibernate.properties)", e);
120         }
121     }
122     
123 	public String toXML() {
124     	String retStr = "\t\t<hibernatePersistenceFactory\n"+
125     		"\t\t\tname='"+getName()+"'\n"+
126     		"\t\t\tdescription='"+getDescription()+"'\n";
127     	
128     	Iterator keyIt = getProperties().keySet().iterator();
129     	while(keyIt.hasNext()) {
130     		String key = (String)keyIt.next();
131     		retStr += "\t\t\t"+key+"='"+getProperties().getProperty(key)+"'\n";
132     	}
133 
134     	if (retStr.charAt(retStr.length()-1)=='\n')
135     		retStr = retStr.substring(0, retStr.length()-1);    	
136     	
137     	retStr += "/>\n\n";
138     	
139     	return retStr;
140 	}
141     
142     private SessionFactory buildSessionFactory() {
143         try {
144             log.trace("Creating Hibernate Session Factory with the following properties: " + getProperties());
145             // Properties for the hibernate session are taken from the properties specified either in the
146             // gums.config file, or set programmatically to the class (when unit testing)
147             net.sf.hibernate.cfg.Configuration cfg = new net.sf.hibernate.cfg.Configuration()
148                 .setProperties(getProperties())
149                 .addClass(gov.bnl.gums.db.HibernateMapping.class)
150                 .addClass(gov.bnl.gums.db.HibernateUser.class);
151             return cfg.buildSessionFactory();
152         } catch (Exception e) {
153             log.error("Couldn't initialize Hibernate", e);
154             throw new RuntimeException("An error occurred while initializing the database environment (hibernate): "+ e.getMessage(), e);
155         }
156     }
157     
158     private Properties readHibernateProperties() {
159         log.trace("Retrieving hibernate properties from hibernate.properties in the classpath");
160         PropertyResourceBundle prop = (PropertyResourceBundle) ResourceBundle.getBundle("hibernate");
161         Properties prop2 = new Properties();
162         Enumeration keys = prop.getKeys();
163         while (keys.hasMoreElements()) {
164             String key = (String) keys.nextElement();
165             prop2.setProperty(key, prop.getString(key));
166         }
167         return prop2;
168     }
169     
170 }