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

org.kawanfw.sql.jdbc.util.FileBackedListRs 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.File;
import java.sql.SQLException;
import java.util.AbstractList;
import java.util.List;
import java.util.logging.Level;

import org.kawanfw.commons.util.ClientLogger;
import org.kawanfw.commons.util.FrameworkDebug;
import org.kawanfw.sql.jdbc.ConnectionHttp;
import org.kawanfw.sql.json.StringListTransport;

/**
 * @author Nicolas de Pomereu
 * 
 *         a concrete List of objects backed on a file
 * 
 * @param 
 *            the type of object to back in a List
 */
public class FileBackedListRs extends AbstractList {

    /** Debug flag */
    private static boolean DEBUG = FrameworkDebug.isSet(FileBackedListRs.class);

    /** The RemoteConnection in use (for encryption) */
    private ConnectionHttp connectionHttp = null;

    /** The File Backed by a List of String */
    private FileBackedList fileBackList = null;

    /** The index to store for a line of List */
    private int storedIndex = -1;

    /** The line to store to avoid recalling Json costly creation */
    private List storedList;

    /**
     * Constructor with connection in order to decrypt
     * 
     * @param fileIn
     *            the file to read as a list of lines
     * @param connectionHttp
     *            connection in order to decrypt
     */
    public FileBackedListRs(File file, ConnectionHttp connectionHttp)
	    throws SQLException {

	if (connectionHttp == null) {
	    throw new IllegalArgumentException(
		    "connectionHttp can not be null!");
	}

	this.connectionHttp = connectionHttp;

	// Build a List backed by the file
	fileBackList = new FileBackedList(file);
    }

    @SuppressWarnings("unchecked")
    @Override
    public E get(int index) {

	if (fileBackList == null) {
	    throw new IllegalStateException(
		    "FileBaskListRs has been cleared. Can not be used anymore.");
	}

	// Get the sting line corresponding to the element in the list
	String line = fileBackList.get(index);

	// Return it as a List after decoding the Json string

	// We put in memory the line of the ResultSet,
	// because we may a have a lot of ResulSet.getXxx() for the same line
	// This avoids unnecessary recalling fromJson costly call.
	if (storedIndex == index) {
	    return (E) storedList;
	} else {
	    storedIndex = index;
	    storedList = fromJson(line);
	    return (E) storedList;
	}
    }

    /**
     * Return the current file line as list of strings
     * 
     * @param jsonString
     *            the current file line in Json format
     * @return current file line as list of strings
     */
    private List fromJson(String jsonString) {

	if (connectionHttp != null) {
	    try {
		jsonString = JsonLineDecryptor.decrypt(jsonString,
			connectionHttp);
	    } catch (SQLException e) {
		throw new IllegalArgumentException(e.getCause());
	    }
	}

	List line = StringListTransport.fromJson(jsonString);
	return line;
    }

    @Override
    public int size() {

	if (fileBackList == null) {
	    throw new IllegalStateException(
		    "FileBaskListRs has been cleared. Can not be used anymore.");
	}
	return fileBackList.size();
    }

    /**
     * closes the underlying FileBackList
     */
    @Override
    public void clear() {
	debug("clear called");
	fileBackList.clear();
	fileBackList = null;
    }

    /**
     * Displays the given message if DEBUG is set.
     * 
     * @param s
     *            the debug message
     */

    private void debug(String s) {
	if (DEBUG) {
	    ClientLogger.getLogger().log(Level.WARNING, s);
	}
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy