[1] | 1 | package de.ugoe.cs.eventbench;
|
---|
| 2 |
|
---|
| 3 | import java.io.File;
|
---|
[44] | 4 | import java.io.FileOutputStream;
|
---|
[1] | 5 | import java.io.IOException;
|
---|
[44] | 6 | import java.io.OutputStreamWriter;
|
---|
[1] | 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 {
|
---|
[58] | 15 |
|
---|
| 16 | private IReplayDecorator decorator = null;
|
---|
| 17 |
|
---|
[1] | 18 | public void createLogfileMultipleSessions(List<List<ReplayableEvent<?>>> sequences, String filename) {
|
---|
[44] | 19 | OutputStreamWriter writer = openReplayFile(filename);
|
---|
[1] | 20 | if( writer!=null ) {
|
---|
| 21 | try {
|
---|
[58] | 22 | decorator = sequences.get(0).get(0).getReplayDecorator();
|
---|
| 23 | if( decorator!=null ) {
|
---|
| 24 | writer.write(decorator.getHeader());
|
---|
| 25 | }
|
---|
[1] | 26 | for( List<ReplayableEvent<?>> actions : sequences ) {
|
---|
| 27 | writeSession(actions, writer);
|
---|
| 28 | }
|
---|
[58] | 29 | if( decorator!=null ) {
|
---|
| 30 | writer.write(decorator.getFooter());
|
---|
| 31 | }
|
---|
| 32 | decorator = null;
|
---|
[1] | 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) {
|
---|
[44] | 41 | OutputStreamWriter writer = openReplayFile(filename);
|
---|
[1] | 42 | if( writer!=null ) {
|
---|
| 43 | try {
|
---|
[58] | 44 | actions.get(0).getReplayDecorator();
|
---|
| 45 | if( decorator!=null ) {
|
---|
| 46 | writer.write(decorator.getHeader());
|
---|
| 47 | }
|
---|
[1] | 48 | writeSession(actions, writer);
|
---|
[58] | 49 | if( decorator!=null ) {
|
---|
| 50 | writer.write(decorator.getFooter());
|
---|
| 51 | }
|
---|
| 52 | decorator = null;
|
---|
[1] | 53 | writer.close();
|
---|
| 54 | } catch (IOException e) {
|
---|
| 55 | Console.printerrln("Unable to write replay file " + filename);
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[44] | 60 | private OutputStreamWriter openReplayFile(String filename) {
|
---|
[1] | 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 | }
|
---|
[44] | 74 | OutputStreamWriter writer = null;
|
---|
[1] | 75 | try {
|
---|
[44] | 76 | writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-16");
|
---|
[1] | 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 |
|
---|
[44] | 84 | private void writeSession(List<ReplayableEvent<?>> actions, OutputStreamWriter writer)
|
---|
[1] | 85 | throws IOException {
|
---|
[58] | 86 | if( decorator!=null ) {
|
---|
| 87 | writer.write(decorator.getSessionHeader());
|
---|
| 88 | }
|
---|
[1] | 89 | for( ReplayableEvent<?> currentAction : actions ) {
|
---|
| 90 |
|
---|
| 91 | List<? extends IReplayable> replayables = currentAction.getReplayMessages();
|
---|
| 92 | for( IReplayable replayble : replayables ) {
|
---|
| 93 | writer.write(replayble.getReplayXml()+StringTools.ENDLINE);
|
---|
[58] | 94 | writer.flush();
|
---|
[1] | 95 | }
|
---|
| 96 | }
|
---|
[58] | 97 | if( decorator!=null ) {
|
---|
| 98 | writer.write(decorator.getSessionFooter());
|
---|
| 99 | }
|
---|
[1] | 100 | }
|
---|
| 101 |
|
---|
| 102 | }
|
---|