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 **********************************************************************/
12 package org
.eclipse
.linuxtools
.internal
.lttng2
.control
.stubs
.shells
;
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
;
24 import java
.util
.regex
.Matcher
;
25 import java
.util
.regex
.Pattern
;
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
;
32 @SuppressWarnings("javadoc")
33 public class LTTngToolsFileShell
extends TestCommandShell
{
35 // ------------------------------------------------------------------------
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
= "#.*";
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)";
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
55 private String fScenariofile
;
56 private String fScenario
;
58 private final Map
<String
, Map
<String
, ICommandResult
>> fScenarioMap
= new HashMap
<>();
59 private final Map
<String
, Integer
> fSessionNameMap
= new HashMap
<>();
62 * Parse a scenario file with the format:
67 * <COMMAND_INPUT>
69 * </COMAND_INPUT>
71 * <COMMAND_RESULT>
73 * </COMMAND_RESULT>
75 * <COMMAND_OUTPUT>
77 * <COMMAND_ERROR_OUTPUT>
79 * </COMMAND_ERROR_OUTPUT>
80 * </COMMAND_OUTPUT>
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)
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)
94 * @param scenariofile - path to scenario file
97 public synchronized void loadScenarioFile(String scenariofile
) throws Exception
{
98 fScenariofile
= scenariofile
;
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();
106 fScenarioMap
.clear();
111 try (FileInputStream fstream
= new FileInputStream(fScenariofile
);
112 DataInputStream in
= new DataInputStream(fstream
);
113 BufferedReader br
= new BufferedReader(new InputStreamReader(in
));) {
116 // Read File Line by Line
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) {
125 if (isComment(strLine
)) {
129 if (SCENARIO_KEY
.equals(strLine
)) {
133 strLine
= br
.readLine();
134 while (isComment(strLine
)) {
135 strLine
= br
.readLine();
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;
144 boolean inOutput
= false;
145 boolean inErrorOutput
= false;
147 tmpSessionNameMap
.clear();
148 while ((strLine
= br
.readLine()) != null) {
150 if (isComment(strLine
)) {
154 if (SCENARIO_END_KEY
.equals(strLine
)) {
155 // Scenario is finished
158 if (INPUT_KEY
.equals(strLine
)) {
159 strLine
= br
.readLine();
161 while (isComment(strLine
)) {
162 strLine
= br
.readLine();
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
);
178 tmpSessionNameMap
.put(sessionName
, i
);
179 input
+= String
.valueOf(i
);
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();
188 while (isComment(strLine
)) {
189 strLine
= br
.readLine();
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()])));
199 } else if (OUTPUT_KEY
.equals(strLine
)) {
200 // first line of output
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();
211 // lines of output/error output
212 if (errorOutput
!= null && inErrorOutput
) {
213 errorOutput
.add(strLine
);
214 } else if (output
!= null) {
219 // if (RESULT_END_KEY.equals(strLine)) {
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();
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
;
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
);
251 fSessionNameMap
.put(sessionName
, i
);
252 fullCommand
+= String
.valueOf(i
);
255 if (commands
.containsKey(fullCommand
)) {
256 return commands
.get(fullCommand
);
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
);
269 // ------------------------------------------------------------------------
271 // ------------------------------------------------------------------------
273 private static boolean isComment(String line
) {
275 throw new RuntimeException("line is null");
277 return line
.matches(COMMENT_KEY
);