lttng/tmf: Enable and fix parameter assignment warnings
[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
64636df8
BH
187 if (output != null) {
188 commandMap.put(input, new CommandResult(result, output.toArray(new String[output.size()])));
189 }
d132bcc7
BH
190 inOutput = false;
191 } else if (OUTPUT_KEY.equals(strLine)) {
192 // first line of output
193 inOutput = true;
194 strLine = br.readLine();
195
196 // Ignore comments
197 while (isComment(strLine)) {
198 strLine = br.readLine();
199 }
64636df8
BH
200 if (output != null) {
201 output.add(strLine);
202 }
d132bcc7
BH
203 } else if (inOutput) {
204 // subsequent lines of output
64636df8
BH
205 if (output != null) {
206 output.add(strLine);
207 }
cfdb727a
AM
208 }
209// else {
d132bcc7
BH
210// if (RESULT_END_KEY.equals(strLine)) {
211 // nothing to do
212// }
213 }
214 }
215 }
216 //Close the input stream
217 in.close();
218 }
219
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();
226 }
227 }
228
229 /*
230 * (non-Javadoc)
231 * @see org.eclipse.linuxtools.lttng.stubs.service.shells.TestCommandShell#executeCommand(java.lang.String, org.eclipse.core.runtime.IProgressMonitor, boolean)
232 */
d132bcc7 233 @Override
1f2f091b 234 public synchronized ICommandResult executeCommand(String command, IProgressMonitor monitor, boolean checkReturnValue) throws ExecutionException {
d132bcc7 235 Map<String, ICommandResult> commands = fScenarioMap.get(fScenario);
41b5c37f 236 String fullCommand = command;
d132bcc7
BH
237
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);
242 if (i != null) {
243 i++;
244 } else {
245 i = 0;
246 }
247 fSessionNameMap.put(sessionName, i);
41b5c37f 248 fullCommand += String.valueOf(i);
d132bcc7
BH
249 }
250
41b5c37f
AM
251 if (commands.containsKey(fullCommand)) {
252 return commands.get(fullCommand);
cfdb727a 253 }
d132bcc7
BH
254
255 String[] output = new String[1];
41b5c37f 256 output[0] = String.valueOf("Command not found"); //$NON-NLS-1$
4ea599a5
BH
257 CommandResult result = new CommandResult(0, null);
258 // For verification of setters of class CommandResult
259 result.setOutput(output);
260 result.setResult(1);
261 return result;
d132bcc7 262 }
cfdb727a 263
d132bcc7
BH
264 // ------------------------------------------------------------------------
265 // Helper methods
266 // ------------------------------------------------------------------------
0a78d11a 267 private static boolean isComment(String line) {
1f2f091b
BH
268 if (line == null) {
269 throw new RuntimeException("line is null"); //$NON-NLS-1$
270 }
d132bcc7
BH
271 return line.matches(COMMENT_KEY);
272 }
273}
This page took 0.045924 seconds and 5 git commands to generate.