⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 localsessionfactory.java

📁 Spring API核心源代码 Spring API核心源代码 Spring API核心源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 * Specify a standard JDBC DataSource that TopLink should use as connection pool.
	 * This allows for using a shared DataSource definition instead of TopLink's
	 * own connection pool.
	 * <p>A passed-in DataSource will be wrapped in an appropriate TopLink Connector
	 * and registered with the TopLink DatabaseLogin instance (either the default
	 * instance or one passed in through the "databaseLogin" property). The
	 * "usesExternalConnectionPooling" flag will automatically be set to "true".
	 * @see oracle.toplink.sessions.DatabaseLogin#setConnector(oracle.toplink.sessions.Connector)
	 * @see oracle.toplink.sessions.DatabaseLogin#setUsesExternalConnectionPooling(boolean)
	 * @see #setDatabaseLogin(oracle.toplink.sessions.DatabaseLogin)
	 */
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	/**
	 * Specify the TopLink DatabasePlatform instance that the Session should use:
	 * for example, HSQLPlatform. This is an alternative to specifying the platform
	 * in a &lt;login&gt; tag in the <code>sessions.xml</code> configuration file.
	 * <p>A passed-in DatabasePlatform will be registered with the TopLink
	 * DatabaseLogin instance (either the default instance or one passed in
	 * through the "databaseLogin" property).
	 * @see oracle.toplink.internal.databaseaccess.HSQLPlatform
	 * @see oracle.toplink.platform.database.HSQLPlatform
	 */
	public void setDatabasePlatform(DatabasePlatform databasePlatform) {
		this.databasePlatform = databasePlatform;
	}

	/**
	 * Specify a TopLink SessionLog instance to use for detailed logging of the
	 * Session's activities: for example, DefaultSessionLog (which logs to the
	 * console), JavaLog (which logs through JDK 1.4'S <code>java.util.logging</code>,
	 * available as of TopLink 10.1.3), or CommonsLoggingSessionLog /
	 * CommonsLoggingSessionLog904 (which logs through Commons Logging,
	 * on TopLink 10.1.3 and 9.0.4, respectively).
	 * <p>Note that detailed Session logging is usually only useful for debug
	 * logging, with adjustable detail level. As of TopLink 10.1.3, TopLink also
	 * uses different log categories, which allows for fine-grained filtering of
	 * log messages. For standard execution, no SessionLog needs to be specified.
	 * @see oracle.toplink.sessions.DefaultSessionLog
	 * @see oracle.toplink.logging.DefaultSessionLog
	 * @see oracle.toplink.logging.JavaLog
	 * @see org.springframework.orm.toplink.support.CommonsLoggingSessionLog
	 * @see org.springframework.orm.toplink.support.CommonsLoggingSessionLog904
	 */
	public void setSessionLog(SessionLog sessionLog) {
		this.sessionLog = sessionLog;
	}


	/**
	 * Create a TopLink SessionFactory according to the configuration settings.
	 * @return the new TopLink SessionFactory
	 * @throws TopLinkException in case of errors
	 */
	public SessionFactory createSessionFactory() throws TopLinkException {
		if (logger.isInfoEnabled()) {
			logger.info("Initializing TopLink SessionFactory from [" + this.configLocation + "]");
		}

		// Determine class loader to use.
		ClassLoader classLoader =
				(this.sessionClassLoader != null ? this.sessionClassLoader : ClassUtils.getDefaultClassLoader());

		// Initialize the TopLink Session, using the configuration file
		// and the session name.
		DatabaseSession session = loadDatabaseSession(this.configLocation, this.sessionName, classLoader);

		// It is possible for SessionManager to return a null Session!
		if (session == null) {
			throw new IllegalStateException(
					"A session named '" + this.sessionName + "' could not be loaded from resource [" +
					this.configLocation + "] using ClassLoader [" + classLoader + "]. " +
					"This is most likely a deployment issue: Can the class loader access the resource?");
		}

		DatabaseLogin login = (this.databaseLogin != null ? this.databaseLogin : session.getLogin());

		// Apply specified login properties to the DatabaseLogin instance.
		if (this.loginPropertyMap != null) {
			new BeanWrapperImpl(login).setPropertyValues(this.loginPropertyMap);
		}

		// Override default connection pool with specified DataSource, if any.
		if (this.dataSource != null) {
			login.setConnector(new JNDIConnector(this.dataSource));
			login.setUsesExternalConnectionPooling(true);
		}

		// Override default DatabasePlatform with specified one, if any.
		if (this.databasePlatform != null) {
			login.usePlatform(this.databasePlatform);
		}

		// Override default DatabaseLogin instance with specified one, if any.
		if (this.databaseLogin != null) {
			setDatabaseLogin(session, this.databaseLogin);
		}

		// Override default SessionLog with specified one, if any.
		if (this.sessionLog != null) {
			session.setSessionLog(this.sessionLog);
			session.logMessages();
		}

		// Log in and create corresponding SessionFactory.
		session.login();
		return newSessionFactory(session);
	}

	/**
	 * Handle differences between the <code>Session.setLogin</code> interface
	 * between TopLink 9.0.4 to 10.1.3.
	 * <p>The Login interface was introduced in TopLink 10.1.3.
	 * @param session the DatabaseSession being logged in
	 * @param login the DatabaseLogin injected by Spring
	 * @see oracle.toplink.sessions.DatabaseSession#setLogin
	 */
	protected void setDatabaseLogin(DatabaseSession session, DatabaseLogin login) {
		Method setLoginMethod = null;
		try {
			// Search for the new 10.1.3 Login interface...
			Class loginClass = Class.forName("oracle.toplink.sessions.Login");
			setLoginMethod = DatabaseSession.class.getMethod("setLogin", new Class[] {loginClass});
			if (logger.isDebugEnabled()) {
				logger.debug("Using TopLink 10.1.3 setLogin(Login) API");
			}
		}
		catch (Exception ex) {
			// TopLink 10.1.3 Login interface not found ->
			// fall back to TopLink 9.0.4's setLogin(DatabaseLogin)
			if (logger.isDebugEnabled()) {
				logger.debug("Using TopLink 9.0.4 setLogin(DatabaseLogin) API");
			}
			session.setLogin(login);
			return;
		}

		// Invoke the 10.1.3 version: Session.setLogin(Login)
		ReflectionUtils.invokeMethod(setLoginMethod, session, new Object[] {login});
	}

	/**
	 * Load the specified DatabaseSession from the TopLink <code>sessions.xml</code>
	 * configuration file.
	 * @param configLocation the class path location of the <code>sessions.xml</code> file
	 * @param sessionName the name of the TopLink Session in the configuration file
	 * @param sessionClassLoader the class loader to use
	 * @return the DatabaseSession instance
	 * @throws TopLinkException in case of errors
	 */
	protected DatabaseSession loadDatabaseSession(
			String configLocation, String sessionName, ClassLoader sessionClassLoader)
			throws TopLinkException {

		SessionManager manager = getSessionManager();

		// Try to find TopLink 10.1.3 XMLSessionConfigLoader.
		Method getSessionMethod = null;
		Object loader = null;
		try {
			Class loaderClass = Class.forName("oracle.toplink.tools.sessionconfiguration.XMLSessionConfigLoader");
			getSessionMethod = SessionManager.class.getMethod("getSession",
					new Class[] {loaderClass, String.class, ClassLoader.class, boolean.class, boolean.class, boolean.class});
			if (logger.isDebugEnabled()) {
				logger.debug("Using TopLink 10.1.3 XMLSessionConfigLoader");
			}
			Constructor ctor = loaderClass.getConstructor(new Class[] {String.class});
			loader = ctor.newInstance(new Object[] {configLocation});
		}
		catch (Exception ex) {
			// TopLink 10.1.3 XMLSessionConfigLoader not found ->
			// fall back to TopLink 9.0.4 XMLLoader.
			if (logger.isDebugEnabled()) {
				logger.debug("Using TopLink 9.0.4 XMLLoader");
			}
			XMLLoader xmlLoader = new XMLLoader(configLocation);
			return (DatabaseSession) manager.getSession(xmlLoader, sessionName, sessionClassLoader, false, false);
		}

		// TopLink 10.1.3 XMLSessionConfigLoader found -> create loader instance
		// through reflection and fetch specified Session from SessionManager.
		// This invocation will check if the ClassLoader passed in is the same
		// as the one used to a session currently loaded with the same "sessionName"
		// If the ClassLoaders are different, then this LocalSessionFactory is being
		// re-loaded after a hot-deploy and the existing DatabaseSession will be logged
		// out and re-built from scratch.
		return (DatabaseSession) ReflectionUtils.invokeMethod(getSessionMethod, manager,
				new Object[] {loader, sessionName, sessionClassLoader, Boolean.FALSE, Boolean.FALSE, Boolean.TRUE});
	}

	/**
	 * Return the TopLink SessionManager to use for loading DatabaseSessions.
	 * <p>The default implementation creates a new plain SessionManager instance,
	 * leading to completely independent TopLink Session instances. Could be
	 * overridden to return a shared or pre-configured SessionManager.
	 * @return the TopLink SessionManager instance
	 */
	protected SessionManager getSessionManager() {
		return new SessionManager();
	}

	/**
	 * Create a new SessionFactory for the given TopLink DatabaseSession.
	 * <p>The default implementation creates a ServerSessionFactory for a
	 * ServerSession and a SingleSessionFactory for a plain DatabaseSession.
	 * @param session the TopLink DatabaseSession to create a SessionFactory for
	 * @return the SessionFactory
	 * @throws TopLinkException in case of errors
	 * @see ServerSessionFactory
	 * @see SingleSessionFactory
	 * @see oracle.toplink.threetier.ServerSession
	 * @see oracle.toplink.sessions.DatabaseSession
	 */
	protected SessionFactory newSessionFactory(DatabaseSession session) {
		if (session instanceof ServerSession) {
			return new ServerSessionFactory((ServerSession) session);
		}
		else if (session instanceof SessionBroker) {
			return new SessionBrokerSessionFactory((SessionBroker) session);
		}
		else {
			return new SingleSessionFactory(session);
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -