1 | package de.ugoe.cs.eventbench;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileOutputStream;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.OutputStreamWriter;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import de.ugoe.cs.eventbench.data.IReplayable;
|
---|
10 | import de.ugoe.cs.eventbench.data.ReplayableEvent;
|
---|
11 | import de.ugoe.cs.util.StringTools;
|
---|
12 | import de.ugoe.cs.util.console.Console;
|
---|
13 |
|
---|
14 | public class ReplayGenerator {
|
---|
15 |
|
---|
16 | private IReplayDecorator decorator = null;
|
---|
17 |
|
---|
18 | public void createLogfileMultipleSessions(List<List<ReplayableEvent<?>>> sequences, String filename) {
|
---|
19 | OutputStreamWriter writer = openReplayFile(filename);
|
---|
20 | if( writer!=null ) {
|
---|
21 | try {
|
---|
22 | decorator = sequences.get(0).get(0).getReplayDecorator();
|
---|
23 | if( decorator!=null ) {
|
---|
24 | writer.write(decorator.getHeader());
|
---|
25 | }
|
---|
26 | for( List<ReplayableEvent<?>> actions : sequences ) {
|
---|
27 | writeSession(actions, writer);
|
---|
28 | }
|
---|
29 | if( decorator!=null ) {
|
---|
30 | writer.write(decorator.getFooter());
|
---|
31 | }
|
---|
32 | decorator = null;
|
---|
33 | writer.close();
|
---|
34 | } catch (IOException e) {
|
---|
35 | Console.printerrln("Unable to write replay file " + filename);
|
---|
36 | }
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public void createLogfileSingleSession(List<ReplayableEvent<?>> actions, String filename) {
|
---|
41 | OutputStreamWriter writer = openReplayFile(filename);
|
---|
42 | if( writer!=null ) {
|
---|
43 | try {
|
---|
44 | actions.get(0).getReplayDecorator();
|
---|
45 | if( decorator!=null ) {
|
---|
46 | writer.write(decorator.getHeader());
|
---|
47 | }
|
---|
48 | writeSession(actions, writer);
|
---|
49 | if( decorator!=null ) {
|
---|
50 | writer.write(decorator.getFooter());
|
---|
51 | }
|
---|
52 | decorator = null;
|
---|
53 | writer.close();
|
---|
54 | } catch (IOException e) {
|
---|
55 | Console.printerrln("Unable to write replay file " + filename);
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | private OutputStreamWriter openReplayFile(String filename) {
|
---|
61 | File file = new File(filename);
|
---|
62 | boolean fileCreated;
|
---|
63 | try {
|
---|
64 | fileCreated = file.createNewFile();
|
---|
65 | if( !fileCreated ) {
|
---|
66 | Console.traceln("Created logfile " + filename);
|
---|
67 | } else {
|
---|
68 | Console.traceln("Overwrote existing logfile " + filename);
|
---|
69 | }
|
---|
70 | } catch (IOException e) {
|
---|
71 | Console.printerrln("Unable to create file " + filename);
|
---|
72 | Console.printStacktrace(e);
|
---|
73 | }
|
---|
74 | OutputStreamWriter writer = null;
|
---|
75 | try {
|
---|
76 | writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-16");
|
---|
77 | } catch (IOException e) {
|
---|
78 | Console.printerrln("Unable to open file for writing (read-only file):" + filename);
|
---|
79 | Console.printStacktrace(e);
|
---|
80 | }
|
---|
81 | return writer;
|
---|
82 | }
|
---|
83 |
|
---|
84 | private void writeSession(List<ReplayableEvent<?>> actions, OutputStreamWriter writer)
|
---|
85 | throws IOException {
|
---|
86 | if( decorator!=null ) {
|
---|
87 | writer.write(decorator.getSessionHeader());
|
---|
88 | }
|
---|
89 | for( ReplayableEvent<?> currentAction : actions ) {
|
---|
90 |
|
---|
91 | List<? extends IReplayable> replayables = currentAction.getReplayMessages();
|
---|
92 | for( IReplayable replayble : replayables ) {
|
---|
93 | writer.write(replayble.getReplay()+StringTools.ENDLINE);
|
---|
94 | writer.flush();
|
---|
95 | }
|
---|
96 | }
|
---|
97 | if( decorator!=null ) {
|
---|
98 | writer.write(decorator.getSessionFooter());
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | }
|
---|