1 16 17 package org.springframework.orm.toplink; 18 19 import java.lang.reflect.Constructor ; 20 import java.lang.reflect.Method ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 import java.util.Properties ; 24 25 import javax.sql.DataSource ; 26 27 import oracle.toplink.exceptions.TopLinkException; 28 import oracle.toplink.internal.databaseaccess.DatabasePlatform; 29 import oracle.toplink.jndi.JNDIConnector; 30 import oracle.toplink.sessionbroker.SessionBroker; 31 import oracle.toplink.sessions.DatabaseLogin; 32 import oracle.toplink.sessions.DatabaseSession; 33 import oracle.toplink.sessions.SessionLog; 34 import oracle.toplink.threetier.ServerSession; 35 import oracle.toplink.tools.sessionconfiguration.XMLLoader; 36 import oracle.toplink.tools.sessionmanagement.SessionManager; 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 40 import org.springframework.beans.BeanWrapperImpl; 41 import org.springframework.util.ClassUtils; 42 import org.springframework.util.CollectionUtils; 43 import org.springframework.util.ReflectionUtils; 44 45 96 public class LocalSessionFactory { 97 98 102 public static final String DEFAULT_SESSIONS_XML = "sessions.xml"; 103 104 107 public static final String DEFAULT_SESSION_NAME = "Session"; 108 109 110 protected final Log logger = LogFactory.getLog(getClass()); 111 112 115 private String configLocation = DEFAULT_SESSIONS_XML; 116 117 120 private String sessionName = DEFAULT_SESSION_NAME; 121 122 125 private ClassLoader sessionClassLoader; 126 127 private DatabaseLogin databaseLogin; 128 129 private final Map loginPropertyMap = new HashMap (); 130 131 private DataSource dataSource; 132 133 private DatabasePlatform databasePlatform; 134 135 private SessionLog sessionLog; 136 137 138 148 public void setConfigLocation(String configLocation) { 149 this.configLocation = configLocation; 150 } 151 152 157 public void setSessionName(String sessionName) { 158 this.sessionName = sessionName; 159 } 160 161 171 public void setSessionClassLoader(ClassLoader sessionClassLoader) { 172 this.sessionClassLoader = sessionClassLoader; 173 } 174 175 191 public void setDatabaseLogin(DatabaseLogin databaseLogin) { 192 this.databaseLogin = databaseLogin; 193 } 194 195 202 public void setLoginProperties(Properties loginProperties) { 203 CollectionUtils.mergePropertiesIntoMap(loginProperties, this.loginPropertyMap); 204 } 205 206 212 public void setLoginPropertyMap(Map loginProperties) { 213 if (loginProperties != null) { 214 this.loginPropertyMap.putAll(loginProperties); 215 } 216 } 217 218 225 public Map getLoginPropertyMap() { 226 return this.loginPropertyMap; 227 } 228 229 241 public void setDataSource(DataSource dataSource) { 242 this.dataSource = dataSource; 243 } 244 245 255 public void setDatabasePlatform(DatabasePlatform databasePlatform) { 256 this.databasePlatform = databasePlatform; 257 } 258 259 276 public void setSessionLog(SessionLog sessionLog) { 277 this.sessionLog = sessionLog; 278 } 279 280 281 286 public SessionFactory createSessionFactory() throws TopLinkException { 287 if (logger.isInfoEnabled()) { 288 logger.info("Initializing TopLink SessionFactory from [" + this.configLocation + "]"); 289 } 290 291 ClassLoader classLoader = 293 (this.sessionClassLoader != null ? this.sessionClassLoader : ClassUtils.getDefaultClassLoader()); 294 295 DatabaseSession session = loadDatabaseSession(this.configLocation, this.sessionName, classLoader); 298 299 if (session == null) { 301 throw new IllegalStateException ( 302 "A session named '" + this.sessionName + "' could not be loaded from resource [" + 303 this.configLocation + "] using ClassLoader [" + classLoader + "]. " + 304 "This is most likely a deployment issue: Can the class loader access the resource?"); 305 } 306 307 DatabaseLogin login = (this.databaseLogin != null ? this.databaseLogin : session.getLogin()); 308 309 if (this.loginPropertyMap != null) { 311 new BeanWrapperImpl(login).setPropertyValues(this.loginPropertyMap); 312 } 313 314 if (this.dataSource != null) { 316 login.setConnector(new JNDIConnector(this.dataSource)); 317 login.setUsesExternalConnectionPooling(true); 318 } 319 320 if (this.databasePlatform != null) { 322 login.usePlatform(this.databasePlatform); 323 } 324 325 if (this.databaseLogin != null) { 327 setDatabaseLogin(session, this.databaseLogin); 328 } 329 330 if (this.sessionLog != null) { 332 session.setSessionLog(this.sessionLog); 333 session.logMessages(); 334 } 335 336 session.login(); 338 return newSessionFactory(session); 339 } 340 341 349 protected void setDatabaseLogin(DatabaseSession session, DatabaseLogin login) { 350 Method setLoginMethod = null; 351 try { 352 Class loginClass = Class.forName("oracle.toplink.sessions.Login"); 354 setLoginMethod = DatabaseSession.class.getMethod("setLogin", new Class [] {loginClass}); 355 if (logger.isDebugEnabled()) { 356 logger.debug("Using TopLink 10.1.3 setLogin(Login) API"); 357 } 358 } 359 catch (Exception ex) { 360 if (logger.isDebugEnabled()) { 363 logger.debug("Using TopLink 9.0.4 setLogin(DatabaseLogin) API"); 364 } 365 session.setLogin(login); 366 return; 367 } 368 369 ReflectionUtils.invokeMethod(setLoginMethod, session, new Object [] {login}); 371 } 372 373 382 protected DatabaseSession loadDatabaseSession( 383 String configLocation, String sessionName, ClassLoader sessionClassLoader) 384 throws TopLinkException { 385 386 SessionManager manager = getSessionManager(); 387 388 Method getSessionMethod = null; 390 Object loader = null; 391 try { 392 Class loaderClass = Class.forName("oracle.toplink.tools.sessionconfiguration.XMLSessionConfigLoader"); 393 getSessionMethod = SessionManager.class.getMethod("getSession", 394 new Class [] {loaderClass, String .class, ClassLoader .class, boolean.class, boolean.class, boolean.class}); 395 if (logger.isDebugEnabled()) { 396 logger.debug("Using TopLink 10.1.3 XMLSessionConfigLoader"); 397 } 398 Constructor ctor = loaderClass.getConstructor(new Class [] {String .class}); 399 loader = ctor.newInstance(new Object [] {configLocation}); 400 } 401 catch (Exception ex) { 402 if (logger.isDebugEnabled()) { 405 logger.debug("Using TopLink 9.0.4 XMLLoader"); 406 } 407 XMLLoader xmlLoader = new XMLLoader(configLocation); 408 return (DatabaseSession) manager.getSession(xmlLoader, sessionName, sessionClassLoader, false, false); 409 } 410 411 return (DatabaseSession) ReflectionUtils.invokeMethod(getSessionMethod, manager, 419 new Object [] {loader, sessionName, sessionClassLoader, Boolean.FALSE, Boolean.FALSE, Boolean.TRUE}); 420 } 421 422 429 protected SessionManager getSessionManager() { 430 return new SessionManager(); 431 } 432 433 445 protected SessionFactory newSessionFactory(DatabaseSession session) { 446 if (session instanceof ServerSession) { 447 return new ServerSessionFactory((ServerSession) session); 448 } 449 else if (session instanceof SessionBroker) { 450 return new SessionBrokerSessionFactory((SessionBroker) session); 451 } 452 else { 453 return new SingleSessionFactory(session); 454 } 455 } 456 457 } 458 | Popular Tags |