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

📄 defaultconnectionmanager.java

📁 Mandarax是一个规则引擎的纯Java实现。它支持多类型的事实和基于反映的规则
💻 JAVA
字号:
/*
 * Copyright (C) 2003 <a href="mailto:jochen.hiller@bauer-partner.com">Jochen Hiller</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.mandarax.sql;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.mandarax.util.logging.LogCategories;

/**
 * The DefaultConnectionManager implements the 
 * interface of the SQLConnectionManager. 
 * 
 * It supports specification of a plain SQL connection,
 * or usage of the J2EE compliant data sources.
 * If a plain SQL connection will be given during initialize,
 * a data source wrapper will be used for this connection. 
 * 
 * @see SQLConnectionManager
 * @see java.sql.Connection
 * @see javax.sql.DataSource
 * 
 * @author <a href="mailto:jochen.hiller@bauer-partner.com">Jochen Hiller</a>
 * @version 3.4 <7 March 05>
 * @since 2.2
 */
public class DefaultConnectionManager implements SQLConnectionManager {

	// attributes

	/** a data source */
	private DataSource dataSource = null;

	// constructors

	/**
	 * Instantiates a new connection manager for a given SQL connection.
	 * The connection will be wrapped through our data source
	 * connection wrapper.
	 * 
	 * @param con the plain sql connection
	 */
	public DefaultConnectionManager(Connection con) {
		if (con == null) {
			throw new IllegalArgumentException("Connection is null");
		}
		this.dataSource = new ConnectionWrapperDataSource (con);
	}

	/**
	 * Instantiates a new connection manager for a given data source.
	 * 
	 * @param ds the given data source
	 */
	public DefaultConnectionManager(DataSource ds) {
		if (ds == null) {
			throw new IllegalArgumentException("DataSource is null");
		}
		this.dataSource = ds;
	}

	// public methods

	/**
	 * Gets a data source from the connection manager.
	 * 
	 * @return a data source
	 */
	public DataSource getDataSource () {
		LogCategories.LOG_SQL.debug("getDataSource()");
		return this.dataSource;
	}

	/**
	 * Releases a connection, give it back to connection manager.
	 * 
	 * @todo close the connection, dependent on a bool setting
	 * @param con the connection to release 
	 */
	public void releaseConnection(Connection con) throws SQLException {
		LogCategories.LOG_SQL.debug("releaseConnection ()");
		// nothing to do for the moment
	}

	// inner classes

	/**
	 * A data source, which wraps a simple SQL connection.
	 * 
	 * Only in private scope.
	 * 
     * @author <a href="mailto:jochen.hiller@bauer-partner.com">Jochen Hiller</a>
 * @version 3.4 <7 March 05>
     * @since 2.2
	 */
	private class ConnectionWrapperDataSource implements DataSource {

		/** the connection to wrap */
		private Connection wrappedConnection;

		/** the login timeout */
		private int loginTimeout;

		/** the log print writer */
		private PrintWriter logPrintWriter;

		/**
		 * Default constructor.
		 */
		public ConnectionWrapperDataSource(Connection con) {
			super();
			wrappedConnection = con;
		}

		/**
		 * Get a database connection.
		 * @return a database connection
		 */
		public Connection getConnection() throws SQLException {
			return wrappedConnection;
		}

		/**
		 * Get a database connection.
		 * 
		 * @todo support user/password
		 * @param userName a user name
		 * @param password a password
		 * @return a database connection
		 */
		public Connection getConnection(String userName, String password) throws SQLException {
			return getConnection ();
		}
	
		/**
		 * Get the login timeout.
		 * @return the login timeout (an integer).
		 */
		public int getLoginTimeout() throws SQLException {
			return loginTimeout; 
		}

		/**
		 * Get a log writer.
		 * @return a log writer.
		 */
		public PrintWriter getLogWriter() throws SQLException {
			return logPrintWriter;
		}

		/**
		 * Set the login timeout.
		 * @param millis the time in milli seconds
		 */
		public void setLoginTimeout(int millis) throws SQLException {
			loginTimeout = millis;
		}

		/**
		 * Set the log writer.
		 * @param writer a writer
		 */
		public void setLogWriter(PrintWriter writer) throws SQLException {
			logPrintWriter = writer;
		}
	}
}

⌨️ 快捷键说明

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