1 package org
.lwes
.journaller
;
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.
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
;
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
;
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() {
53 if (fileName
== null || "".equals(fileName
)) {
54 log
.error("File name was not specified");
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;
70 log
.debug("Opening file: " + fileName
);
72 in
= new DataInputStream(new GZIPInputStream(new FileInputStream(fileName
)));
75 in
= new DataInputStream(new FileInputStream(fileName
));
78 while ((evt
= EventHandlerUtil
.readEvent(in
, state
, evtTemplate
, validate
)) != null) {
83 catch (EOFException e
) {
84 // this is normal. Just catch and ignore.
87 log
.error(e
.getMessage(), e
);
94 catch (IOException e
) {
95 log
.error(e
.getMessage(), e
);
104 * This method is called after all events in the file have been processed.
107 if (log
.isDebugEnabled()) {
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
119 public void handleEvent(Event event
) {
120 System
.out
.println(event
.toOneLineString());
123 public static void main(String
[] args
) {
125 DeJournaller dj
= new DeJournaller();
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")) {
148 if (line
.hasOption("v") || line
.hasOption("validate")) {
149 dj
.setValidate(true);
154 catch (Exception e
) {
156 Runtime
.getRuntime().exit(1);
160 public String
getFileName() {
164 public void setFileName(String fileName
) {
165 this.fileName
= fileName
;
168 public String
getEsfFile() {
172 public void setEsfFile(String esfFile
) {
173 this.esfFile
= esfFile
;
176 public boolean isGzipped() {
180 public void setGzipped(boolean gzipped
) {
181 this.gzipped
= gzipped
;
184 public boolean isValidate() {
188 public void setValidate(boolean validate
) {
189 this.validate
= validate
;