1 /**********************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
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
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
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import java
.io
.BufferedReader
;
18 import java
.io
.DataInputStream
;
19 import java
.io
.FileInputStream
;
20 import java
.io
.IOException
;
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
;
28 import java
.util
.regex
.Matcher
;
29 import java
.util
.regex
.Pattern
;
31 import org
.eclipse
.core
.commands
.ExecutionException
;
32 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
33 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.core
.stubs
.shells
.TestCommandShell
;
34 import org
.eclipse
.tracecompass
.tmf
.remote
.core
.shell
.ICommandInput
;
35 import org
.eclipse
.tracecompass
.tmf
.remote
.core
.shell
.ICommandResult
;
37 @SuppressWarnings("javadoc")
38 public class LTTngToolsFileShell
extends TestCommandShell
{
40 // ------------------------------------------------------------------------
42 // ------------------------------------------------------------------------
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>";
48 private final static String OUTPUT_KEY
= "<COMMAND_OUTPUT>";
49 private final static String OUTPUT_END_KEY
= "</COMMAND_OUTPUT>";
50 private final static String ERROR_OUTPUT_KEY
= "<COMMAND_ERROR_OUTPUT>";
51 private final static String ERROR_OUTPUT_END_KEY
= "</COMMAND_ERROR_OUTPUT>";
52 private final static String COMMENT_KEY
= "#.*";
54 private final static Pattern LTTNG_LIST_SESSION_PATTERN
= Pattern
.compile("lttng\\s+list\\s+(.+)");
55 private final static String LTTNG_LIST_PROVIDER_PATTERN
= "lttng\\s+list\\s+(-u|-k)";
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
60 private String fScenariofile
;
61 private String fScenario
;
63 private final Map
<String
, Map
<String
, ICommandResult
>> fScenarioMap
= new HashMap
<>();
64 private final Map
<String
, Integer
> fSessionNameMap
= new HashMap
<>();
67 * Parse a scenario file with the format:
73 * <COMMAND_INPUT>
75 * </COMMAND_INPUT>
77 * <COMMAND_RESULT>
79 * </COMMAND_RESULT>
81 * <COMMAND_OUTPUT>
83 * <COMMAND_ERROR_OUTPUT>
85 * </COMMAND_ERROR_OUTPUT>
86 * </COMMAND_OUTPUT>
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)
93 * CommandOutput - the command output string (multi-line possible)
94 * CommandErrorOutput - the command error output string (multi-line possible)
96 * Note: 1) There can be many scenarios per file
97 * 2) There can be many (Command-CommandResult-CommandOutput) triples per scenario
98 * 3) Lines starting with # will be ignored (comments)
101 * @param scenariofile - path to scenario file
103 public synchronized void loadScenarioFile(String scenariofile
) {
104 fScenariofile
= scenariofile
;
107 Collection
<Map
<String
, ICommandResult
>> values
= fScenarioMap
.values();
108 for (Iterator
<Map
<String
, ICommandResult
>> iterator
= values
.iterator(); iterator
.hasNext();) {
109 Map
<String
, ICommandResult
> map
= iterator
.next();
112 fScenarioMap
.clear();
117 try (FileInputStream fstream
= new FileInputStream(fScenariofile
);
118 DataInputStream in
= new DataInputStream(fstream
);
119 BufferedReader br
= new BufferedReader(new InputStreamReader(in
));) {
122 // Read File Line by Line
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) {
131 if (isComment(strLine
)) {
135 if (SCENARIO_KEY
.equals(strLine
)) {
139 strLine
= br
.readLine();
140 while (isComment(strLine
)) {
141 strLine
= br
.readLine();
144 String scenario
= strLine
;
145 Map
<String
, ICommandResult
> commandMap
= new HashMap
<>();
146 fScenarioMap
.put(scenario
, commandMap
);
147 List
<String
> output
= null;
148 List
<String
> errorOutput
= null;
150 boolean inOutput
= false;
151 boolean inErrorOutput
= false;
153 tmpSessionNameMap
.clear();
154 while ((strLine
= br
.readLine()) != null) {
156 if (isComment(strLine
)) {
160 if (SCENARIO_END_KEY
.equals(strLine
)) {
161 // Scenario is finished
164 if (INPUT_KEY
.equals(strLine
)) {
165 strLine
= br
.readLine();
167 while (isComment(strLine
)) {
168 strLine
= br
.readLine();
173 // Handle instances of 'lttng list
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
);
184 tmpSessionNameMap
.put(sessionName
, i
);
185 input
+= String
.valueOf(i
);
187 } else if (INPUT_END_KEY
.equals(strLine
)) {
188 // Initialize output array
189 output
= new ArrayList
<>();
190 errorOutput
= new ArrayList
<>();
191 } else if (RESULT_KEY
.equals(strLine
)) {
192 strLine
= br
.readLine();
194 while (isComment(strLine
)) {
195 strLine
= br
.readLine();
198 result
= Integer
.parseInt(strLine
);
199 } else if (OUTPUT_END_KEY
.equals(strLine
)) {
200 // Save output/result in command map
201 if (output
!= null && errorOutput
!= null) {
202 commandMap
.put(input
, createCommandResult(result
,
203 checkNotNull(output
.toArray(new String
[output
.size()])),
204 checkNotNull(errorOutput
.toArray(new String
[errorOutput
.size()]))));
207 } else if (OUTPUT_KEY
.equals(strLine
)) {
208 // first line of output
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
) {
216 while (isComment(strLine
)) {
217 strLine
= br
.readLine();
219 // lines of output/error output
220 if (errorOutput
!= null && inErrorOutput
) {
221 errorOutput
.add(strLine
);
222 } else if (output
!= null) {
227 // if (RESULT_END_KEY.equals(strLine)) {
233 } catch (IOException e
) {
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();
248 public synchronized ICommandResult
executeCommand(ICommandInput command
, IProgressMonitor monitor
) throws ExecutionException
{
249 Map
<String
, ICommandResult
> commands
= fScenarioMap
.get(fScenario
);
250 String commandLine
= command
.toString();
251 String fullCommand
= commandLine
;
253 Matcher matcher
= LTTNG_LIST_SESSION_PATTERN
.matcher(commandLine
);
254 if (matcher
.matches() && !commandLine
.matches(LTTNG_LIST_PROVIDER_PATTERN
)) {
255 String sessionName
= matcher
.group(1).trim();
256 Integer i
= fSessionNameMap
.get(sessionName
);
262 fSessionNameMap
.put(sessionName
, i
);
263 fullCommand
+= String
.valueOf(i
);
266 if (commands
.containsKey(fullCommand
)) {
267 return checkNotNull(commands
.get(fullCommand
));
270 String
[] output
= new String
[1];
271 output
[0] = String
.valueOf("Command not found");
272 ICommandResult result
= createCommandResult(1, output
, output
);
276 // ------------------------------------------------------------------------
278 // ------------------------------------------------------------------------
280 private static boolean isComment(String line
) {
282 throw new RuntimeException("line is null");
284 return line
.matches(COMMENT_KEY
);