src-main.org.awakefw.sql.jdbc.util.ResultSetFileSplitter 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
* ResultSet and and a ResultSet.getMetaData():
*
* The ResultSet part id before the line that contain the separator produced by
* host: br.write("ResultSet.getMetaData()" + CR_LF);
*
* The MetaData part is after that line.
*
* @author Nicolas de Pomereu
*
*/
public class ResultSetFileSplitter {
/** Debug flag */
private static boolean DEBUG = AwakeDebug.isSet(ResultSetFileSplitter.class);
/** Universal and clean line separator */
public static String CR_LF = System.getProperty("line.separator");
/** The file that contains all: ResultSet + MetaData */
private File rsAndMetaDataFile = null;
/** The file that contains only the ResultSet */
private File rsFile = null;
/** The file that contains only the ResultSet */
private File metaDataFile = null;
/**
* Constructor
*
* @param rsAndMetaDataFile
* The file that contains all: ResultSet + MetaData
*
*/
public ResultSetFileSplitter(File rsAndMetaDataFile) throws SQLException {
if (rsAndMetaDataFile == null) {
String message = Tag.AWAKE_PRODUCT_FAIL
+ "rsAndMetaDataFile can not be null!";
throw new SQLException(message, new IOException(message));
}
this.rsAndMetaDataFile = rsAndMetaDataFile;
splitFiles();
}
/**
* Split the file
*
* @throws IOException
*/
private void splitFiles() throws SQLException {
BufferedReader br = null;
BufferedWriter bwResultSet = null;
BufferedWriter bwMetaData = null;
try {
br = new BufferedReader(new FileReader(rsAndMetaDataFile));
rsFile = new File(rsAndMetaDataFile.toString() + ".1");
metaDataFile = new File(rsAndMetaDataFile.toString() + ".2");
bwResultSet = new BufferedWriter(new FileWriter(rsFile));
bwMetaData = new BufferedWriter(new FileWriter(metaDataFile));
boolean separatorReached = false;
String line = null;
while ((line = br.readLine()) != null) {
line.trim();
if (line.equals("ResultSet.getMetaData()")) {
separatorReached = true;
continue;
}
if (!separatorReached) {
bwResultSet.write(line + CR_LF);
} else {
bwMetaData.write(line + CR_LF);
}
}
} catch (Exception e) {
throw new SQLException(e);
} finally {
IOUtils.closeQuietly(br);
IOUtils.closeQuietly(bwResultSet);
IOUtils.closeQuietly(bwMetaData);
if (!DEBUG && ! KeepTempFilePolicyParms.KEEP_TEMP_FILE) {
this.rsAndMetaDataFile.delete();
}
}
}
/**
* Returns the ResultSet File
* @return the ResultSet File
*/
public File getResultSetFile() {
return rsFile;
}
/**
* Returns the ResultSet File
* @return the ResultSet File
*/
public File getMetaDataFile() {
return metaDataFile;
}
/**
* 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