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

org.kawanfw.sql.jdbc.util.CallableResultFileSplitter Maven / Gradle / Ivy

/*
 * This file is part of AceQL. 
 * AceQL: Remote JDBC access over HTTP.                                     
 * Copyright (C) 2015,  KawanSoft SAS
 * (http://www.kawansoft.com). All rights reserved.                                
 *                                                                               
 * AceQL 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.            
 *                                                                               
 * AceQL 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.
 */
package org.kawanfw.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 java.util.logging.Level;

import org.apache.commons.io.IOUtils;
import org.kawanfw.commons.util.ClientLogger;
import org.kawanfw.commons.util.FrameworkDebug;
import org.kawanfw.commons.util.KeepTempFilePolicyParms;
import org.kawanfw.commons.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 = FrameworkDebug .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.PRODUCT_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) { ClientLogger.getLogger().log(Level.WARNING, s); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy