Restore some code inadvertently commented out
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui.tests / stubs / org / eclipse / linuxtools / internal / lttng2 / stubs / shells / LTTngToolsFileShell.java
CommitLineData
d132bcc7
BH
1/**********************************************************************
2 * Copyright (c) 2012 Ericsson
cfdb727a 3 *
d132bcc7
BH
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
cfdb727a
AM
8 *
9 * Contributors:
d132bcc7
BH
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
115b4a01 12package org.eclipse.linuxtools.internal.lttng2.stubs.shells;
d132bcc7
BH
13
14import java.io.BufferedReader;
15import java.io.DataInputStream;
16import java.io.FileInputStream;
17import java.io.InputStreamReader;
18import java.util.ArrayList;
19import java.util.Collection;
20import java.util.HashMap;
21import java.util.Iterator;
22import java.util.List;
23import java.util.Map;
24import java.util.regex.Matcher;
25import java.util.regex.Pattern;
26
27import org.eclipse.core.commands.ExecutionException;
28import org.eclipse.core.runtime.IProgressMonitor;
9315aeee
BH
29import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.CommandResult;
30import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.ICommandResult;
d132bcc7 31
cfdb727a 32@SuppressWarnings("javadoc")
d132bcc7
BH
33public class LTTngToolsFileShell extends TestCommandShell {
34
35 // ------------------------------------------------------------------------
36 // CONSTANTS
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$
cfdb727a 48
d132bcc7
BH
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$
51
52 // ------------------------------------------------------------------------
53 // Attributes
54 // ------------------------------------------------------------------------
55 private String fScenariofile;
56 private String fScenario;
57
cfdb727a
AM
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>();
d132bcc7
BH
60
61 /**
62 * Parse a scenario file with the format:
63 * <SCENARIO>
64 * ScenarioName
cfdb727a 65 *
d132bcc7
BH
66 * <COMMAND_INPUT>
67 * Command
68 * </COMAND_INPUT>
cfdb727a 69 *
d132bcc7
BH
70 * <COMMAND_RESULT>
71 * CommandResult
72 * </COMMAND_RESULT>
cfdb727a 73 *
d132bcc7
BH
74 * <COMMAND_OUTPUT>
75 * CommandOutput
76 * </COMMAND_OUTPUT>
cfdb727a 77 *
d132bcc7 78 * </SCENARIO>
cfdb727a 79 *
d132bcc7
BH
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)
cfdb727a 84 *
d132bcc7
BH
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)
cfdb727a 88 *
d132bcc7
BH
89 * @param scenariofile - path to scenario file
90 * @throws Exception
91 */
1f2f091b 92 public synchronized void loadScenarioFile(String scenariofile) throws Exception {
d132bcc7 93 fScenariofile = scenariofile;
cfdb727a 94
d132bcc7
BH
95 // clean up map
96 Collection<Map<String, ICommandResult>> values = fScenarioMap.values();
97 for (Iterator<Map<String, ICommandResult>> iterator = values.iterator(); iterator.hasNext();) {
cfdb727a 98 Map<String, ICommandResult> map = iterator.next();
d132bcc7
BH
99 map.clear();
100 }
101 fScenarioMap.clear();
cfdb727a 102
d132bcc7 103 // load from file
cfdb727a 104
d132bcc7
BH
105 // Open the file
106 FileInputStream fstream = new FileInputStream(fScenariofile);
cfdb727a 107
d132bcc7
BH
108 // Get the object of DataInputStream
109 DataInputStream in = new DataInputStream(fstream);
110 BufferedReader br = new BufferedReader(new InputStreamReader(in));
111 String strLine;
cfdb727a 112
d132bcc7 113 // Read File Line by Line
cfdb727a
AM
114
115 // Temporary map for generating instance numbers for lttng list <session> commands.
d132bcc7
BH
116 // The numbers are per scenario.
117 Map<String, Integer> tmpSessionNameMap = new HashMap<String, Integer>();
118 while ((strLine = br.readLine()) != null) {
119
120 // Ignore comments
121 if(isComment(strLine)) {
122 continue;
123 }
124
125 if (SCENARIO_KEY.equals(strLine)) {
126 // scenario start
127
128 // Ignore comments
129 strLine = br.readLine();
130 while (isComment(strLine)) {
131 strLine = br.readLine();
132 }
133
134 String scenario = strLine;
135 Map<String, ICommandResult> commandMap = new HashMap<String, ICommandResult>();
136 fScenarioMap.put(scenario, commandMap);
137 List<String> output = null;
138 String input = null;
139 boolean inOutput = false;
140 int result = 0;
141 tmpSessionNameMap.clear();
142 while ((strLine = br.readLine()) != null) {
cfdb727a 143 // Ignore comments
d132bcc7
BH
144 if(isComment(strLine)) {
145 continue;
146 }
147
148 if (SCENARIO_END_KEY.equals(strLine)) {
149 // Scenario is finished
150 break;
cfdb727a 151 }
d132bcc7
BH
152 if (INPUT_KEY.equals(strLine)) {
153 strLine = br.readLine();
154 // Ignore comments
155 while (isComment(strLine)) {
156 strLine = br.readLine();
157 }
158 // Read command
159 input = strLine;
cfdb727a 160
d132bcc7
BH
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);
166 if (i != null) {
167 i++;
168 } else {
169 i = 0;
170 }
171 tmpSessionNameMap.put(sessionName, i);
172 input += String.valueOf(i);
173 }
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();
179 // Ignore comments
180 while (isComment(strLine)) {
181 strLine = br.readLine();
182 }
183 // Save result value
184 result = Integer.parseInt(strLine);
185 } else if (OUTPUT_END_KEY.equals(strLine)) {
186 // Save output/result in command map
187 commandMap.put(input, new CommandResult(result, output.toArray(new String[output.size()])));
188 inOutput = false;
189 } else if (OUTPUT_KEY.equals(strLine)) {
190 // first line of output
191 inOutput = true;
192 strLine = br.readLine();
193
194 // Ignore comments
195 while (isComment(strLine)) {
196 strLine = br.readLine();
197 }
198 output.add(strLine);
199 } else if (inOutput) {
200 // subsequent lines of output
201 output.add(strLine);
cfdb727a
AM
202 }
203// else {
d132bcc7
BH
204// if (RESULT_END_KEY.equals(strLine)) {
205 // nothing to do
206// }
207 }
208 }
209 }
210 //Close the input stream
211 in.close();
212 }
213
214 // Set the scenario to consider in executeCommand()
215 public synchronized void setScenario(String scenario) {
216 fScenario = scenario;
217 fSessionNameMap.clear();
218 if (!fScenarioMap.containsKey(fScenario)) {
219 throw new IllegalArgumentException();
220 }
221 }
222
223 /*
224 * (non-Javadoc)
225 * @see org.eclipse.linuxtools.lttng.stubs.service.shells.TestCommandShell#executeCommand(java.lang.String, org.eclipse.core.runtime.IProgressMonitor, boolean)
226 */
227 @SuppressWarnings("nls")
228 @Override
1f2f091b 229 public synchronized ICommandResult executeCommand(String command, IProgressMonitor monitor, boolean checkReturnValue) throws ExecutionException {
d132bcc7
BH
230 Map<String, ICommandResult> commands = fScenarioMap.get(fScenario);
231
232 Matcher matcher = LTTNG_LIST_SESSION_PATTERN.matcher(command);
233 if (matcher.matches() && !command.matches(LTTNG_LIST_PROVIDER_PATTERN)) {
234 String sessionName = matcher.group(1).trim();
235 Integer i = fSessionNameMap.get(sessionName);
236 if (i != null) {
237 i++;
238 } else {
239 i = 0;
240 }
241 fSessionNameMap.put(sessionName, i);
242 command += String.valueOf(i);
243 }
244
245 if (commands.containsKey(command)) {
246 return commands.get(command);
cfdb727a 247 }
d132bcc7
BH
248
249 String[] output = new String[1];
250 output[0] = String.valueOf("Command not found");
4ea599a5
BH
251 CommandResult result = new CommandResult(0, null);
252 // For verification of setters of class CommandResult
253 result.setOutput(output);
254 result.setResult(1);
255 return result;
d132bcc7 256 }
cfdb727a 257
d132bcc7
BH
258 // ------------------------------------------------------------------------
259 // Helper methods
260 // ------------------------------------------------------------------------
261 private boolean isComment(String line) {
1f2f091b
BH
262 if (line == null) {
263 throw new RuntimeException("line is null"); //$NON-NLS-1$
264 }
d132bcc7
BH
265 return line.matches(COMMENT_KEY);
266 }
267}
This page took 0.041996 seconds and 5 git commands to generate.