All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.aguacate.function.FunctionContext Maven / Gradle / Ivy

There is a newer version: 0.10.9
Show newest version
package net.sf.aguacate.function;

import java.io.Closeable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.sf.aguacate.configuration.field.format.FieldFormat;
import net.sf.aguacate.util.config.database.DatabaseBridge;
import net.sf.aguacate.util.config.database.DatabaseInterface;

public class FunctionContext implements Closeable {

	private static final Logger LOGGER = LogManager.getLogger(FunctionContext.class);

	private final HttpServletResponse response;

	private final Map outputFields;

	private final DatabaseBridge databaseBridge;

	private Connection connection;

	public FunctionContext(DatabaseBridge databaseBridge) {
		this(null, null, databaseBridge);
	}

	public FunctionContext(HttpServletResponse response, Map outputFields,
			DatabaseBridge databaseBridge) {
		this.response = response;
		this.outputFields = outputFields;
		this.databaseBridge = databaseBridge;
	}

	public Connection acquireConnection() throws SQLException {
		if (connection == null) {
			connection = databaseBridge.getDataSource().getConnection();
		}
		if (LOGGER.isTraceEnabled()) {
			LOGGER.trace("using url: {}", connection.getMetaData().getURL());
		}
		return connection;
	}

	public DatabaseInterface databaseInterface() {
		return databaseBridge.getDatabaseInterface();
	}

	public HttpServletResponse response() {
		return response;
	}

	public Map getOutputFields() {
		return outputFields;
	}

	public void rollback() {
		if (connection != null) {
			try {
				connection.rollback();
			} catch (SQLException e) {
				throw new IllegalStateException(e);
			}
		}
	}

	@Override
	public void close() {
		if (connection != null) {
			Connection temp = connection;
			connection = null;
			try {
				temp.close();
			} catch (SQLException e) {
				LOGGER.warn("On close connection", e);
			}
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy