net.sf.blunder.service.persistence.opencsv.BlunderOpenCsvService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of blunder Show documentation
Show all versions of blunder Show documentation
Blunder is an automated tool for analyzing chained exceptions in Java. It's usefull for classify,
generate a customized error message and a list for possible solutions.
The aim of this project is to provide a tool to identify different error contexts, analyze them
and assemble a customized response to an application end-user or another application.
The newest version!
/*
* Blunder is an automated tool for analyzing chained exceptions.
* Copyright (C) 2009 - Ambrosi Lucas
*
* This file is part of Blunder.
*
* Blunder is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* version 3 as published by the Free Software Foundation.
*
* Blunder 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 Blunder. If not, see .
*/
package net.sf.blunder.service.persistence.opencsv;
import au.com.bytecode.opencsv.CSVWriter;
import au.com.bytecode.opencsv.bean.CsvToBean;
import au.com.bytecode.opencsv.bean.HeaderColumnNameMappingStrategy;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import net.sf.blunder.BlunderException;
import net.sf.blunder.model.ExceptionRelation;
import net.sf.blunder.service.persistence.InMemoryPersistenceService;
import org.apache.log4j.Logger;
public class BlunderOpenCsvService extends InMemoryPersistenceService {
private static final Logger log = Logger.getLogger( BlunderOpenCsvService.class );
/**
* OpenCSV implementation of the data writer.
* @see net.sf.blunder.service.persistence.InMemoryPersistenceService#writeData()
*/
@Override
public void writeData() {
try {
CSVWriter writer = new CSVWriter( getWriter() );
List beans = new ArrayList();
beans.addAll( getData().values() );
if ( log.isDebugEnabled() ) {
log.debug( "Writing relations [" + beans + "]" );
}
new BeanToCsv( true, false ).writeAllBeans( writer, beans, Object.class );
writer.close();
if ( log.isDebugEnabled() ) {
log.debug( "Done!" );
}
} catch ( Exception e ) {
log.error( "Error writing output file [" + e.getMessage() + "]" );
throw new BlunderException( "Error writing output file", e );
}
}
/**
* OpenCSV implementation of the data reader.
* @see net.sf.blunder.service.persistence.InMemoryPersistenceService#readData()
*/
@Override
public HashMap readData() {
CsvToBean csv = new CsvToBean();
Reader reader = getReader();
HashMap list = new HashMap();
try {
log.info( "Reading object storage..." );
log.debug( "Reading from [" + getFilePath() + "]" );
HeaderColumnNameMappingStrategy strat = new HeaderColumnNameMappingStrategy();
strat.setType( ExceptionRelation.class );
@SuppressWarnings("unchecked")
List objects = csv.parse( strat, reader );
for ( ExceptionRelation er : objects ) {
if ( log.isDebugEnabled() ) {
log.debug( "Hidratated [" + er + "]" );
}
list.put( er.getId(), er );
}
log.debug( "All objects hidratated from [" + getFilePath() + "]" );
return list;
} catch ( Exception e ) {
throw new BlunderException( "Error loading CSV data from [" + getFilePath() + "] " + e.toString(), e );
} finally {
if ( reader != null ) {
try { reader.close(); } catch ( IOException e ) {}
}
}
}
}