001: /*************************************************************************
002: * *
003: * EJBCA: The OpenSource Certificate Authority *
004: * *
005: * This software is free software; you can redistribute it and/or *
006: * modify it under the terms of the GNU Lesser General Public *
007: * License as published by the Free Software Foundation; either *
008: * version 2.1 of the License, or any later version. *
009: * *
010: * See terms of license at gnu.org. *
011: * *
012: *************************************************************************/package org.ejbca.core.model.log;
013:
014: import java.io.ByteArrayOutputStream;
015: import java.io.IOException;
016: import java.io.PrintWriter;
017: import java.util.Collection;
018: import java.util.Iterator;
019:
020: import org.apache.log4j.Logger;
021:
022: public class CsvLogExporter implements ILogExporter {
023:
024: /** Log4j logging */
025: private static final Logger log = Logger
026: .getLogger(CsvLogExporter.class);
027:
028: private Collection logentries = null;
029: private String signingCA = null;
030:
031: /**
032: * @see org.ejbca.core.model.log.ILogExporter
033: */
034: public void setEntries(Collection logentries) {
035: this .logentries = logentries;
036: }
037:
038: /**
039: * @see org.ejbca.core.model.log.ILogExporter
040: */
041: public int getNoOfEntries() {
042: if (logentries == null) {
043: return 0;
044: }
045: return logentries.size();
046: }
047:
048: public String getSigningCA() {
049: return signingCA;
050: }
051:
052: public void setSigningCA(String ca) {
053: this .signingCA = ca;
054: }
055:
056: /**
057: * @see org.ejbca.core.model.log.ILogExporter
058: */
059: public byte[] export() {
060: log.debug(">export");
061: byte[] ret = null;
062: if (logentries != null) {
063: ByteArrayOutputStream baos = new ByteArrayOutputStream();
064: PrintWriter pw = new PrintWriter(baos);
065: try {
066: Iterator i = logentries.iterator();
067: while (i.hasNext()) {
068: LogEntry next = (LogEntry) i.next();
069: pw.print(next.getTime());
070: pw.print("\t");
071: pw.print(next.getAdminType());
072: pw.print("\t");
073: pw.print(next.getAdminData());
074: pw.print("\t");
075: pw.print(next.getCAId());
076: pw.print("\t");
077: pw.print(next.getModule());
078: pw.print("\t");
079: pw.print(next.getEvent());
080: pw.print("\t");
081: pw.print(next.getEventName());
082: pw.print("\t");
083: pw.print(next.getUsername());
084: pw.print("\t");
085: pw.print(next.getCertificateSNR());
086: pw.print("\t");
087: pw.print(next.getComment());
088: pw.print("\t");
089: pw.print(next.getVerifyResult());
090: pw.print("\n");
091: }
092: pw.close();
093: if (baos.size() > 0) {
094: ret = baos.toByteArray();
095: }
096: } finally {
097: try {
098: pw.close();
099: baos.close();
100: } catch (IOException e) {
101: log.error("Error closing ByteArrayOutputStream: ",
102: e);
103: }
104: }
105: }
106: int no = getNoOfEntries();
107: log.debug("<export: " + no + " entries");
108: return ret;
109: }
110:
111: }
|