View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   package org.lsst.ccs.localdb.statusdb.utils;
6   
7   import java.io.IOException;
8   import java.util.Properties;
9   import org.hibernate.SessionFactory;
10  import org.hibernate.cfg.AnnotationConfiguration;
11  import org.hibernate.cfg.Configuration;
12  import org.lsst.ccs.bootstrap.resources.BootstrapResourceUtils;
13  import org.lsst.ccs.utilities.logging.Logger;
14  
15  /**
16   *
17   * @author turri
18   */
19  public class StatusdbUtils {
20      
21      public static AnnotationConfiguration ac;
22      public static Configuration cfg;
23      public static SessionFactory fac;
24      
25      
26      public static SessionFactory getSessionFactory(Properties inputProperties) {
27                          
28          if (ac == null) {
29              ac = new AnnotationConfiguration();
30          }
31  
32          if (cfg == null) {
33              String hibernateConfigFile = System.getProperty("org.lsst.ccs.localdb.hibernate.config.file", "hibernate-tm.cfg.xml");
34              cfg = ac.configure(hibernateConfigFile);
35              String propertiesFile = System.getProperty("org.lsst.ccs.localdb.hibernate.properties.file", "statusPersister.properties");
36              
37  //            Properties cfgProps = FrameworkUtils.loadProperties(null, StatusdbUtils.class, propertiesFile);
38    
39              //Load the properties from the environment.
40              //The Properties must be flat as the addProperties method on the Configuration object
41              //does not support chained properties.
42              Properties cfgProps = new Properties();
43              try {
44                  cfgProps.load(BootstrapResourceUtils.getBootstrapPropertiesInputStream(propertiesFile,StatusdbUtils.class));
45              } catch (IOException ioe) {
46                  throw new RuntimeException("Could not load Hibernate properties",ioe);
47              }
48                            
49              if ( inputProperties != null ) {
50                  cfg = cfg.addProperties(inputProperties);
51              }
52              
53              cfg = cfg.addProperties(cfgProps);
54  
55              Logger log = Logger.getLogger("org.lsst.ccs.localdb");
56              Properties finalP = cfg.getProperties();            
57              log.info("*** Working with connection properties ");
58              for (Object key : finalP.keySet()) {
59                  String skey = (String) key;
60                  if (skey.contains("connection")) {
61                      log.info(skey + ": " + finalP.getProperty(skey));
62                  }
63              }
64              log.info("*********************************");
65          }
66  
67          if (fac == null) {
68              fac = cfg.buildSessionFactory();
69          }
70  
71          return fac;
72      }
73      
74      
75  }