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