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