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
.service
.CommandResult
;
30 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.service
.ICommandResult
;
32 public class LTTngToolsFileShell
extends TestCommandShell
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
37 private final static String SCENARIO_KEY
= "<SCENARIO>"; //$NON-NLS-1$
38 private final static String SCENARIO_END_KEY
= "</SCENARIO>"; //$NON-NLS-1$
39 private final static String INPUT_KEY
= "<COMMAND_INPUT>"; //$NON-NLS-1$
40 private final static String INPUT_END_KEY
= "</COMMAND_INPUT>"; //$NON-NLS-1$
41 private final static String RESULT_KEY
= "<COMMAND_RESULT>"; //$NON-NLS-1$
42 @SuppressWarnings("unused")
43 private final static String RESULT_END_KEY
= "</COMMAND_RESULT>"; //$NON-NLS-1$
44 private final static String OUTPUT_KEY
= "<COMMAND_OUTPUT>"; //$NON-NLS-1$
45 private final static String OUTPUT_END_KEY
= "</COMMAND_OUTPUT>"; //$NON-NLS-1$
46 private final static String COMMENT_KEY
= "#.*"; //$NON-NLS-1$
48 private final static Pattern LTTNG_LIST_SESSION_PATTERN
= Pattern
.compile("lttng\\s+list\\s+(.+)"); //$NON-NLS-1$
49 private final static String LTTNG_LIST_PROVIDER_PATTERN
= "lttng\\s+list\\s+(-u|-k)"; //$NON-NLS-1$
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
54 private String fScenariofile
;
55 private String fScenario
;
57 private Map
<String
, Map
<String
, ICommandResult
>> fScenarioMap
= new HashMap
<String
, Map
<String
, ICommandResult
>>();
58 private Map
<String
, Integer
> fSessionNameMap
= new HashMap
<String
, Integer
>();
61 * Parse a scenario file with the format:
79 * Where: ScenarioName - is the scenario name
80 * Command - the command line string
81 * CommandResult - the result integer of the command (0 for success, 1 for failure)
82 * ComandOutput - the command output string (multi-line possible)
84 * Note: 1) There can be many scenarios per file
85 * 2) There can be many (Command-CommandResult-CommandOutput) triples per scenario
86 * 3) Lines starting with # will be ignored (comments)
88 * @param scenariofile - path to scenario file
91 public void loadScenarioFile(String scenariofile
) throws Exception
{
92 fScenariofile
= scenariofile
;
95 Collection
<Map
<String
, ICommandResult
>> values
= fScenarioMap
.values();
96 for (Iterator
<Map
<String
, ICommandResult
>> iterator
= values
.iterator(); iterator
.hasNext();) {
97 Map
<String
, ICommandResult
> map
= (Map
<String
, ICommandResult
>) iterator
.next();
100 fScenarioMap
.clear();
105 FileInputStream fstream
= new FileInputStream(fScenariofile
);
107 // Get the object of DataInputStream
108 DataInputStream in
= new DataInputStream(fstream
);
109 BufferedReader br
= new BufferedReader(new InputStreamReader(in
));
112 // Read File Line by Line
114 // Temporary map for generating instance numbers for lttng list <session> commands.
115 // The numbers are per scenario.
116 Map
<String
, Integer
> tmpSessionNameMap
= new HashMap
<String
, Integer
>();
117 while ((strLine
= br
.readLine()) != null) {
120 if(isComment(strLine
)) {
124 if (SCENARIO_KEY
.equals(strLine
)) {
128 strLine
= br
.readLine();
129 while (isComment(strLine
)) {
130 strLine
= br
.readLine();
133 String scenario
= strLine
;
134 Map
<String
, ICommandResult
> commandMap
= new HashMap
<String
, ICommandResult
>();
135 fScenarioMap
.put(scenario
, commandMap
);
136 List
<String
> output
= null;
138 boolean inOutput
= false;
140 tmpSessionNameMap
.clear();
141 while ((strLine
= br
.readLine()) != null) {
143 if(isComment(strLine
)) {
147 if (SCENARIO_END_KEY
.equals(strLine
)) {
148 // Scenario is finished
151 if (INPUT_KEY
.equals(strLine
)) {
152 strLine
= br
.readLine();
154 while (isComment(strLine
)) {
155 strLine
= br
.readLine();
160 // Handle instances of 'lttng list <session"-comamand
161 Matcher matcher
= LTTNG_LIST_SESSION_PATTERN
.matcher(strLine
);
162 if (matcher
.matches() && !input
.matches(LTTNG_LIST_PROVIDER_PATTERN
)) {
163 String sessionName
= matcher
.group(1).trim();
164 Integer i
= tmpSessionNameMap
.get(sessionName
);
170 tmpSessionNameMap
.put(sessionName
, i
);
171 input
+= String
.valueOf(i
);
173 } else if (INPUT_END_KEY
.equals(strLine
)) {
174 // Initialize output array
175 output
= new ArrayList
<String
>();
176 } else if (RESULT_KEY
.equals(strLine
)) {
177 strLine
= br
.readLine();
179 while (isComment(strLine
)) {
180 strLine
= br
.readLine();
183 result
= Integer
.parseInt(strLine
);
184 } else if (OUTPUT_END_KEY
.equals(strLine
)) {
185 // Save output/result in command map
186 commandMap
.put(input
, new CommandResult(result
, output
.toArray(new String
[output
.size()])));
188 } else if (OUTPUT_KEY
.equals(strLine
)) {
189 // first line of output
191 strLine
= br
.readLine();
194 while (isComment(strLine
)) {
195 strLine
= br
.readLine();
198 } else if (inOutput
) {
199 // subsequent lines of output
203 // if (RESULT_END_KEY.equals(strLine)) {
209 //Close the input stream
213 // Set the scenario to consider in executeCommand()
214 public synchronized void setScenario(String scenario
) {
215 fScenario
= scenario
;
216 fSessionNameMap
.clear();
217 if (!fScenarioMap
.containsKey(fScenario
)) {
218 throw new IllegalArgumentException();
224 * @see org.eclipse.linuxtools.lttng.stubs.service.shells.TestCommandShell#executeCommand(java.lang.String, org.eclipse.core.runtime.IProgressMonitor, boolean)
226 @SuppressWarnings("nls")
228 public ICommandResult
executeCommand(String command
, IProgressMonitor monitor
, boolean checkReturnValue
) throws ExecutionException
{
229 Map
<String
, ICommandResult
> commands
= fScenarioMap
.get(fScenario
);
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 command
+= String
.valueOf(i
);
244 if (commands
.containsKey(command
)) {
245 return commands
.get(command
);
248 String
[] output
= new String
[1];
249 output
[0] = String
.valueOf("Command not found");
250 return new CommandResult(1, output
);
253 // ------------------------------------------------------------------------
255 // ------------------------------------------------------------------------
256 private boolean isComment(String line
) {
257 return line
.matches(COMMENT_KEY
);