1 26 27 package org.objectweb.openccm.pss.runtime.hibernate.lib; 28 29 import net.sf.hibernate.cfg.Configuration; 30 import net.sf.hibernate.SessionFactory; 31 import net.sf.hibernate.Session; 32 33 40 41 public class Connector 42 extends org.objectweb.openccm.pss.runtime.common.lib.ConnectorBase 43 implements org.objectweb.openccm.pss.runtime.hibernate.api.Connector 44 { 45 51 public static final String NAME = "org.objectweb.openccm.pss.runtime.Hibernate"; 52 53 59 60 private Configuration _configuration = null; 61 62 63 private SessionFactory _session_factory = null; 64 65 66 java.util.Properties props_ = null; 67 68 74 77 public Connector() 78 { 79 super(NAME); 80 } 81 82 88 93 private void 94 load_hibernate_properties(String file) 95 { 96 props_ = new java.util.Properties (); 97 98 try { 99 java.io.InputStream propStream = null; 100 101 propStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(file); 102 props_.load(propStream); 103 }catch (Exception e) { 104 throw new RuntimeException ("Cannot load " + file + " file" + e); 105 } 106 107 System.getProperties().putAll(props_); 108 getConfiguration().setProperties(props_); 109 } 110 111 116 protected Configuration 117 getConfiguration() 118 { 119 if ( _configuration == null ) 120 { 121 _configuration = new Configuration(); 122 _configuration.setProperties(props_); 123 } 124 125 return _configuration; 126 } 127 128 134 protected void 135 registerPersistentClass(Class clazz) 136 { 137 if ( (clazz != null) && 138 (clazz.getName().compareTo("org.objectweb.openccm.pss.runtime.hibernate.lib.StorageObjectBase") != 0) ) 139 { 140 registerPersistentClass( clazz.getSuperclass() ); 141 try { 142 getConfiguration().addClass(clazz); 143 } catch (net.sf.hibernate.MappingException ex) { 144 } 147 } 148 } 149 150 155 protected SessionFactory 156 getSessionFactory() 157 { 158 if ( null == _session_factory ) { 159 try { 160 _session_factory = getConfiguration().buildSessionFactory(); 161 } catch (net.sf.hibernate.HibernateException ex) { 162 throw new RuntimeException ("Cannot instantiate hibernate session factory", ex); 163 } 164 } 165 return _session_factory; 166 } 167 168 174 180 183 public org.omg.CosPersistentState.Session 184 create_basic_session(short access_mode, 185 org.omg.CosPersistentState.Parameter[] additional_parameters) 186 { 187 String db_url = null, 188 db_driver = null, 189 user_name = null, 190 password = null; 191 java.sql.Connection connection = null; 192 193 if (additional_parameters.length == 1) 195 { 196 org.omg.CosPersistentState.Parameter param = additional_parameters[0]; 197 198 if (param.name.compareTo(Connector.CONF_FILE_KEY) == 0) 199 { 200 load_hibernate_properties( param.val.extract_string() ); 201 } 202 else 203 throw new RuntimeException ("CONF_FILE parameter is missing!"); 204 } 205 else 206 throw new RuntimeException ("CONF_FILE parameter is missing!"); 207 208 db_driver = props_.getProperty("hibernate.connection.driver_class"); 209 db_url = props_.getProperty("hibernate.connection.url"); 210 user_name = props_.getProperty("hibernate.connection.username"); 211 password = props_.getProperty("hibernate.connection.password"); 212 217 try { 219 Class.forName(db_driver); 220 } catch ( ClassNotFoundException ex ) { 221 throw new RuntimeException (ex); 222 } 223 224 try { 226 227 if ( (user_name == null) || (password == null) ) { 228 connection = java.sql.DriverManager.getConnection(db_url); 229 } else { 230 connection = java.sql.DriverManager.getConnection(db_url, user_name, password); 231 } 232 } catch ( java.sql.SQLException ex ) { 233 throw new RuntimeException (ex); 234 } 235 236 Session session = getSessionFactory().openSession(connection); 238 return new org.objectweb.openccm.pss.runtime.hibernate.lib.Session(get_free_id(), session); 239 248 } 249 250 253 public org.omg.CosPersistentState.TransactionalSession 254 create_transactional_session(short access_mode, 255 short default_isolation_level, 256 org.omg.CosPersistentState.EndOfAssociationCallback callback, 257 org.omg.CosPersistentState.Parameter[] additional_parameters) 258 { 259 return null; 261 } 262 263 266 public org.omg.CosPersistentState.SessionPool 267 create_session_pool(short access_mode, 268 short tx_policy, 269 org.omg.CosPersistentState.Parameter[] additional_parameters) 270 { 271 return null; 273 } 274 275 278 public org.omg.CosPersistentState.TransactionalSession 279 current_session() 280 { 281 return null; 283 } 284 285 288 public org.omg.CosPersistentState.TransactionalSession[] 289 sessions(org.omg.CosTransactions.Coordinator transaction) 290 { 291 return null; 293 } 294 295 296 302 305 public java.lang.Class 306 register_storage_object_factory(String storage_type_name, 307 java.lang.Class storage_object_factory) 308 { 309 registerPersistentClass(storage_object_factory); 310 return super.register_storage_object_factory(storage_type_name, storage_object_factory); 311 } 312 313 } 314 | Popular Tags |