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

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

Go to download

Awake SQL is an open source framework that allows remote and secure JDBC access through HTTP.

The newest version!
/*
 * 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 java.sql.Savepoint;

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.api.util.HtmlConverter;
import org.awakefw.file.util.TransferStatus;
import org.awakefw.file.util.parms.Parameter;
import org.awakefw.sql.transport.SavepointHttp;
import org.awakefw.sql.util.ConnectionParms;
import org.awakefw.sql.util.SqlActionTransaction;
import org.awakefw.sql.util.SqlReturnCode;

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

    private static boolean DEBUG = AwakeDebug.isSet(SavepointUtil.class);

    /**
     * Calls a setSavepoint(), setSavepoint(name), rollback(Savepoint
     * savepoint), releaseSavepoint(Savepoint savepoint)
     * 
     * @param request
     * @param awakeCommonsConfigurator
     * @param out
     * @param action
     * @throws IOException
     * @throws SQLException
     * @throws IllegalArgumentException
     */
    public static void setSavepointExecute(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();

	debug("");
	debug("setSavepointExecute");
	debug("action       : " + action);
	debug("connectionId : " + connectionId);
	debug("username     : " + username);
		
	if (connection == null) {
	    out.println(TransferStatus.SEND_OK);
	    out.println(SqlReturnCode.SESSION_INVALIDATED);
	    return;
	}

	if (action.equals(SqlActionTransaction.ACTION_SQL_SET_SAVEPOINT)) {

	    Savepoint savepoint = connection.setSavepoint();
	    connectionStore.put(savepoint);

	    SavepointHttp savepointHttp = new SavepointHttp(savepoint.getSavepointId(), "noname");
	    String savepointStr = savepointHttp.toString();
	    savepointStr = HtmlConverter.toHtml(savepointStr);
	    out.println(TransferStatus.SEND_OK);
	    out.println(savepointStr);
	    return;

	} else if (action
		.equals(SqlActionTransaction.ACTION_SQL_SET_SAVEPOINT_NAME)) {
	    String name = request.getParameter(ConnectionParms.NAME);

	    name = HtmlConverter.fromHtml(name);

	    Savepoint savepoint = connection.setSavepoint(name);
	    connectionStore.put(savepoint);
	    
	    SavepointHttp savepointHttp = new SavepointHttp(-1, savepoint.getSavepointName());
	    String savepointStr = savepointHttp.toString();
	    savepointStr = HtmlConverter.toHtml(savepointStr);
	    out.println(TransferStatus.SEND_OK);
	    out.println(savepointStr);
	    return;
	    

	} else if (action
		.equals(SqlActionTransaction.ACTION_SQL_SET_ROLLBACK_SAVEPOINT)) {
	    String savepointStr = request
		    .getParameter(ConnectionParms.SAVEPOINT);
	    savepointStr = HtmlConverter.fromHtml(savepointStr);

	    Savepoint savepointInfo = SavepointHttp
		    .buildFromString(savepointStr);

	    Savepoint savepoint = connectionStore.getSavepoint(savepointInfo);
	    
	    if (savepoint == null) {
		throw new SQLException("Savepoint does not esxists anymore");
	    }
	    
	    connection.rollback(savepoint);

	    out.println(TransferStatus.SEND_OK);
	    return;

	} else if (action
		.equals(SqlActionTransaction.ACTION_SQL_SET_RELEASE_SAVEPOINT)) {
	    String savepointStr = request
		    .getParameter(ConnectionParms.SAVEPOINT);
	    savepointStr = HtmlConverter.fromHtml(savepointStr);

	    Savepoint savepointInfo = SavepointHttp
		    .buildFromString(savepointStr);

	    Savepoint savepoint = connectionStore.getSavepoint(savepointInfo);
	    
	    if (savepoint == null) {
		throw new SQLException("Savepoint does not esxists anymore");
	    }
	    
	    connection.releaseSavepoint(savepoint);
	    connectionStore.remove(savepointInfo);
	    
	    out.println(TransferStatus.SEND_OK);
	    return;
	} else {
	    throw new IllegalArgumentException(
		    "Invalid Sql Action for setSavepointExecute(): " + 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