Commit | Line | Data |
---|---|---|
d132bcc7 BH |
1 | /********************************************************************** |
2 | * Copyright (c) 2012 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.lttng.stubs.service.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.lttng.ui.views.control.service.CommandResult; | |
30 | import org.eclipse.linuxtools.lttng.ui.views.control.service.ICommandResult; | |
31 | ||
32 | public class LTTngToolsFileShell extends TestCommandShell { | |
33 | ||
34 | // ------------------------------------------------------------------------ | |
35 | // CONSTANTS | |
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$ | |
47 | ||
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$ | |
50 | ||
51 | // ------------------------------------------------------------------------ | |
52 | // Attributes | |
53 | // ------------------------------------------------------------------------ | |
54 | private String fScenariofile; | |
55 | private String fScenario; | |
56 | ||
57 | private Map<String, Map<String, ICommandResult>> fScenarioMap = new HashMap<String, Map<String, ICommandResult>>(); | |
58 | private Map<String, Integer> fSessionNameMap = new HashMap<String, Integer>(); | |
59 | ||
60 | /** | |
61 | * Parse a scenario file with the format: | |
62 | * <SCENARIO> | |
63 | * ScenarioName | |
64 | * | |
65 | * <COMMAND_INPUT> | |
66 | * Command | |
67 | * </COMAND_INPUT> | |
68 | * | |
69 | * <COMMAND_RESULT> | |
70 | * CommandResult | |
71 | * </COMMAND_RESULT> | |
72 | * | |
73 | * <COMMAND_OUTPUT> | |
74 | * CommandOutput | |
75 | * </COMMAND_OUTPUT> | |
76 | * | |
77 | * </SCENARIO> | |
78 | * | |
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) | |
83 | * | |
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) | |
87 | * | |
88 | * @param scenariofile - path to scenario file | |
89 | * @throws Exception | |
90 | */ | |
91 | public void loadScenarioFile(String scenariofile) throws Exception { | |
92 | fScenariofile = scenariofile; | |
93 | ||
94 | // clean up map | |
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(); | |
98 | map.clear(); | |
99 | } | |
100 | fScenarioMap.clear(); | |
101 | ||
102 | // load from file | |
103 | ||
104 | // Open the file | |
105 | FileInputStream fstream = new FileInputStream(fScenariofile); | |
106 | ||
107 | // Get the object of DataInputStream | |
108 | DataInputStream in = new DataInputStream(fstream); | |
109 | BufferedReader br = new BufferedReader(new InputStreamReader(in)); | |
110 | String strLine; | |
111 | ||
112 | // Read File Line by Line | |
113 | ||
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) { | |
118 | ||
119 | // Ignore comments | |
120 | if(isComment(strLine)) { | |
121 | continue; | |
122 | } | |
123 | ||
124 | if (SCENARIO_KEY.equals(strLine)) { | |
125 | // scenario start | |
126 | ||
127 | // Ignore comments | |
128 | strLine = br.readLine(); | |
129 | while (isComment(strLine)) { | |
130 | strLine = br.readLine(); | |
131 | } | |
132 | ||
133 | String scenario = strLine; | |
134 | Map<String, ICommandResult> commandMap = new HashMap<String, ICommandResult>(); | |
135 | fScenarioMap.put(scenario, commandMap); | |
136 | List<String> output = null; | |
137 | String input = null; | |
138 | boolean inOutput = false; | |
139 | int result = 0; | |
140 | tmpSessionNameMap.clear(); | |
141 | while ((strLine = br.readLine()) != null) { | |
142 | // Ignore comments | |
143 | if(isComment(strLine)) { | |
144 | continue; | |
145 | } | |
146 | ||
147 | if (SCENARIO_END_KEY.equals(strLine)) { | |
148 | // Scenario is finished | |
149 | break; | |
150 | } | |
151 | if (INPUT_KEY.equals(strLine)) { | |
152 | strLine = br.readLine(); | |
153 | // Ignore comments | |
154 | while (isComment(strLine)) { | |
155 | strLine = br.readLine(); | |
156 | } | |
157 | // Read command | |
158 | input = strLine; | |
159 | ||
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); | |
165 | if (i != null) { | |
166 | i++; | |
167 | } else { | |
168 | i = 0; | |
169 | } | |
170 | tmpSessionNameMap.put(sessionName, i); | |
171 | input += String.valueOf(i); | |
172 | } | |
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(); | |
178 | // Ignore comments | |
179 | while (isComment(strLine)) { | |
180 | strLine = br.readLine(); | |
181 | } | |
182 | // Save result value | |
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()]))); | |
187 | inOutput = false; | |
188 | } else if (OUTPUT_KEY.equals(strLine)) { | |
189 | // first line of output | |
190 | inOutput = true; | |
191 | strLine = br.readLine(); | |
192 | ||
193 | // Ignore comments | |
194 | while (isComment(strLine)) { | |
195 | strLine = br.readLine(); | |
196 | } | |
197 | output.add(strLine); | |
198 | } else if (inOutput) { | |
199 | // subsequent lines of output | |
200 | output.add(strLine); | |
201 | } | |
202 | // else { | |
203 | // if (RESULT_END_KEY.equals(strLine)) { | |
204 | // nothing to do | |
205 | // } | |
206 | } | |
207 | } | |
208 | } | |
209 | //Close the input stream | |
210 | in.close(); | |
211 | } | |
212 | ||
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(); | |
219 | } | |
220 | } | |
221 | ||
222 | /* | |
223 | * (non-Javadoc) | |
224 | * @see org.eclipse.linuxtools.lttng.stubs.service.shells.TestCommandShell#executeCommand(java.lang.String, org.eclipse.core.runtime.IProgressMonitor, boolean) | |
225 | */ | |
226 | @SuppressWarnings("nls") | |
227 | @Override | |
228 | public ICommandResult executeCommand(String command, IProgressMonitor monitor, boolean checkReturnValue) throws ExecutionException { | |
229 | Map<String, ICommandResult> commands = fScenarioMap.get(fScenario); | |
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 | command += String.valueOf(i); | |
242 | } | |
243 | ||
244 | if (commands.containsKey(command)) { | |
245 | return commands.get(command); | |
246 | } | |
247 | ||
248 | String[] output = new String[1]; | |
249 | output[0] = String.valueOf("Command not found"); | |
250 | return new CommandResult(1, output); | |
251 | } | |
252 | ||
253 | // ------------------------------------------------------------------------ | |
254 | // Helper methods | |
255 | // ------------------------------------------------------------------------ | |
256 | private boolean isComment(String line) { | |
257 | return line.matches(COMMENT_KEY); | |
258 | } | |
259 | } |