lttng: Move to Java 7 and fix warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui.tests / stubs / org / eclipse / linuxtools / internal / lttng2 / stubs / shells / LTTngToolsFileShell.java
1 /**********************************************************************
2 * Copyright (c) 2012, 2013 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12 package org.eclipse.linuxtools.internal.lttng2.stubs.shells;
13
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;
23 import java.util.Map;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
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;
31
32 @SuppressWarnings("javadoc")
33 public class LTTngToolsFileShell extends TestCommandShell {
34
35 // ------------------------------------------------------------------------
36 // CONSTANTS
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 = "#.*";
46
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)";
49
50 // ------------------------------------------------------------------------
51 // Attributes
52 // ------------------------------------------------------------------------
53 private String fScenariofile;
54 private String fScenario;
55
56 private final Map<String, Map<String, ICommandResult>> fScenarioMap = new HashMap<>();
57 private final Map<String, Integer> fSessionNameMap = new HashMap<>();
58
59 /**
60 * Parse a scenario file with the format:
61 * <SCENARIO>
62 * ScenarioName
63 *
64 * <COMMAND_INPUT>
65 * Command
66 * </COMAND_INPUT>
67 *
68 * <COMMAND_RESULT>
69 * CommandResult
70 * </COMMAND_RESULT>
71 *
72 * <COMMAND_OUTPUT>
73 * CommandOutput
74 * </COMMAND_OUTPUT>
75 *
76 * </SCENARIO>
77 *
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)
82 *
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)
86 *
87 * @param scenariofile - path to scenario file
88 * @throws Exception
89 */
90 public synchronized void loadScenarioFile(String scenariofile) throws Exception {
91 fScenariofile = scenariofile;
92
93 // clean up map
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();
97 map.clear();
98 }
99 fScenarioMap.clear();
100
101 // load from file
102
103 // Open the file
104 try (FileInputStream fstream = new FileInputStream(fScenariofile);
105 DataInputStream in = new DataInputStream(fstream);
106 BufferedReader br = new BufferedReader(new InputStreamReader(in));) {
107 String strLine;
108
109 // Read File Line by Line
110
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) {
116
117 // Ignore comments
118 if (isComment(strLine)) {
119 continue;
120 }
121
122 if (SCENARIO_KEY.equals(strLine)) {
123 // scenario start
124
125 // Ignore comments
126 strLine = br.readLine();
127 while (isComment(strLine)) {
128 strLine = br.readLine();
129 }
130
131 String scenario = strLine;
132 Map<String, ICommandResult> commandMap = new HashMap<>();
133 fScenarioMap.put(scenario, commandMap);
134 List<String> output = null;
135 String input = null;
136 boolean inOutput = false;
137 int result = 0;
138 tmpSessionNameMap.clear();
139 while ((strLine = br.readLine()) != null) {
140 // Ignore comments
141 if (isComment(strLine)) {
142 continue;
143 }
144
145 if (SCENARIO_END_KEY.equals(strLine)) {
146 // Scenario is finished
147 break;
148 }
149 if (INPUT_KEY.equals(strLine)) {
150 strLine = br.readLine();
151 // Ignore comments
152 while (isComment(strLine)) {
153 strLine = br.readLine();
154 }
155 // Read command
156 input = strLine;
157
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);
164 if (i != null) {
165 i++;
166 } else {
167 i = 0;
168 }
169 tmpSessionNameMap.put(sessionName, i);
170 input += String.valueOf(i);
171 }
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();
177 // Ignore comments
178 while (isComment(strLine)) {
179 strLine = br.readLine();
180 }
181 // Save result value
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()])));
187 }
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 if (output != null) {
199 output.add(strLine);
200 }
201 } else if (inOutput) {
202 // subsequent lines of output
203 if (output != null) {
204 output.add(strLine);
205 }
206 }
207 // else {
208 // if (RESULT_END_KEY.equals(strLine)) {
209 // nothing to do
210 // }
211 }
212 }
213 }
214 }
215 }
216
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();
223 }
224 }
225
226 @Override
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;
230
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);
235 if (i != null) {
236 i++;
237 } else {
238 i = 0;
239 }
240 fSessionNameMap.put(sessionName, i);
241 fullCommand += String.valueOf(i);
242 }
243
244 if (commands.containsKey(fullCommand)) {
245 return commands.get(fullCommand);
246 }
247
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);
253 result.setResult(1);
254 return result;
255 }
256
257 // ------------------------------------------------------------------------
258 // Helper methods
259 // ------------------------------------------------------------------------
260
261 private static boolean isComment(String line) {
262 if (line == null) {
263 throw new RuntimeException("line is null");
264 }
265 return line.matches(COMMENT_KEY);
266 }
267 }
This page took 0.054922 seconds and 5 git commands to generate.