Search in sources :

Example 1 with Statistics

use of alma.alarmsystem.statistics.generated.Statistics in project ACS by ACS-Community.

the class StatHashMap method openOutputFile.

/**
	 * Open and create the output stream for writing statistics on file.
	 * <P>
	 * The file is opened for each writing and closed immediately.
	 * A new file is created, whenever the size of the actual file is
	 * greater then {@link #maxFileSize}.
	 * 
	 * @return The stream for writing into the file
	 * @throws IOException If can't open/create the file for writing
	 * @throws ValidationException In case of error validating the Statistics element (should never happen)
	 * @throws MarshalException Error writing Statistics header in the file
	 */
private BufferedWriter openOutputFile() throws IOException, MarshalException, ValidationException {
    String actualFileName = fileNamePrefix + fileNumber + ".xml";
    String folderName = folder.getAbsolutePath();
    if (!folderName.endsWith("" + File.separator)) {
        folderName = folderName + File.separator;
    }
    // Check the size of the file if it exists
    File f = new File(folderName + actualFileName);
    if (f.exists() && f.length() > maxFileSize) {
        fileNumber++;
        return openOutputFile();
    }
    if (f.length() == 0) {
        // New file: write the header
        Statistics statistics = new Statistics();
        BufferedWriter outF = new BufferedWriter(new FileWriter(f, true));
        Marshaller marshaller = getMarshaller(outF);
        marshaller.setSupressXMLDeclaration(false);
        marshaller.marshal(statistics);
        outF.flush();
        outF.close();
        RandomAccessFile raf = new RandomAccessFile(f, "rw");
        raf.setLength(raf.length() - 3);
        raf.seek(raf.length());
        raf.writeBytes(">\n");
        raf.close();
    } else {
        // Remove the closing tag (closeXMLTag) before adding a new record
        RandomAccessFile raf = new RandomAccessFile(f, "rw");
        raf.setLength(raf.length() - closeXMLTag.length() - 1);
        raf.seek(raf.length());
        raf.writeBytes("\n");
        raf.close();
    }
    return new BufferedWriter(new FileWriter(f, true));
}
Also used : Marshaller(org.exolab.castor.xml.Marshaller) RandomAccessFile(java.io.RandomAccessFile) FileWriter(java.io.FileWriter) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Statistics(alma.alarmsystem.statistics.generated.Statistics) BufferedWriter(java.io.BufferedWriter)

Example 2 with Statistics

use of alma.alarmsystem.statistics.generated.Statistics in project ACS by ACS-Community.

the class StatsHashMapTest method dump.

/**
	 * Dump the content of the file of statistics on stdout
	 */
private void dump() throws Exception {
    BufferedReader reader = new BufferedReader(new FileReader(buildFileName()));
    String line = null;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
    reader.close();
    // Unmarshal the file to check if it is valid
    Statistics stats = Statistics.unmarshalStatistics(new FileReader(buildFileName()));
}
Also used : BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Statistics(alma.alarmsystem.statistics.generated.Statistics)

Aggregations

Statistics (alma.alarmsystem.statistics.generated.Statistics)2 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 RandomAccessFile (java.io.RandomAccessFile)1 Marshaller (org.exolab.castor.xml.Marshaller)1