Package it.unina.seclab.jafimon

Source Code of it.unina.seclab.jafimon.DataCollection

package it.unina.seclab.jafimon;

import it.unina.seclab.jafimon.exceptions.CannotCreateDataCollection;
import it.unina.seclab.jafimon.exceptions.CannotFindDataCollection;
import it.unina.seclab.jafimon.exceptions.CannotWriteDataCollection;
import it.unina.seclab.jafimon.interfaces.IDataCollection;
import it.unina.seclab.jafimon.interfaces.IMonitoredData;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.log4j.Logger;

/**
* Questa classe incapsula il comportamento del gestore di una
* collezione di <code>MonitoreData</code>.
*
* @author   Mauro Iorio
*
*/
public class DataCollection implements IDataCollection {
 
  private File dataFile;
 
  private FileWriter writer;
 
  private FileReader reader;
 
  private static final String newLine = "\r\n";

  private static final Logger logger = Logger.getRootLogger();
 
  /**
   * Legge la prossima riga di dati dalla collection
   *
   * @return la riga letta
   */
  private String readLn() {
    StringBuffer s = new StringBuffer();
    int c;
    try {
      do {
        c = reader.read();
        if (c > -1) s.append((char)c);
      } while ( (c != newLine.charAt(0)) && (c != -1) );

    } catch (IOException e) {
      logger.error("Error while reading from \"" + dataFile.getName() + "\". Error message is \"" + e.getLocalizedMessage() + "\"");
    }
   
    return s.toString();
  }
 
  /**
   * Crea l'oggetto <code>DataCollection</code> con il nome indicato.
   * Il nome della DataCollection nella corrente implementazione si riferisce
   * al nome del file dove sono memorizzati i dati rilevati
   * 
   * @param name il nome della <code>DataCollection</code> da creare
   * @throws CannotCreateDataCollection
   */
  public DataCollection(String name) throws CannotCreateDataCollection {
    dataFile = new File(name);
    if (dataFile.exists()) {
      logger.error("Cannot create data collection \"" + name + "\". Error message is \"File already exists\"");
      throw new CannotCreateDataCollection(name,"File already exists");
    }
    try {
      writer = new FileWriter(dataFile);
      reader = new FileReader(dataFile);
    } catch (IOException e) {
      logger.error("Cannot create data collection \"" + name + "\". Error message is \"IOException: " + e.getLocalizedMessage() + "\"");
      throw new CannotCreateDataCollection(name,"IOException: \"" + e.getLocalizedMessage() + "\"");
    }
  }

  protected DataCollection() {
    dataFile = null;
    writer = null;
    reader = null;
  }

  /**
   * Aggiunge a questa <code>DataCollection</code> il dato passato
   * come parametro
   *
   * @param data il dato da memorizzare
   */
  public void appendData(IMonitoredData data) throws CannotWriteDataCollection {
    try {
      writer.write(data.toString() + newLine);
      writer.flush();
    } catch (IOException e) {
      logger.error("Cannot write to data collection \"" + dataFile.getName() + "\". Error message is \"IOException: " + e.getLocalizedMessage() + "\"");
      throw new CannotWriteDataCollection(dataFile.getName(),"IOException: " + e.getLocalizedMessage());
    }
  }

  /**
   * Recupera il primo dato memorizzato in questa <code>DataCollection</code>
   */
  public String getFirstData() {
    try {
      reader.reset();
    } catch (IOException e) {
      logger.error("Error while resetting file \"" + dataFile.getName() + "\". Error message is \"" + e.getLocalizedMessage() + "\"");
    }

    return getNextData();
  }

  /**
   * Recupera da questa <code>DataCollection</code> il dato memorizzato
   * successivo a quello corrente
   */
  public String getNextData() {
    return readLn();
  }

  /**
   * Recupera il nome di questa <code>DataCollection</code>
   */
  public String getCollectionName() {
    return dataFile.getName();
  }

  /**
   * Istanzia un nuovo oggetto <code>DataCollection</code> aprendo la collection
   * individuata dal nome passato come parametro. La <code>DataCollection</code>
   * da aprire deve esistere.
   *
   * @param name il nome della <code>DataCollection</code> da aprire
   * @return la collection aperta
   * @throws CannotFindDataCollection se la <code>DataCollection</code> non esiste
   * @throws CannotCreateDataCollection
   */
  public static DataCollection open(String name) throws   CannotFindDataCollection,
                              CannotCreateDataCollection {
    DataCollection res = new DataCollection();
    res.dataFile = new File(name);
    if (! res.dataFile.exists()) {
      logger.error("Cannot open data collection \"" + name + "\". Error message is \"File does not exists\"");
      throw new CannotFindDataCollection(name);
    }
    try {
      res.reader = new FileReader(res.dataFile);
    } catch (IOException e) {
      logger.error("Cannot create data collection \"" + name + "\". Error message is \"IOException: " + e.getLocalizedMessage() + "\"");
      throw new CannotCreateDataCollection(name,"IOException: \"" + e.getLocalizedMessage() + "\"");
    }
   
    return res;
  }
}
TOP

Related Classes of it.unina.seclab.jafimon.DataCollection

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.