Changed how events are output
[lwes-journaller-java.git] / src / main / java / org / lwes / journaller / DeJournaller.java
blobe32e2fc401c6c965c383d82825a91163000b4669
1 package org.lwes.journaller;
2 /**
3 * A Class that will "dejournal" files written to by the GZIPEventHandler. If you want to
4 * do something other than write the events to stdout, subclass this and override handleEvent.
6 * User: fmaritato
7 * Date: Apr 16, 2009
8 */
10 import org.apache.commons.cli.CommandLine;
11 import org.apache.commons.cli.CommandLineParser;
12 import org.apache.commons.cli.HelpFormatter;
13 import org.apache.commons.cli.Options;
14 import org.apache.commons.cli.PosixParser;
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.lwes.Event;
18 import org.lwes.db.EventTemplateDB;
19 import org.lwes.journaller.util.EventHandlerUtil;
20 import org.lwes.serializer.DeserializerState;
22 import java.io.DataInputStream;
23 import java.io.EOFException;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.util.zip.GZIPInputStream;
29 public class DeJournaller implements Runnable, JournallerConstants {
30 private transient Log log = LogFactory.getLog(DeJournaller.class);
32 private boolean gzipped;
33 private String fileName;
34 private String esfFile;
35 private boolean validate = false;
37 protected static Options options;
39 static {
40 options = new Options();
41 options.addOption("f", "file", true, "File to read events from.");
42 options.addOption("e", "esf-file", true, "Event definition file.");
43 options.addOption("h", "help", false, "Print this message.");
44 options.addOption("g", "gzipped", false, "Event log is gzipped.");
45 options.addOption("v", "validate", false, "Validate events");
48 public DeJournaller() {
51 public void run() {
53 if (fileName == null || "".equals(fileName)) {
54 log.error("File name was not specified");
55 return;
58 EventTemplateDB evtTemplate = new EventTemplateDB();
59 DeserializerState state = new DeserializerState();
61 if (getEsfFile() != null) {
62 evtTemplate.setESFFile(new File(getEsfFile()));
64 log.debug("esf: " + evtTemplate.getESFFile());
65 log.debug("validate: " + validate);
66 evtTemplate.initialize();
68 DataInputStream in = null;
69 try {
70 log.debug("Opening file: " + fileName);
71 if (gzipped) {
72 in = new DataInputStream(new GZIPInputStream(new FileInputStream(fileName)));
74 else {
75 in = new DataInputStream(new FileInputStream(fileName));
77 Event evt;
78 while ((evt = EventHandlerUtil.readEvent(in, state, evtTemplate, validate)) != null) {
79 state.reset();
80 handleEvent(evt);
83 catch (EOFException e) {
84 // this is normal. Just catch and ignore.
86 catch (Exception e) {
87 log.error(e.getMessage(), e);
89 finally {
90 if (in != null) {
91 try {
92 in.close();
94 catch (IOException e) {
95 log.error(e.getMessage(), e);
98 done();
104 * This method is called after all events in the file have been processed.
106 public void done() {
107 if (log.isDebugEnabled()) {
108 log.debug("done");
113 * Subclass and override this method if you want to do something other than print the
114 * events to the logging system. For example, you may want to write these events to a
115 * database.
117 * @param event
119 public void handleEvent(Event event) {
120 System.out.println(event.toOneLineString());
123 public static void main(String[] args) {
125 DeJournaller dj = new DeJournaller();
126 try {
127 CommandLineParser parser = new PosixParser();
128 CommandLine line = parser.parse(options, args);
130 if (line.hasOption("h") || line.hasOption("help")) {
131 HelpFormatter formatter = new HelpFormatter();
132 formatter.printHelp("dejournaller", options);
133 Runtime.getRuntime().exit(1);
135 if (line.hasOption("f") || line.hasOption("file")) {
136 dj.setFileName(line.getOptionValue("f") == null ?
137 line.getOptionValue("file") :
138 line.getOptionValue("f"));
140 if (line.hasOption("e") || line.hasOption("esf-file")) {
141 dj.setEsfFile(line.getOptionValue("e") == null ?
142 line.getOptionValue("esf-file") :
143 line.getOptionValue("e"));
145 if (line.hasOption("g") || line.hasOption("gzipped")) {
146 dj.setGzipped(true);
148 if (line.hasOption("v") || line.hasOption("validate")) {
149 dj.setValidate(true);
152 dj.run();
154 catch (Exception e) {
155 e.printStackTrace();
156 Runtime.getRuntime().exit(1);
160 public String getFileName() {
161 return fileName;
164 public void setFileName(String fileName) {
165 this.fileName = fileName;
168 public String getEsfFile() {
169 return esfFile;
172 public void setEsfFile(String esfFile) {
173 this.esfFile = esfFile;
176 public boolean isGzipped() {
177 return gzipped;
180 public void setGzipped(boolean gzipped) {
181 this.gzipped = gzipped;
184 public boolean isValidate() {
185 return validate;
188 public void setValidate(boolean validate) {
189 this.validate = validate;