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 ; 12 import java.util.List ; 13 14 15 23 public class HibernateServiceImpl implements Service, HibernateService { 24 private Configuration _config = new Configuration(); 25 private SessionFactory _sessions; 26 private List _props = new ArrayList (); 27 28 public HibernateServiceImpl() { 29 super(); 30 } 31 32 47 public void addClass(String className) throws Exception { 48 _config.addClass(Class.forName(className)); 49 } 50 51 63 public Param createProperty() { 64 Param param = new Param(); 65 _props.add(param); 66 67 return param; 68 } 69 70 73 public void init() throws Exception { 74 } 75 76 79 public void start() throws Exception { 80 _sessions = _config.buildSessionFactory(); 81 } 82 83 86 public void dispose() { 87 try { 88 _sessions.close(); 89 } catch (HibernateException e) { 90 } 92 } 93 94 97 public Session currentSession() throws HibernateException { 98 return SessionState.currentSession(_sessions); 99 } 100 101 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 |