1 /**********************************************************************
2 * Copyright (c) 2012, 2014 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 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
;
25 import java
.util
.regex
.Matcher
;
26 import java
.util
.regex
.Pattern
;
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
;
33 @SuppressWarnings("javadoc")
34 public class LTTngToolsFileShell
extends TestCommandShell
{
36 // ------------------------------------------------------------------------
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
= "#.*";
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)";
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
56 private String fScenariofile
;
57 private String fScenario
;
59 private final Map
<String
, Map
<String
, ICommandResult
>> fScenarioMap
= new HashMap
<>();
60 private final Map
<String
, Integer
> fSessionNameMap
= new HashMap
<>();
63 * Parse a scenario file with the format:
68 * <COMMAND_INPUT>
70 * </COMAND_INPUT>
72 * <COMMAND_RESULT>
74 * </COMMAND_RESULT>
76 * <COMMAND_OUTPUT>
78 * <COMMAND_ERROR_OUTPUT>
80 * </COMMAND_ERROR_OUTPUT>
81 * </COMMAND_OUTPUT>
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)
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)
95 * @param scenariofile - path to scenario file
98 public synchronized void loadScenarioFile(String scenariofile
) throws Exception
{
99 fScenariofile
= scenariofile
;
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();
107 fScenarioMap
.clear();
112 try (FileInputStream fstream
= new FileInputStream(fScenariofile
);
113 DataInputStream in
= new DataInputStream(fstream
);
114 BufferedReader br
= new BufferedReader(new InputStreamReader(in
));) {
117 // Read File Line by Line
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) {
126 if (isComment(strLine
)) {
130 if (SCENARIO_KEY
.equals(strLine
)) {
134 strLine
= br
.readLine();
135 while (isComment(strLine
)) {
136 strLine
= br
.readLine();
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;
145 boolean inOutput
= false;
146 boolean inErrorOutput
= false;
148 tmpSessionNameMap
.clear();
149 while ((strLine
= br
.readLine()) != null) {
151 if (isComment(strLine
)) {
155 if (SCENARIO_END_KEY
.equals(strLine
)) {
156 // Scenario is finished
159 if (INPUT_KEY
.equals(strLine
)) {
160 strLine
= br
.readLine();
162 while (isComment(strLine
)) {
163 strLine
= br
.readLine();
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
);
179 tmpSessionNameMap
.put(sessionName
, i
);
180 input
+= String
.valueOf(i
);
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();
189 while (isComment(strLine
)) {
190 strLine
= br
.readLine();
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()])));
200 } else if (OUTPUT_KEY
.equals(strLine
)) {
201 // first line of output
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();
212 // lines of output/error output
213 if (errorOutput
!= null && inErrorOutput
) {
214 errorOutput
.add(strLine
);
215 } else if (output
!= null) {
220 // if (RESULT_END_KEY.equals(strLine)) {
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();
239 public synchronized ICommandResult
executeCommand(String command
, IProgressMonitor monitor
) throws ExecutionException
{
240 Map
<String
, ICommandResult
> commands
= fScenarioMap
.get(fScenario
);
241 String fullCommand
= command
;
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
);
252 fSessionNameMap
.put(sessionName
, i
);
253 fullCommand
+= String
.valueOf(i
);
256 if (commands
.containsKey(fullCommand
)) {
257 return commands
.get(fullCommand
);
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
);
270 // ------------------------------------------------------------------------
272 // ------------------------------------------------------------------------
274 private static boolean isComment(String line
) {
276 throw new RuntimeException("line is null");
278 return line
.matches(COMMENT_KEY
);