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
;
32 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.service
.LTTngControlService
;
34 @SuppressWarnings("javadoc")
35 public class LTTngToolsFileShell
extends TestCommandShell
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
40 private final static String SCENARIO_KEY
= "<SCENARIO>";
41 private final static String SCENARIO_END_KEY
= "</SCENARIO>";
42 private final static String INPUT_KEY
= "<COMMAND_INPUT>";
43 private final static String INPUT_END_KEY
= "</COMMAND_INPUT>";
44 private final static String RESULT_KEY
= "<COMMAND_RESULT>";
45 private final static String OUTPUT_KEY
= "<COMMAND_OUTPUT>";
46 private final static String OUTPUT_END_KEY
= "</COMMAND_OUTPUT>";
47 private final static String ERROR_OUTPUT_KEY
= "<COMMAND_ERROR_OUTPUT>";
48 private final static String ERROR_OUTPUT_END_KEY
= "</COMMAND_ERROR_OUTPUT>";
49 private final static String COMMENT_KEY
= "#.*";
51 private final static Pattern LTTNG_LIST_SESSION_PATTERN
= Pattern
.compile("lttng\\s+list\\s+(.+)");
52 private final static String LTTNG_LIST_PROVIDER_PATTERN
= "lttng\\s+list\\s+(-u|-k)";
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
57 private String fScenariofile
;
58 private String fScenario
;
60 private final Map
<String
, Map
<String
, ICommandResult
>> fScenarioMap
= new HashMap
<>();
61 private final Map
<String
, Integer
> fSessionNameMap
= new HashMap
<>();
64 * Parse a scenario file with the format:
70 * <COMMAND_INPUT>
72 * </COMAND_INPUT>
74 * <COMMAND_RESULT>
76 * </COMMAND_RESULT>
78 * <COMMAND_OUTPUT>
80 * <COMMAND_ERROR_OUTPUT>
82 * </COMMAND_ERROR_OUTPUT>
83 * </COMMAND_OUTPUT>
87 * Where: ScenarioName - is the scenario name
88 * Command - the command line string
89 * CommandResult - the result integer of the command (0 for success, 1 for failure)
90 * ComandOutput - the command output string (multi-line possible)
91 * ComandErrorOutput - the command error output string (multi-line possible)
93 * Note: 1) There can be many scenarios per file
94 * 2) There can be many (Command-CommandResult-CommandOutput) triples per scenario
95 * 3) Lines starting with # will be ignored (comments)
98 * @param scenariofile - path to scenario file
101 public synchronized void loadScenarioFile(String scenariofile
) throws Exception
{
102 fScenariofile
= scenariofile
;
105 Collection
<Map
<String
, ICommandResult
>> values
= fScenarioMap
.values();
106 for (Iterator
<Map
<String
, ICommandResult
>> iterator
= values
.iterator(); iterator
.hasNext();) {
107 Map
<String
, ICommandResult
> map
= iterator
.next();
110 fScenarioMap
.clear();
115 try (FileInputStream fstream
= new FileInputStream(fScenariofile
);
116 DataInputStream in
= new DataInputStream(fstream
);
117 BufferedReader br
= new BufferedReader(new InputStreamReader(in
));) {
120 // Read File Line by Line
122 // Temporary map for generating instance numbers for lttng list
123 // <session> commands.
124 // The numbers are per scenario.
125 Map
<String
, Integer
> tmpSessionNameMap
= new HashMap
<>();
126 while ((strLine
= br
.readLine()) != null) {
129 if (isComment(strLine
)) {
133 if (SCENARIO_KEY
.equals(strLine
)) {
137 strLine
= br
.readLine();
138 while (isComment(strLine
)) {
139 strLine
= br
.readLine();
142 String scenario
= strLine
;
143 Map
<String
, ICommandResult
> commandMap
= new HashMap
<>();
144 fScenarioMap
.put(scenario
, commandMap
);
145 List
<String
> output
= null;
146 List
<String
> errorOutput
= null;
148 boolean inOutput
= false;
149 boolean inErrorOutput
= false;
151 tmpSessionNameMap
.clear();
152 while ((strLine
= br
.readLine()) != null) {
154 if (isComment(strLine
)) {
158 if (SCENARIO_END_KEY
.equals(strLine
)) {
159 // Scenario is finished
162 if (INPUT_KEY
.equals(strLine
)) {
163 strLine
= br
.readLine();
165 while (isComment(strLine
)) {
166 strLine
= br
.readLine();
171 // Handle instances of 'lttng list
172 // <session"-comamand
173 Matcher matcher
= LTTNG_LIST_SESSION_PATTERN
.matcher(strLine
);
174 if (matcher
.matches() && !input
.matches(LTTNG_LIST_PROVIDER_PATTERN
)) {
175 String sessionName
= matcher
.group(1).trim();
176 Integer i
= tmpSessionNameMap
.get(sessionName
);
182 tmpSessionNameMap
.put(sessionName
, i
);
183 input
+= String
.valueOf(i
);
185 } else if (INPUT_END_KEY
.equals(strLine
)) {
186 // Initialize output array
187 output
= new ArrayList
<>();
188 errorOutput
= new ArrayList
<>();
189 } else if (RESULT_KEY
.equals(strLine
)) {
190 strLine
= br
.readLine();
192 while (isComment(strLine
)) {
193 strLine
= br
.readLine();
196 result
= Integer
.parseInt(strLine
);
197 } else if (OUTPUT_END_KEY
.equals(strLine
)) {
198 // Save output/result in command map
199 if (output
!= null && errorOutput
!= null) {
200 commandMap
.put(input
, new CommandResult(result
, output
.toArray(new String
[output
.size()]), errorOutput
.toArray(new String
[errorOutput
.size()])));
203 } else if (OUTPUT_KEY
.equals(strLine
)) {
204 // first line of output
206 } else if (ERROR_OUTPUT_KEY
.equals(strLine
)) {
207 // first line of output
208 inErrorOutput
= true;
209 } else if (ERROR_OUTPUT_END_KEY
.equals(strLine
)) {
210 inErrorOutput
= false;
211 } else if (inOutput
) {
212 while (isComment(strLine
)) {
213 strLine
= br
.readLine();
215 // lines of output/error output
216 if (errorOutput
!= null && inErrorOutput
) {
217 errorOutput
.add(strLine
);
218 } else if (output
!= null) {
223 // if (RESULT_END_KEY.equals(strLine)) {
232 // Set the scenario to consider in executeCommand()
233 public synchronized void setScenario(String scenario
) {
234 fScenario
= scenario
;
235 fSessionNameMap
.clear();
236 if (!fScenarioMap
.containsKey(fScenario
)) {
237 throw new IllegalArgumentException();
242 public synchronized ICommandResult
executeCommand(List
<String
> command
, IProgressMonitor monitor
) throws ExecutionException
{
243 Map
<String
, ICommandResult
> commands
= fScenarioMap
.get(fScenario
);
244 String commandLine
= LTTngControlService
.toCommandString(command
);
245 String fullCommand
= commandLine
;
247 Matcher matcher
= LTTNG_LIST_SESSION_PATTERN
.matcher(commandLine
);
248 if (matcher
.matches() && !commandLine
.matches(LTTNG_LIST_PROVIDER_PATTERN
)) {
249 String sessionName
= matcher
.group(1).trim();
250 Integer i
= fSessionNameMap
.get(sessionName
);
256 fSessionNameMap
.put(sessionName
, i
);
257 fullCommand
+= String
.valueOf(i
);
260 if (commands
.containsKey(fullCommand
)) {
261 return commands
.get(fullCommand
);
264 String
[] output
= new String
[1];
265 output
[0] = String
.valueOf("Command not found");
266 CommandResult result
= new CommandResult(0, null, null);
267 // For verification of setters of class CommandResult
268 result
.setOutput(output
);
269 result
.setErrorOutput(output
);
274 // ------------------------------------------------------------------------
276 // ------------------------------------------------------------------------
278 private static boolean isComment(String line
) {
280 throw new RuntimeException("line is null");
282 return line
.matches(COMMENT_KEY
);