package cl.alma.camel.acslog;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.math.BigInteger;
import org.apache.camel.Processor;
import org.apache.commons.io.FilenameUtils;
import alma.acs.logging.engine.io.IOHelper;
import alma.acs.logging.engine.io.IOPorgressListener;
import com.cosylab.logging.engine.ACS.ACSRemoteRawLogListener;
/**
*
* @author atejeda
*
*/
public class ACSLogConsumerXML extends ACSLogConsumer implements ACSRemoteRawLogListener, IOPorgressListener {
private final ACSLogEndpoint endpoint;
private BigInteger log_preuid = null;
private BigInteger log_parsed = null;
private int files_read = 0;
private String xmlPath = null;
private File xmlDir = null;
private IOHelper ioHelper = null;
/**
* Constructor, set the xml directory path from the endpoint options,
* @param endpoint
* @param processor
*/
public ACSLogConsumerXML(ACSLogEndpoint endpoint, Processor processor) {
super(endpoint, processor);
this.endpoint = endpoint;
this.log_preuid = BigInteger.ZERO;
this.log_parsed= BigInteger.ZERO;
this.xmlPath = this.endpoint.getXml();
}
/**
* Start the common validation files and reading process
* of the log, if is a file, or the logs located at that directory.
*
* Invoke {@link #doStop()} whe all logs files were processed.
*/
@Override
protected void doStart() throws Exception {
log.info("Using directory ", this.xmlPath);
this.xmlDir = new File(this.xmlPath);
if(!this.xmlDir.canRead() || !this.xmlDir.exists()) {
log.info("Can't read " + this.xmlPath + ", exists?");
} else if(this.xmlDir.isFile()) {
this.readLog(this.xmlDir);
} else {
this.readLogs();
}
log.info("*** all xml log files were read ***");
log.info("logs file read :" + this.files_read);
}
/**
* If the endpoint option were a directory, it will start reading
* all logs from that directory with an .xml extension.
*/
public void readLogs() {
try {
for(File nfile : this.xmlDir.listFiles(this.xmlFileFilter())) {
this.readLog(nfile);
}
} catch (Exception e) {
log.error("Can't read the file..." + e.toString());
}
}
/**
* Using ACS (ALMA Common Software) loggin api, it will start reading
* the log xml file.
* @param logFile the xml log to read.
*/
public void readLog(File logFile) {
this.files_read = 0;
this.log_preuid = BigInteger.ZERO;
this.log_parsed = BigInteger.ZERO;
log.info("reading " + logFile.getAbsolutePath());
try {
BufferedReader bufferReader = new BufferedReader(new FileReader(logFile));
this.ioHelper = new IOHelper();
this.ioHelper.setDiscardLevel(this.getLogDiscardLevel());
this.ioHelper.setAudience(this.getAudience());
this.ioHelper.loadLogs(bufferReader, this, this, this, this);
this.files_read++;
} catch (FileNotFoundException e) {
log.error(e.getMessage());
} catch (Exception e) {
log.error(e.getMessage());
}
log.info("logs read : " + this.log_preuid.toString());
log.info("logs parsed : " + this.log_parsed.toString());
}
/**
* It's an anonymous implementation for filtering
* xml files with xml entension.
* @return a file filter for xml file extension.
*/
public FileFilter xmlFileFilter() {
return new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isFile() &&
FilenameUtils.isExtension(pathname.getName(), "xml");
}
};
}
/**
* Not implemented and used, forced by the observer interface.
*/
@Override
public void logsRead(int numOfLogs) {}
/**
* Not implemented and used, forced by the observer interface.
*/
@Override
public void xmlEntryReceived(String xmlLogString) {}
/**
* Not implemented and used, forced by the observer interface.
*/
@Override
public void logsWritten(int numOfLogs) {}
/**
* Not implemented and used, forced by the observer interface.
*/
@Override
public void errorReceived(String xml) {}
/**
* Not implemented and used, forced by the observer interface.
*/
@Override
public void bytesRead(long nBytes) {}
/**
* Not implemented and used, forced by the observer interface.
*/
@Override
public void bytesWritten(long nBytes) {}
}