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