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 | /**
|
---|
15 | * <p>
|
---|
16 | * This class provides the functionality to generate replay files from sequences
|
---|
17 | * if {@link ReplayableEvent}s.
|
---|
18 | * </p>
|
---|
19 | *
|
---|
20 | * @author Steffen Herbold
|
---|
21 | * @version 1.0
|
---|
22 | */
|
---|
23 | public class ReplayGenerator {
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * <p>
|
---|
27 | * {@link IReplayDecorator} to be used. If this field is {@code null}, no
|
---|
28 | * decorator is used. Default: {@code null}
|
---|
29 | * </p>
|
---|
30 | */
|
---|
31 | private IReplayDecorator decorator = null;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * <p>
|
---|
35 | * Id of the current session. The starting id is 1.
|
---|
36 | * </p>
|
---|
37 | */
|
---|
38 | int sessionId = 1;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * <p>
|
---|
42 | * Creates a replay file that contains multiple event sequences.
|
---|
43 | * </p>
|
---|
44 | *
|
---|
45 | * @param sequences
|
---|
46 | * collection of event sequences from which the sessions are
|
---|
47 | * generated
|
---|
48 | * @param filename
|
---|
49 | * name and path of the replay file
|
---|
50 | */
|
---|
51 | public void createLogfileMultipleSessions(
|
---|
52 | List<List<ReplayableEvent<?>>> sequences, String filename) {
|
---|
53 | OutputStreamWriter writer = openReplayFile(filename);
|
---|
54 | if (writer != null) {
|
---|
55 | try {
|
---|
56 | decorator = sequences.get(0).get(0).getReplayDecorator();
|
---|
57 | if (decorator != null) {
|
---|
58 | writer.write(decorator.getHeader());
|
---|
59 | }
|
---|
60 | for (List<ReplayableEvent<?>> actions : sequences) {
|
---|
61 | writeSession(actions, writer);
|
---|
62 | }
|
---|
63 | if (decorator != null) {
|
---|
64 | writer.write(decorator.getFooter());
|
---|
65 | }
|
---|
66 | decorator = null;
|
---|
67 | writer.close();
|
---|
68 | } catch (IOException e) {
|
---|
69 | Console.printerrln("Unable to write replay file " + filename);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * <p>
|
---|
76 | * Creates a replay file that from a single event sequence.
|
---|
77 | * </p>
|
---|
78 | *
|
---|
79 | * @param actions
|
---|
80 | * event sequence from which the sessions are generated
|
---|
81 | * @param filename
|
---|
82 | * name and path of the replay file
|
---|
83 | */
|
---|
84 | public void createLogfileSingleSession(List<ReplayableEvent<?>> actions,
|
---|
85 | String filename) {
|
---|
86 | OutputStreamWriter writer = openReplayFile(filename);
|
---|
87 | if (writer != null) {
|
---|
88 | try {
|
---|
89 | actions.get(0).getReplayDecorator();
|
---|
90 | if (decorator != null) {
|
---|
91 | writer.write(decorator.getHeader());
|
---|
92 | }
|
---|
93 | writeSession(actions, writer);
|
---|
94 | if (decorator != null) {
|
---|
95 | writer.write(decorator.getFooter());
|
---|
96 | }
|
---|
97 | decorator = null;
|
---|
98 | writer.close();
|
---|
99 | } catch (IOException e) {
|
---|
100 | Console.printerrln("Unable to write replay file " + filename);
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * <p>
|
---|
107 | * Helper function that opens the replay file for writing.
|
---|
108 | * </p>
|
---|
109 | *
|
---|
110 | * @param filename
|
---|
111 | * name and path of the replay file
|
---|
112 | * @return {@link OutputStreamWriter} that writes to the replay file
|
---|
113 | */
|
---|
114 | private OutputStreamWriter openReplayFile(String filename) {
|
---|
115 | File file = new File(filename);
|
---|
116 | boolean fileCreated;
|
---|
117 | try {
|
---|
118 | fileCreated = file.createNewFile();
|
---|
119 | if (!fileCreated) {
|
---|
120 | Console.traceln("Created logfile " + filename);
|
---|
121 | } else {
|
---|
122 | Console.traceln("Overwrote existing logfile " + filename);
|
---|
123 | }
|
---|
124 | } catch (IOException e) {
|
---|
125 | Console.printerrln("Unable to create file " + filename);
|
---|
126 | Console.printStacktrace(e);
|
---|
127 | }
|
---|
128 | OutputStreamWriter writer = null;
|
---|
129 | try {
|
---|
130 | writer = new OutputStreamWriter(new FileOutputStream(file),
|
---|
131 | "UTF-16");
|
---|
132 | } catch (IOException e) {
|
---|
133 | Console.printerrln("Unable to open file for writing (read-only file):"
|
---|
134 | + filename);
|
---|
135 | Console.printStacktrace(e);
|
---|
136 | }
|
---|
137 | return writer;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * <p>
|
---|
142 | * Helper function that adds an event sequence to the replay.
|
---|
143 | * </p>
|
---|
144 | *
|
---|
145 | * @param actions
|
---|
146 | * event sequences to be added
|
---|
147 | * @param writer
|
---|
148 | * {@link OutputStreamWriter} to which the replay is added
|
---|
149 | * @throws IOException
|
---|
150 | * thrown if there is a problem writing to writer
|
---|
151 | */
|
---|
152 | private void writeSession(List<ReplayableEvent<?>> actions,
|
---|
153 | OutputStreamWriter writer) throws IOException {
|
---|
154 | if (decorator != null) {
|
---|
155 | writer.write(decorator.getSessionHeader(sessionId));
|
---|
156 | }
|
---|
157 | for (ReplayableEvent<?> currentAction : actions) {
|
---|
158 |
|
---|
159 | List<? extends IReplayable> replayables = currentAction
|
---|
160 | .getReplayMessages();
|
---|
161 | for (IReplayable replayble : replayables) {
|
---|
162 | writer.write(replayble.getReplay() + StringTools.ENDLINE);
|
---|
163 | writer.flush();
|
---|
164 | }
|
---|
165 | }
|
---|
166 | if (decorator != null) {
|
---|
167 | writer.write(decorator.getSessionFooter(sessionId));
|
---|
168 | }
|
---|
169 | sessionId++;
|
---|
170 | }
|
---|
171 |
|
---|
172 | }
|
---|