src-main.org.awakefw.sql.jdbc.util.CallableResultFileSplitter 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.jdbc.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import org.apache.commons.io.IOUtils;
import org.awakefw.file.api.util.AwakeDebug;
import org.awakefw.file.util.AwakeClientLogger;
import org.awakefw.file.util.KeepTempFilePolicyParms;
import org.awakefw.file.util.Tag;
/**
*
* Class that allows to split in two file a received file that contains both a
* StatementHolder and a ResultSet:
*
* - The StatementHolder is the fist line of the file.
* - The ResultSet is the rest of the file.
*
*
* @author Nicolas de Pomereu
*
*/
public class CallableResultFileSplitter {
/** Debug flag */
private static boolean DEBUG = AwakeDebug.isSet(CallableResultFileSplitter.class);
/** Universal and clean line separator */
public static String CR_LF = System.getProperty("line.separator");
/** The file that contains all: StatementHolder (1 line) + ResultSet */
private File file = null;
/** The file that contains only the StatementHolderFile */
private File callableStatementHolderFile = null;
/** The file that contains only the ResultSet */
private File resultSetFile = null;
/**
* Constructor
*
* @param file
* The file that contains: first line a StatementHolderFile and a ResultSet in the other lines
*
*/
public CallableResultFileSplitter(File file) throws SQLException {
if (file == null) {
String message = Tag.AWAKE_PRODUCT_FAIL
+ "file can not be null!";
throw new SQLException(message, new IOException(message));
}
this.file = file;
splitFiles();
}
/**
* Split the file
*
* @throws IOException
*/
private void splitFiles() throws SQLException {
BufferedReader br = null;
BufferedWriter bwResultSet = null;
BufferedWriter bwCallableStatement = null;
try {
br = new BufferedReader(new FileReader(file));
callableStatementHolderFile = new File(file.toString() + ".1");
resultSetFile = new File(file.toString() + ".2");
bwResultSet = new BufferedWriter(new FileWriter(resultSetFile));
bwCallableStatement = new BufferedWriter(new FileWriter(callableStatementHolderFile));
boolean booleanfirstLineRead = false;
String line = null;
while ((line = br.readLine()) != null) {
line.trim();
if (!booleanfirstLineRead) {
bwCallableStatement.write(line + CR_LF);
booleanfirstLineRead = true;
} else {
bwResultSet.write(line + CR_LF);
}
}
} catch (Exception e) {
throw new SQLException(e);
} finally {
IOUtils.closeQuietly(br);
IOUtils.closeQuietly(bwResultSet);
IOUtils.closeQuietly(bwCallableStatement);
if (!DEBUG && ! KeepTempFilePolicyParms.KEEP_TEMP_FILE) {
this.file.delete();
}
}
}
/**
* Returns the ResultSet File
* @return the ResultSet File
*/
public File getResultSetFile() {
return resultSetFile;
}
/**
* @return the callableStatementHolderFile
*/
public File getCallableStatementHolderFile() {
return callableStatementHolderFile;
}
/**
* Displays the given message if DEBUG is set.
*
* @param s
* the debug message
*/
@SuppressWarnings("unused")
private void debug(String s) {
if (DEBUG) {
AwakeClientLogger.log(s);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy