2560c746de0e1ae2011a04928d652ff723af7b19
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.control.ui.tests / stubs / org / eclipse / tracecompass / internal / lttng2 / control / stubs / shells / LTTngToolsFileShell.java
1 /**********************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 * Markus Schorn - Bug 448058: Use org.eclipse.remote in favor of RSE
12 **********************************************************************/
13 package org.eclipse.tracecompass.internal.lttng2.control.stubs.shells;
14
15 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.io.BufferedReader;
18 import java.io.DataInputStream;
19 import java.io.FileInputStream;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30
31 import org.eclipse.core.commands.ExecutionException;
32 import org.eclipse.core.runtime.IProgressMonitor;
33 import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandInput;
34 import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandResult;
35
36 @SuppressWarnings("javadoc")
37 public class LTTngToolsFileShell extends TestCommandShell {
38
39 // ------------------------------------------------------------------------
40 // CONSTANTS
41 // ------------------------------------------------------------------------
42 private final static String SCENARIO_KEY = "<SCENARIO>";
43 private final static String SCENARIO_END_KEY = "</SCENARIO>";
44 private final static String INPUT_KEY = "<COMMAND_INPUT>";
45 private final static String INPUT_END_KEY = "</COMMAND_INPUT>";
46 private final static String RESULT_KEY = "<COMMAND_RESULT>";
47 private final static String OUTPUT_KEY = "<COMMAND_OUTPUT>";
48 private final static String OUTPUT_END_KEY = "</COMMAND_OUTPUT>";
49 private final static String ERROR_OUTPUT_KEY = "<COMMAND_ERROR_OUTPUT>";
50 private final static String ERROR_OUTPUT_END_KEY = "</COMMAND_ERROR_OUTPUT>";
51 private final static String COMMENT_KEY = "#.*";
52
53 private final static Pattern LTTNG_LIST_SESSION_PATTERN = Pattern.compile("lttng\\s+list\\s+(.+)");
54 private final static String LTTNG_LIST_PROVIDER_PATTERN = "lttng\\s+list\\s+(-u|-k)";
55
56 // ------------------------------------------------------------------------
57 // Attributes
58 // ------------------------------------------------------------------------
59 private String fScenariofile;
60 private String fScenario;
61
62 private final Map<String, Map<String, ICommandResult>> fScenarioMap = new HashMap<>();
63 private final Map<String, Integer> fSessionNameMap = new HashMap<>();
64
65 /**
66 * Parse a scenario file with the format:
67 *
68 * <pre>
69 * &lt;SCENARIO&gt;
70 * ScenarioName
71 *
72 * &lt;COMMAND_INPUT&gt;
73 * Command
74 * &lt;/COMMAND_INPUT&gt;
75 *
76 * &lt;COMMAND_RESULT&gt;
77 * CommandResult
78 * &lt;/COMMAND_RESULT&gt;
79 *
80 * &lt;COMMAND_OUTPUT&gt;
81 * CommandOutput
82 * &lt;COMMAND_ERROR_OUTPUT&gt;
83 * CommandErrorOutput
84 * &lt;/COMMAND_ERROR_OUTPUT&gt;
85 * &lt;/COMMAND_OUTPUT&gt;
86 *
87 * &lt;/SCENARIO&gt;
88 *
89 * Where: ScenarioName - is the scenario name
90 * Command - the command line string
91 * CommandResult - the result integer of the command (0 for success, 1 for failure)
92 * CommandOutput - the command output string (multi-line possible)
93 * CommandErrorOutput - the command error output string (multi-line possible)
94 *
95 * Note: 1) There can be many scenarios per file
96 * 2) There can be many (Command-CommandResult-CommandOutput) triples per scenario
97 * 3) Lines starting with # will be ignored (comments)
98 *
99 * <pre>
100 * @param scenariofile - path to scenario file
101 */
102 public synchronized void loadScenarioFile(String scenariofile) {
103 fScenariofile = scenariofile;
104
105 // clean up map
106 Collection<Map<String, ICommandResult>> values = fScenarioMap.values();
107 for (Iterator<Map<String, ICommandResult>> iterator = values.iterator(); iterator.hasNext();) {
108 Map<String, ICommandResult> map = iterator.next();
109 map.clear();
110 }
111 fScenarioMap.clear();
112
113 // load from file
114
115 // Open the file
116 try (FileInputStream fstream = new FileInputStream(fScenariofile);
117 DataInputStream in = new DataInputStream(fstream);
118 BufferedReader br = new BufferedReader(new InputStreamReader(in));) {
119 String strLine;
120
121 // Read File Line by Line
122
123 // Temporary map for generating instance numbers for lttng list
124 // <session> commands.
125 // The numbers are per scenario.
126 Map<String, Integer> tmpSessionNameMap = new HashMap<>();
127 while ((strLine = br.readLine()) != null) {
128
129 // Ignore comments
130 if (isComment(strLine)) {
131 continue;
132 }
133
134 if (SCENARIO_KEY.equals(strLine)) {
135 // scenario start
136
137 // Ignore comments
138 strLine = br.readLine();
139 while (isComment(strLine)) {
140 strLine = br.readLine();
141 }
142
143 String scenario = strLine;
144 Map<String, ICommandResult> commandMap = new HashMap<>();
145 fScenarioMap.put(scenario, commandMap);
146 List<String> output = null;
147 List<String> errorOutput = null;
148 String input = null;
149 boolean inOutput = false;
150 boolean inErrorOutput = false;
151 int result = 0;
152 tmpSessionNameMap.clear();
153 while ((strLine = br.readLine()) != null) {
154 // Ignore comments
155 if (isComment(strLine)) {
156 continue;
157 }
158
159 if (SCENARIO_END_KEY.equals(strLine)) {
160 // Scenario is finished
161 break;
162 }
163 if (INPUT_KEY.equals(strLine)) {
164 strLine = br.readLine();
165 // Ignore comments
166 while (isComment(strLine)) {
167 strLine = br.readLine();
168 }
169 // Read command
170 input = strLine;
171
172 // Handle instances of 'lttng list
173 // <session"-command
174 Matcher matcher = LTTNG_LIST_SESSION_PATTERN.matcher(strLine);
175 if (matcher.matches() && !input.matches(LTTNG_LIST_PROVIDER_PATTERN)) {
176 String sessionName = matcher.group(1).trim();
177 Integer i = tmpSessionNameMap.get(sessionName);
178 if (i != null) {
179 i++;
180 } else {
181 i = 0;
182 }
183 tmpSessionNameMap.put(sessionName, i);
184 input += String.valueOf(i);
185 }
186 } else if (INPUT_END_KEY.equals(strLine)) {
187 // Initialize output array
188 output = new ArrayList<>();
189 errorOutput = new ArrayList<>();
190 } else if (RESULT_KEY.equals(strLine)) {
191 strLine = br.readLine();
192 // Ignore comments
193 while (isComment(strLine)) {
194 strLine = br.readLine();
195 }
196 // Save result value
197 result = Integer.parseInt(strLine);
198 } else if (OUTPUT_END_KEY.equals(strLine)) {
199 // Save output/result in command map
200 if (output != null && errorOutput != null) {
201 commandMap.put(input, new CommandResultStub(result,
202 checkNotNull(output.toArray(new String[output.size()])),
203 checkNotNull(errorOutput.toArray(new String[errorOutput.size()]))));
204 }
205 inOutput = false;
206 } else if (OUTPUT_KEY.equals(strLine)) {
207 // first line of output
208 inOutput = true;
209 } else if (ERROR_OUTPUT_KEY.equals(strLine)) {
210 // first line of output
211 inErrorOutput = true;
212 } else if (ERROR_OUTPUT_END_KEY.equals(strLine)) {
213 inErrorOutput = false;
214 } else if (inOutput) {
215 while (isComment(strLine)) {
216 strLine = br.readLine();
217 }
218 // lines of output/error output
219 if (errorOutput != null && inErrorOutput) {
220 errorOutput.add(strLine);
221 } else if (output != null) {
222 output.add(strLine);
223 }
224 }
225 // else {
226 // if (RESULT_END_KEY.equals(strLine)) {
227 // nothing to do
228 // }
229 }
230 }
231 }
232 } catch (IOException e) {
233 e.printStackTrace();
234 }
235 }
236
237 // Set the scenario to consider in executeCommand()
238 public synchronized void setScenario(String scenario) {
239 fScenario = scenario;
240 fSessionNameMap.clear();
241 if (!fScenarioMap.containsKey(fScenario)) {
242 throw new IllegalArgumentException();
243 }
244 }
245
246 @Override
247 public synchronized ICommandResult executeCommand(ICommandInput command, IProgressMonitor monitor) throws ExecutionException {
248 Map<String, ICommandResult> commands = fScenarioMap.get(fScenario);
249 String commandLine = command.toString();
250 String fullCommand = commandLine;
251
252 Matcher matcher = LTTNG_LIST_SESSION_PATTERN.matcher(commandLine);
253 if (matcher.matches() && !commandLine.matches(LTTNG_LIST_PROVIDER_PATTERN)) {
254 String sessionName = matcher.group(1).trim();
255 Integer i = fSessionNameMap.get(sessionName);
256 if (i != null) {
257 i++;
258 } else {
259 i = 0;
260 }
261 fSessionNameMap.put(sessionName, i);
262 fullCommand += String.valueOf(i);
263 }
264
265 if (commands.containsKey(fullCommand)) {
266 return checkNotNull(commands.get(fullCommand));
267 }
268
269 String[] output = new String[1];
270 output[0] = String.valueOf("Command not found");
271 CommandResultStub result = new CommandResultStub(1, output, output);
272 return result;
273 }
274
275 // ------------------------------------------------------------------------
276 // Helper methods
277 // ------------------------------------------------------------------------
278
279 private static boolean isComment(String line) {
280 if (line == null) {
281 throw new RuntimeException("line is null");
282 }
283 return line.matches(COMMENT_KEY);
284 }
285 }
This page took 0.037402 seconds and 4 git commands to generate.