1 /**********************************************************************
2 * Copyright (c) 2012 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>"; //$NON-NLS-1$
39 private final static String SCENARIO_END_KEY
= "</SCENARIO>"; //$NON-NLS-1$
40 private final static String INPUT_KEY
= "<COMMAND_INPUT>"; //$NON-NLS-1$
41 private final static String INPUT_END_KEY
= "</COMMAND_INPUT>"; //$NON-NLS-1$
42 private final static String RESULT_KEY
= "<COMMAND_RESULT>"; //$NON-NLS-1$
43 @SuppressWarnings("unused")
44 private final static String RESULT_END_KEY
= "</COMMAND_RESULT>"; //$NON-NLS-1$
45 private final static String OUTPUT_KEY
= "<COMMAND_OUTPUT>"; //$NON-NLS-1$
46 private final static String OUTPUT_END_KEY
= "</COMMAND_OUTPUT>"; //$NON-NLS-1$
47 private final static String COMMENT_KEY
= "#.*"; //$NON-NLS-1$
49 private final static Pattern LTTNG_LIST_SESSION_PATTERN
= Pattern
.compile("lttng\\s+list\\s+(.+)"); //$NON-NLS-1$
50 private final static String LTTNG_LIST_PROVIDER_PATTERN
= "lttng\\s+list\\s+(-u|-k)"; //$NON-NLS-1$
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
55 private String fScenariofile
;
56 private String fScenario
;
58 private final Map
<String
, Map
<String
, ICommandResult
>> fScenarioMap
= new HashMap
<String
, Map
<String
, ICommandResult
>>();
59 private final Map
<String
, Integer
> fSessionNameMap
= new HashMap
<String
, Integer
>();
62 * Parse a scenario file with the format:
80 * Where: ScenarioName - is the scenario name
81 * Command - the command line string
82 * CommandResult - the result integer of the command (0 for success, 1 for failure)
83 * ComandOutput - the command output string (multi-line possible)
85 * Note: 1) There can be many scenarios per file
86 * 2) There can be many (Command-CommandResult-CommandOutput) triples per scenario
87 * 3) Lines starting with # will be ignored (comments)
89 * @param scenariofile - path to scenario file
92 public synchronized void loadScenarioFile(String scenariofile
) throws Exception
{
93 fScenariofile
= scenariofile
;
96 Collection
<Map
<String
, ICommandResult
>> values
= fScenarioMap
.values();
97 for (Iterator
<Map
<String
, ICommandResult
>> iterator
= values
.iterator(); iterator
.hasNext();) {
98 Map
<String
, ICommandResult
> map
= iterator
.next();
101 fScenarioMap
.clear();
106 FileInputStream fstream
= new FileInputStream(fScenariofile
);
108 // Get the object of DataInputStream
109 DataInputStream in
= new DataInputStream(fstream
);
110 BufferedReader br
= new BufferedReader(new InputStreamReader(in
));
113 // Read File Line by Line
115 // Temporary map for generating instance numbers for lttng list <session> commands.
116 // The numbers are per scenario.
117 Map
<String
, Integer
> tmpSessionNameMap
= new HashMap
<String
, Integer
>();
118 while ((strLine
= br
.readLine()) != null) {
121 if(isComment(strLine
)) {
125 if (SCENARIO_KEY
.equals(strLine
)) {
129 strLine
= br
.readLine();
130 while (isComment(strLine
)) {
131 strLine
= br
.readLine();
134 String scenario
= strLine
;
135 Map
<String
, ICommandResult
> commandMap
= new HashMap
<String
, ICommandResult
>();
136 fScenarioMap
.put(scenario
, commandMap
);
137 List
<String
> output
= null;
139 boolean inOutput
= false;
141 tmpSessionNameMap
.clear();
142 while ((strLine
= br
.readLine()) != null) {
144 if(isComment(strLine
)) {
148 if (SCENARIO_END_KEY
.equals(strLine
)) {
149 // Scenario is finished
152 if (INPUT_KEY
.equals(strLine
)) {
153 strLine
= br
.readLine();
155 while (isComment(strLine
)) {
156 strLine
= br
.readLine();
161 // Handle instances of 'lttng list <session"-comamand
162 Matcher matcher
= LTTNG_LIST_SESSION_PATTERN
.matcher(strLine
);
163 if (matcher
.matches() && !input
.matches(LTTNG_LIST_PROVIDER_PATTERN
)) {
164 String sessionName
= matcher
.group(1).trim();
165 Integer i
= tmpSessionNameMap
.get(sessionName
);
171 tmpSessionNameMap
.put(sessionName
, i
);
172 input
+= String
.valueOf(i
);
174 } else if (INPUT_END_KEY
.equals(strLine
)) {
175 // Initialize output array
176 output
= new ArrayList
<String
>();
177 } else if (RESULT_KEY
.equals(strLine
)) {
178 strLine
= br
.readLine();
180 while (isComment(strLine
)) {
181 strLine
= br
.readLine();
184 result
= Integer
.parseInt(strLine
);
185 } else if (OUTPUT_END_KEY
.equals(strLine
)) {
186 // Save output/result in command map
187 if (output
!= null) {
188 commandMap
.put(input
, new CommandResult(result
, output
.toArray(new String
[output
.size()])));
191 } else if (OUTPUT_KEY
.equals(strLine
)) {
192 // first line of output
194 strLine
= br
.readLine();
197 while (isComment(strLine
)) {
198 strLine
= br
.readLine();
200 if (output
!= null) {
203 } else if (inOutput
) {
204 // subsequent lines of output
205 if (output
!= null) {
210 // if (RESULT_END_KEY.equals(strLine)) {
216 //Close the input stream
220 // Set the scenario to consider in executeCommand()
221 public synchronized void setScenario(String scenario
) {
222 fScenario
= scenario
;
223 fSessionNameMap
.clear();
224 if (!fScenarioMap
.containsKey(fScenario
)) {
225 throw new IllegalArgumentException();
231 * @see org.eclipse.linuxtools.lttng.stubs.service.shells.TestCommandShell#executeCommand(java.lang.String, org.eclipse.core.runtime.IProgressMonitor, boolean)
234 public synchronized ICommandResult
executeCommand(String command
, IProgressMonitor monitor
, boolean checkReturnValue
) throws ExecutionException
{
235 Map
<String
, ICommandResult
> commands
= fScenarioMap
.get(fScenario
);
236 String fullCommand
= command
;
238 Matcher matcher
= LTTNG_LIST_SESSION_PATTERN
.matcher(command
);
239 if (matcher
.matches() && !command
.matches(LTTNG_LIST_PROVIDER_PATTERN
)) {
240 String sessionName
= matcher
.group(1).trim();
241 Integer i
= fSessionNameMap
.get(sessionName
);
247 fSessionNameMap
.put(sessionName
, i
);
248 fullCommand
+= String
.valueOf(i
);
251 if (commands
.containsKey(fullCommand
)) {
252 return commands
.get(fullCommand
);
255 String
[] output
= new String
[1];
256 output
[0] = String
.valueOf("Command not found"); //$NON-NLS-1$
257 CommandResult result
= new CommandResult(0, null);
258 // For verification of setters of class CommandResult
259 result
.setOutput(output
);
264 // ------------------------------------------------------------------------
266 // ------------------------------------------------------------------------
267 private static boolean isComment(String line
) {
269 throw new RuntimeException("line is null"); //$NON-NLS-1$
271 return line
.matches(COMMENT_KEY
);