3f262b6c04e324cad5b96fafd551c5e4633ccb74
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.control.ui.tests / stubs / org / eclipse / linuxtools / 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 **********************************************************************/
12 package org.eclipse.linuxtools.internal.lttng2.control.stubs.shells;
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;
29 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.remote.CommandResult;
30 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.remote.ICommandResult;
31
32 @SuppressWarnings("javadoc")
33 public class LTTngToolsFileShell extends TestCommandShell {
34
35 // ------------------------------------------------------------------------
36 // CONSTANTS
37 // ------------------------------------------------------------------------
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>";
43 private final static String OUTPUT_KEY = "<COMMAND_OUTPUT>";
44 private final static String OUTPUT_END_KEY = "</COMMAND_OUTPUT>";
45 private final static String ERROR_OUTPUT_KEY = "<COMMAND_ERROR_OUTPUT>";
46 private final static String ERROR_OUTPUT_END_KEY = "</COMMAND_ERROR_OUTPUT>";
47 private final static String COMMENT_KEY = "#.*";
48
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)";
51
52 // ------------------------------------------------------------------------
53 // Attributes
54 // ------------------------------------------------------------------------
55 private String fScenariofile;
56 private String fScenario;
57
58 private final Map<String, Map<String, ICommandResult>> fScenarioMap = new HashMap<>();
59 private final Map<String, Integer> fSessionNameMap = new HashMap<>();
60
61 /**
62 * Parse a scenario file with the format:
63 * <pre>
64 * &lt;SCENARIO&gt;
65 * ScenarioName
66 *
67 * &lt;COMMAND_INPUT&gt;
68 * Command
69 * &lt;/COMAND_INPUT&gt;
70 *
71 * &lt;COMMAND_RESULT&gt;
72 * CommandResult
73 * &lt;/COMMAND_RESULT&gt;
74 *
75 * &lt;COMMAND_OUTPUT&gt;
76 * CommandOutput
77 * &lt;COMMAND_ERROR_OUTPUT&gt;
78 * CommandErrorOutput
79 * &lt;/COMMAND_ERROR_OUTPUT&gt;
80 * &lt;/COMMAND_OUTPUT&gt;
81 *
82 * &lt;/SCENARIO&gt;
83 *
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)
88 * ComandErrorOutput - the command error output string (multi-line possible)
89 *
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)
93 * <pre>
94 * @param scenariofile - path to scenario file
95 * @throws Exception
96 */
97 public synchronized void loadScenarioFile(String scenariofile) throws Exception {
98 fScenariofile = scenariofile;
99
100 // clean up map
101 Collection<Map<String, ICommandResult>> values = fScenarioMap.values();
102 for (Iterator<Map<String, ICommandResult>> iterator = values.iterator(); iterator.hasNext();) {
103 Map<String, ICommandResult> map = iterator.next();
104 map.clear();
105 }
106 fScenarioMap.clear();
107
108 // load from file
109
110 // Open the file
111 try (FileInputStream fstream = new FileInputStream(fScenariofile);
112 DataInputStream in = new DataInputStream(fstream);
113 BufferedReader br = new BufferedReader(new InputStreamReader(in));) {
114 String strLine;
115
116 // Read File Line by Line
117
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) {
123
124 // Ignore comments
125 if (isComment(strLine)) {
126 continue;
127 }
128
129 if (SCENARIO_KEY.equals(strLine)) {
130 // scenario start
131
132 // Ignore comments
133 strLine = br.readLine();
134 while (isComment(strLine)) {
135 strLine = br.readLine();
136 }
137
138 String scenario = strLine;
139 Map<String, ICommandResult> commandMap = new HashMap<>();
140 fScenarioMap.put(scenario, commandMap);
141 List<String> output = null;
142 List<String> errorOutput = null;
143 String input = null;
144 boolean inOutput = false;
145 boolean inErrorOutput = false;
146 int result = 0;
147 tmpSessionNameMap.clear();
148 while ((strLine = br.readLine()) != null) {
149 // Ignore comments
150 if (isComment(strLine)) {
151 continue;
152 }
153
154 if (SCENARIO_END_KEY.equals(strLine)) {
155 // Scenario is finished
156 break;
157 }
158 if (INPUT_KEY.equals(strLine)) {
159 strLine = br.readLine();
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<>();
184 errorOutput = new ArrayList<>();
185 } else if (RESULT_KEY.equals(strLine)) {
186 strLine = br.readLine();
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
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()])));
197 }
198 inOutput = false;
199 } else if (OUTPUT_KEY.equals(strLine)) {
200 // first line of output
201 inOutput = true;
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) {
208 while (isComment(strLine)) {
209 strLine = br.readLine();
210 }
211 // lines of output/error output
212 if (errorOutput != null && inErrorOutput) {
213 errorOutput.add(strLine);
214 } else if (output != null) {
215 output.add(strLine);
216 }
217 }
218 // else {
219 // if (RESULT_END_KEY.equals(strLine)) {
220 // nothing to do
221 // }
222 }
223 }
224 }
225 }
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
237 @Override
238 public synchronized ICommandResult executeCommand(String command, IProgressMonitor monitor, boolean checkReturnValue) throws ExecutionException {
239 Map<String, ICommandResult> commands = fScenarioMap.get(fScenario);
240 String fullCommand = command;
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);
252 fullCommand += String.valueOf(i);
253 }
254
255 if (commands.containsKey(fullCommand)) {
256 return commands.get(fullCommand);
257 }
258
259 String[] output = new String[1];
260 output[0] = String.valueOf("Command not found");
261 CommandResult result = new CommandResult(0, null, null);
262 // For verification of setters of class CommandResult
263 result.setOutput(output);
264 result.setErrorOutput(output);
265 result.setResult(1);
266 return result;
267 }
268
269 // ------------------------------------------------------------------------
270 // Helper methods
271 // ------------------------------------------------------------------------
272
273 private static boolean isComment(String line) {
274 if (line == null) {
275 throw new RuntimeException("line is null");
276 }
277 return line.matches(COMMENT_KEY);
278 }
279 }
This page took 0.039791 seconds and 4 git commands to generate.