1 /**********************************************************************
2 * Copyright (c) 2012, 2013 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
.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
.ui
.views
.control
.remote
.CommandResult
;
30 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.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 COMMENT_KEY
= "#.*";
47 private final static Pattern LTTNG_LIST_SESSION_PATTERN
= Pattern
.compile("lttng\\s+list\\s+(.+)");
48 private final static String LTTNG_LIST_PROVIDER_PATTERN
= "lttng\\s+list\\s+(-u|-k)";
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
53 private String fScenariofile
;
54 private String fScenario
;
56 private final Map
<String
, Map
<String
, ICommandResult
>> fScenarioMap
= new HashMap
<>();
57 private final Map
<String
, Integer
> fSessionNameMap
= new HashMap
<>();
60 * Parse a scenario file with the format:
78 * Where: ScenarioName - is the scenario name
79 * Command - the command line string
80 * CommandResult - the result integer of the command (0 for success, 1 for failure)
81 * ComandOutput - the command output string (multi-line possible)
83 * Note: 1) There can be many scenarios per file
84 * 2) There can be many (Command-CommandResult-CommandOutput) triples per scenario
85 * 3) Lines starting with # will be ignored (comments)
87 * @param scenariofile - path to scenario file
90 public synchronized void loadScenarioFile(String scenariofile
) throws Exception
{
91 fScenariofile
= scenariofile
;
94 Collection
<Map
<String
, ICommandResult
>> values
= fScenarioMap
.values();
95 for (Iterator
<Map
<String
, ICommandResult
>> iterator
= values
.iterator(); iterator
.hasNext();) {
96 Map
<String
, ICommandResult
> map
= iterator
.next();
104 try (FileInputStream fstream
= new FileInputStream(fScenariofile
);
105 DataInputStream in
= new DataInputStream(fstream
);
106 BufferedReader br
= new BufferedReader(new InputStreamReader(in
));) {
109 // Read File Line by Line
111 // Temporary map for generating instance numbers for lttng list
112 // <session> commands.
113 // The numbers are per scenario.
114 Map
<String
, Integer
> tmpSessionNameMap
= new HashMap
<>();
115 while ((strLine
= br
.readLine()) != null) {
118 if (isComment(strLine
)) {
122 if (SCENARIO_KEY
.equals(strLine
)) {
126 strLine
= br
.readLine();
127 while (isComment(strLine
)) {
128 strLine
= br
.readLine();
131 String scenario
= strLine
;
132 Map
<String
, ICommandResult
> commandMap
= new HashMap
<>();
133 fScenarioMap
.put(scenario
, commandMap
);
134 List
<String
> output
= null;
136 boolean inOutput
= false;
138 tmpSessionNameMap
.clear();
139 while ((strLine
= br
.readLine()) != null) {
141 if (isComment(strLine
)) {
145 if (SCENARIO_END_KEY
.equals(strLine
)) {
146 // Scenario is finished
149 if (INPUT_KEY
.equals(strLine
)) {
150 strLine
= br
.readLine();
152 while (isComment(strLine
)) {
153 strLine
= br
.readLine();
158 // Handle instances of 'lttng list
159 // <session"-comamand
160 Matcher matcher
= LTTNG_LIST_SESSION_PATTERN
.matcher(strLine
);
161 if (matcher
.matches() && !input
.matches(LTTNG_LIST_PROVIDER_PATTERN
)) {
162 String sessionName
= matcher
.group(1).trim();
163 Integer i
= tmpSessionNameMap
.get(sessionName
);
169 tmpSessionNameMap
.put(sessionName
, i
);
170 input
+= String
.valueOf(i
);
172 } else if (INPUT_END_KEY
.equals(strLine
)) {
173 // Initialize output array
174 output
= new ArrayList
<>();
175 } else if (RESULT_KEY
.equals(strLine
)) {
176 strLine
= br
.readLine();
178 while (isComment(strLine
)) {
179 strLine
= br
.readLine();
182 result
= Integer
.parseInt(strLine
);
183 } else if (OUTPUT_END_KEY
.equals(strLine
)) {
184 // Save output/result in command map
185 if (output
!= null) {
186 commandMap
.put(input
, new CommandResult(result
, output
.toArray(new String
[output
.size()])));
189 } else if (OUTPUT_KEY
.equals(strLine
)) {
190 // first line of output
192 strLine
= br
.readLine();
195 while (isComment(strLine
)) {
196 strLine
= br
.readLine();
198 if (output
!= null) {
201 } else if (inOutput
) {
202 // subsequent lines of output
203 if (output
!= null) {
208 // if (RESULT_END_KEY.equals(strLine)) {
217 // Set the scenario to consider in executeCommand()
218 public synchronized void setScenario(String scenario
) {
219 fScenario
= scenario
;
220 fSessionNameMap
.clear();
221 if (!fScenarioMap
.containsKey(fScenario
)) {
222 throw new IllegalArgumentException();
227 public synchronized ICommandResult
executeCommand(String command
, IProgressMonitor monitor
, boolean checkReturnValue
) throws ExecutionException
{
228 Map
<String
, ICommandResult
> commands
= fScenarioMap
.get(fScenario
);
229 String fullCommand
= command
;
231 Matcher matcher
= LTTNG_LIST_SESSION_PATTERN
.matcher(command
);
232 if (matcher
.matches() && !command
.matches(LTTNG_LIST_PROVIDER_PATTERN
)) {
233 String sessionName
= matcher
.group(1).trim();
234 Integer i
= fSessionNameMap
.get(sessionName
);
240 fSessionNameMap
.put(sessionName
, i
);
241 fullCommand
+= String
.valueOf(i
);
244 if (commands
.containsKey(fullCommand
)) {
245 return commands
.get(fullCommand
);
248 String
[] output
= new String
[1];
249 output
[0] = String
.valueOf("Command not found");
250 CommandResult result
= new CommandResult(0, null);
251 // For verification of setters of class CommandResult
252 result
.setOutput(output
);
257 // ------------------------------------------------------------------------
259 // ------------------------------------------------------------------------
261 private static boolean isComment(String line
) {
263 throw new RuntimeException("line is null");
265 return line
.matches(COMMENT_KEY
);