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