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

src-main.org.awakefw.sql.servlet.ServerBatchStatementExecutor Maven / Gradle / Ivy

/*
 * Awake File: Easy file upload & download through HTTP with Java
 * Awake SQL: Remote JDBC access through HTTP.                                    
 * Copyright (C) 2012, Kawan Softwares S.A.S.
 * (http://www.awakeframework.org). All rights reserved.                                
 *                                                                               
 * Awake File/SQL 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.1 of the License, or (at your option) any later version.            
 *                                                                               
 * Awake File/SQL 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., 51 Franklin Street, Fifth Floor, Boston, MA  
 * 02110-1301  USA
 *
 * Any modifications to this file must keep this entire header
 * intact.
 */

//Last Updates: 
// 18/10/11 16:00 NDP - ServerBatchStatementExecutor: creation
// 18/10/11 18:40 NDP - ServerBatchStatementExecutor: use ServerSqlUtil.setConnectionProperties
// 24/11/11 17:15 NDP : ServerBatchStatementExecutor: StatementHolderListReader.close()

package org.awakefw.sql.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.awakefw.commons.api.server.AwakeCommonsConfigurator;
import org.awakefw.file.api.server.AwakeFileConfigurator;
import org.awakefw.file.api.server.fileaction.HttpConfigurationUtil;
import org.awakefw.file.api.util.AwakeDebug;
import org.awakefw.file.util.AwakeLogger;
import org.awakefw.file.util.Tag;
import org.awakefw.file.util.parms.Parameter;
import org.awakefw.sql.api.server.AwakeSqlConfigurator;
import org.awakefw.sql.jdbc.util.crypto.StatementHolderListDecryptor;
import org.awakefw.sql.json.StatementHolder;
import org.awakefw.sql.servlet.sql.ServerBatchStatement;
import org.awakefw.sql.servlet.sql.ServerSqlUtil;
import org.awakefw.sql.servlet.sql.StatementHolderListReader;
import org.awakefw.sql.util.SqlAction;
import org.awakefw.sql.util.TransportConverter;

/**
 * Execute transported batch statements on the server
 * 
 * @author Nicolas de Pomereu
 * 
 */
public class ServerBatchStatementExecutor {
    private static boolean DEBUG = AwakeDebug
	    .isSet(ServerBatchStatementExecutor.class);

    // When executing a non-query (Prepared) Statement
    public static final String STATEMENT_NORMAL_EXECUTED = "0";

    private HttpServletRequest request = null;
    private PrintWriter out = null;

    private AwakeCommonsConfigurator awakeCommonsConfigurator = null;
    private AwakeFileConfigurator awakeFileConfigurator = null;
    private AwakeSqlConfigurator awakeSqlConfigurator = null;

    /**
     * Constructor
     * 
     * @param request
     *            the servlet http request
     * @param out
     *            the servlet output stream
     * @param awakeFileConfigurator
     *            the Awake File Configurator
     * @param awakeCommonsConfigurator
     *            the Awake Commons Configurator
     * @param awakeSqlConfigurator
     *            the Awake SQL Configurator
     */
    public ServerBatchStatementExecutor(HttpServletRequest request,
	    PrintWriter out, AwakeCommonsConfigurator awakeCommonsConfigurator,
	    AwakeFileConfigurator awakeFileConfigurator,
	    AwakeSqlConfigurator awakeSqlConfigurator) {
	this.request = request;
	this.out = out;

	this.awakeCommonsConfigurator = awakeCommonsConfigurator;
	this.awakeFileConfigurator = awakeFileConfigurator;
	this.awakeSqlConfigurator = awakeSqlConfigurator;
    }

    /**
     * Execute the Sql Action
     */
    public void execute() throws IOException, SQLException,
	    IllegalArgumentException {
	String username = request.getParameter(Parameter.LOGIN);
	String conHolderParam = request
		.getParameter(SqlAction.CONNECTION_HOLDER);
	String statementHolderParam = request
		.getParameter(SqlAction.STATEMENT_HOLDER);

	debug("SqlAction.CONNECTION_HOLDER: " + conHolderParam);
	debug("SqlAction.STATEMENT_HOLDER : " + statementHolderParam);

	Connection connection = null;

	try {
	    connection = awakeCommonsConfigurator.getConnection();
	    	    
	    ServerSqlUtil.setConnectionProperties(conHolderParam, connection);

	    if (statementHolderParam
		    .startsWith(TransportConverter.AWAKE_STREAM_FILE)) {
		String fileName = StringUtils.substringAfter(
			statementHolderParam,
			TransportConverter.AWAKE_STREAM_FILE);
		// Param contains only the file to read from the statements
		executeStatementsFromFile(username, fileName, connection);
	    } else {
		// All statements are in single param
		executeStatementsFromList(username, statementHolderParam,
			connection);
	    }

	    if (!connection.getAutoCommit()) {
		connection.commit();
	    }
	} catch (IOException e) {
	    if (!connection.getAutoCommit()) {
		connection.rollback();
	    }

	    throw e;
	} catch (SQLException e) {
	    if (!connection.getAutoCommit()) {
		connection.rollback();
	    }

	    throw e;
	} finally {
	    // Release the connection
	    ServerAwakeSqlDispatch.cleanAndFreeConnection(connection,
		    awakeCommonsConfigurator);
	}

    }

    /**
     * Execute the statements from file name passed in parameter
     * 
     * @param username
     *            the user username
     * @param fileName
     *            the filename containing all the statements in json format, one
     *            perline
     * @param connection
     *            the JDBC Connection
     * @throws SQLException
     * @throws IOException
     */
    private void executeStatementsFromFile(String username, String fileName,
	    Connection connection) throws SQLException, IOException {

	fileName = HttpConfigurationUtil.addRootPath(awakeFileConfigurator,
		username, fileName);

	File file = new File(fileName);

	if (!file.exists()) {
	    throw new IOException(
		    Tag.AWAKE_PRODUCT_FAIL
			    + "The file corresponding to a list of Statements does not exist on remote Server: "
			    + fileName);
	}

	StatementHolder statementHolder = null;

	ServerBatchStatement serverBatchStatement = new ServerBatchStatement(
		request, out, awakeSqlConfigurator, connection, username);

	StatementHolderListReader statementHolderListReader = null;

	try {
	    statementHolderListReader = new StatementHolderListReader(file,
		    awakeCommonsConfigurator);

	    while ((statementHolder = statementHolderListReader.readLine()) != null) {
		String sqlOrder = statementHolder.getSqlOrder();
		serverBatchStatement.addBatch(sqlOrder);
	    }

	    serverBatchStatement.executeBatchAndClose();
	} finally {
	    if (statementHolderListReader != null) {
		statementHolderListReader.close();
		statementHolderListReader = null;
	    }
	    
	    serverBatchStatement = null;
	}
    }

    /**
     * Execute the statements from the parameter that contains the list of
     * StatementHolder in JSon format
     * 
     * @param username
     *            the user username
     * @param statementHolderParam
     *            the statement request parameter
     * @param connection
     *            the JDBC Connection
     * @throws SQLException
     * @throws IOException
     */
    private void executeStatementsFromList(String username,
	    String statementHolderParam, Connection connection)
	    throws SQLException, IOException {

	List statementHolderList = StatementHolderListDecryptor
		.decryptFromJson(statementHolderParam, awakeCommonsConfigurator);

	ServerBatchStatement serverBatchStatement = new ServerBatchStatement(
		request, out, awakeSqlConfigurator, connection, username);

	for (StatementHolder statementHolder : statementHolderList) {
	    String sqlOrder = statementHolder.getSqlOrder();
	    serverBatchStatement.addBatch(sqlOrder);
	}

	serverBatchStatement.executeBatchAndClose();

    }

    /**
     * Method called by children Servlest for debug purpose Println is done only
     * if class name name is in debug_list.ini
     */
    public static void debug(String s) {
	if (DEBUG) {
	    AwakeLogger.log(s);
	}
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy