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.HttpServletRequest;
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;
import net.sf.aguacate.util.servlet.ServletUtil;

public class FunctionContext implements Closeable {

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

	private final String method;

	private final DatabaseBridge databaseBridge;

	private final HttpServletRequest request;

	private final HttpServletResponse response;

	private final Map outputFields;

	private Connection connection;

	public FunctionContext(String method, DatabaseBridge databaseBridge, HttpServletRequest request) {
		this(method, databaseBridge, request, null, null);
	}

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

	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 String getMethod() {
		return method;
	}

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

	@SuppressWarnings("unchecked")
	public Map get(String name) {
		return (Map) request.getAttribute(ServletUtil.REQ_METADATA);
	}

	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 - 2024 Weber Informatics LLC | Privacy Policy