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

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

/*
 * This file is part of Awake SQL. 
 * Awake SQL: Remote JDBC access over HTTP.                                    
 * Copyright (C) 2013,  KawanSoft SAS
 * (http://www.kawansoft.com). All rights reserved.                    
 *                                                                         
 * Awake SQL is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.         
 *              
 * Awake 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 General Public License
 * along with this program; if not, see .
 *
 * If you develop commercial activities using Awake SQL, you must: 
 * a) disclose and distribute all source code of your own product,
 * b) license your own product under the GNU General Public License.
 * 
 * You can be released from the requirements of the license by
 * purchasing a commercial license. Buying such a license will allow you 
 * to ship Awake SQL with your closed source products without disclosing 
 * the source code.
 *
 * For more information, please contact KawanSoft SAS at this
 * address: [email protected]
 * 
 * Any modifications to this file must keep this entire header
 * intact.
 */
package org.awakefw.sql.servlet.connection;

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

import javax.servlet.http.HttpServletRequest;

import org.awakefw.commons.api.server.AwakeCommonsConfigurator;
import org.awakefw.commons.server.util.AwakeServerLogger;
import org.awakefw.file.api.util.AwakeDebug;
import org.awakefw.file.util.TransferStatus;
import org.awakefw.file.util.parms.Parameter;
import org.awakefw.sql.api.server.AwakeSqlConfigurator;
import org.awakefw.sql.servlet.ConnectionCloser;
import org.awakefw.sql.util.ConnectionParms;
import org.awakefw.sql.util.SqlActionTransaction;
import org.awakefw.sql.util.SqlReturnCode;

/**
 * @author Nicolas de Pomereu
 *
 * Wrapper for transactions commands to decrease code in  ServerAwakeSqlDispatch 
 */
public class TransactionUtil {

    private static boolean DEBUG = AwakeDebug.isSet(TransactionUtil.class);
    
    /**
     * Calls a commit(), rollback() or close() on a Connection
     * 
     * @param request
     * @param awakeCommonsConfigurator
     * @param awakeSqlConfigurator
     * @param out
     * @param action
     * @throws IOException
     * @throws SQLException
     * @throws IllegalArgumentException
     */
    public static  void setCommitRollbackCloseExecute(HttpServletRequest request,
	    AwakeCommonsConfigurator awakeCommonsConfigurator, AwakeSqlConfigurator awakeSqlConfigurator,
	    PrintWriter out,
	    String action) 
		    throws IOException, SQLException, IllegalArgumentException {    
	String connectionId = request
		.getParameter(ConnectionParms.CONNECTION_ID);
	String username = request.getParameter(Parameter.USERNAME);
	
	ConnectionStore connectionStore = new ConnectionStore(username,
		connectionId);
	Connection connection = connectionStore.get();

	if (connection == null) {
	    out.println(TransferStatus.SEND_OK);
	    out.println(SqlReturnCode.SESSION_INVALIDATED);
	    return;
	}

	if (action.equals(SqlActionTransaction.ACTION_SQL_COMMIT)) {
	    connection.commit();
	} else if (action.equals(SqlActionTransaction.ACTION_SQL_ROLLBACK)) {
	    connection.rollback();
	} else if (action.equals(SqlActionTransaction.ACTION_SQL_CON_CLOSE)) {	 
	    
	    debug("close asked from PC...");
	    connectionStore.remove();
	    
	    ConnectionCloser.freeConnection(connection,
		    awakeSqlConfigurator);
	}

	out.println(TransferStatus.SEND_OK);
	
    }
    
    /**
     * Calls a setAutocommit, setReadOnly(), setHoldbility, getTransactionIsolation
     * 
     * @param request
     * @param awakeCommonsConfigurator
     * @param out
     * @param action
     * @throws IOException
     * @throws SQLException
     * @throws IllegalArgumentException
     */
    public static void setAutocommitReadOnlyHoldabilityTransactionInsolationExecute(HttpServletRequest request,
	    AwakeCommonsConfigurator awakeCommonsConfigurator,
	    PrintWriter out,
	    String action) 
		    throws IOException, SQLException, IllegalArgumentException {
	String connectionId = request
		.getParameter(ConnectionParms.CONNECTION_ID);
	String username = request.getParameter(Parameter.USERNAME);

	ConnectionStore connectionStore = new ConnectionStore(username,
		connectionId);
	Connection connection = connectionStore.get();

	if (connection == null) {
	    out.println(TransferStatus.SEND_OK);
	    out.println(SqlReturnCode.SESSION_INVALIDATED);
	    return;
	}

	if(action.equals(SqlActionTransaction.ACTION_SQL_SET_AUTOCOMMIT)) {
	    boolean autoCommit = Boolean.parseBoolean(request.getParameter(ConnectionParms.AUTOCOMMIT));
	    connection.setAutoCommit(autoCommit);
	}
	else if(action.equals(SqlActionTransaction.ACTION_SQL_SET_READ_ONLY)) {
	    boolean readOnly = Boolean.parseBoolean(request.getParameter(ConnectionParms.READONLY));
	    connection.setReadOnly(readOnly);
	} else if (action.equals(SqlActionTransaction.ACTION_SQL_SET_HOLDABILITY)) {
	    int holdability = Integer.parseInt(request.getParameter(ConnectionParms.HOLDABILITY));
	    connection.setHoldability(holdability);
	}  else if (action.equals(SqlActionTransaction.ACTION_SQL_SET_TRANSACTION_ISOLATION)) {
	    int level = Integer.parseInt(request.getParameter(ConnectionParms.TRANSACTION_ISOLATION));
	    connection.setTransactionIsolation(level);
	}
	else {
		throw new IllegalArgumentException("Invalid Sql Action for setAutocommitReadOnlyHoldabilityTransactionInsolationExecute(): "
			+ action);
	}

	out.println(TransferStatus.SEND_OK);
	
    }
    
    /**
     * Calls a getAutocommit, isReadOnly(), getHoldbility, SetTransactionIsolation
     * 
     * @param request
     * @param awakeCommonsConfigurator
     * @param out
     * @param action
     * @throws IOException
     * @throws SQLException
     * @throws IllegalArgumentException
     */
    
    public static void getAutocommitReadOnlyHoldabilityExecute(
	    HttpServletRequest request,
	    AwakeCommonsConfigurator awakeCommonsConfigurator, PrintWriter out,
	    String action) 		    throws IOException, SQLException, IllegalArgumentException{

	String connectionId = request
		.getParameter(ConnectionParms.CONNECTION_ID);
	String username = request.getParameter(Parameter.USERNAME);
	
	ConnectionStore connectionStore = new ConnectionStore(username,
		connectionId);
	Connection connection = connectionStore.get();

	if (connection == null) {
	    out.println(TransferStatus.SEND_OK);
	    out.println(SqlReturnCode.SESSION_INVALIDATED);
	    return;
	}

	if(action.equals(SqlActionTransaction.ACTION_SQL_GET_AUTOCOMMIT)) {
	    boolean autoCommit = connection.getAutoCommit();
	    out.println(TransferStatus.SEND_OK);
	    out.println(autoCommit + "");
	}
	else if(action.equals(SqlActionTransaction.ACTION_SQL_IS_READ_ONLY)) {
	    boolean readOnly = connection.isReadOnly();
	    out.println(TransferStatus.SEND_OK);
	    out.println(readOnly + "");
	} else if (action.equals(SqlActionTransaction.ACTION_SQL_GET_HOLDABILITY)) {
	    int holdability = connection.getHoldability();
	    out.println(TransferStatus.SEND_OK);
	    out.println(holdability + "");
	}
	else {
		throw new IllegalArgumentException("Invalid Sql Action for setAutocommitReadOnlyHoldabilityTransactionInsolationExecute(): "
			+ action);
	}	
    }
    
    
    /**
     * Method called by children Servlet for debug purpose println is done only
     * if class name name is in awake-debug.ini
     */
    public static void debug(String s) {
	if (DEBUG) {
	    AwakeServerLogger.log(s);
	}
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy