KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > model > log > CsvLogExporter


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.core.model.log;
15
16 import java.io.ByteArrayOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.PrintWriter JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 import org.apache.log4j.Logger;
23
24 public class CsvLogExporter implements ILogExporter {
25     
26     /** Log4j logging */
27     private static final Logger log = Logger.getLogger(CsvLogExporter.class);
28     
29     private Collection JavaDoc logentries = null;
30     private String JavaDoc signingCA = null;
31     
32     /**
33      * @see org.ejbca.core.model.log.ILogExporter
34      */

35     public void setEntries(Collection JavaDoc logentries) {
36         this.logentries = logentries;
37     }
38     
39     /**
40      * @see org.ejbca.core.model.log.ILogExporter
41      */

42     public int getNoOfEntries() {
43         if (logentries == null) {
44             return 0;
45         }
46         return logentries.size();
47     }
48
49     public String JavaDoc getSigningCA() {
50         return signingCA;
51     }
52     
53     public void setSigningCA(String JavaDoc ca) {
54         this.signingCA = ca;
55     }
56
57     /**
58      * @see org.ejbca.core.model.log.ILogExporter
59      */

60     public byte[] export() {
61         log.debug(">export");
62         byte[] ret = null;
63         if (logentries != null) {
64             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
65             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(baos);
66             try {
67                 Iterator JavaDoc i = logentries.iterator();
68                 while (i.hasNext()) {
69                     LogEntry next = (LogEntry)i.next();
70                     pw.print(next.getTime());
71                     pw.print("\t");
72                     pw.print(next.getAdminType());
73                     pw.print("\t");
74                     pw.print(next.getAdminData());
75                     pw.print("\t");
76                     pw.print(next.getCAId());
77                     pw.print("\t");
78                     pw.print(next.getModule());
79                     pw.print("\t");
80                     pw.print(next.getEvent());
81                     pw.print("\t");
82                     pw.print(next.getEventName());
83                     pw.print("\t");
84                     pw.print(next.getUsername());
85                     pw.print("\t");
86                     pw.print(next.getCertificateSNR());
87                     pw.print("\t");
88                     pw.print(next.getComment());
89                     pw.print("\t");
90                     pw.print(next.getVerifyResult());
91                     pw.print("\n");
92                 }
93                 pw.close();
94                 if (baos.size() > 0) {
95                     ret = baos.toByteArray();
96                 }
97             } finally {
98                 try {
99                     pw.close();
100                     baos.close();
101                 } catch (IOException JavaDoc e) {
102                     log.error("Error closing ByteArrayOutputStream: ", e);
103                 }
104             }
105         }
106         int no = getNoOfEntries();
107         log.debug("<export: "+no+" entries");
108         return ret;
109     }
110
111 }
112
Popular Tags