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