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