| 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 | @SuppressWarnings("unused") |
| 44 | private final static String RESULT_END_KEY = "</COMMAND_RESULT>"; |
| 45 | private final static String OUTPUT_KEY = "<COMMAND_OUTPUT>"; |
| 46 | private final static String OUTPUT_END_KEY = "</COMMAND_OUTPUT>"; |
| 47 | private final static String COMMENT_KEY = "#.*"; |
| 48 | |
| 49 | private final static Pattern LTTNG_LIST_SESSION_PATTERN = Pattern.compile("lttng\\s+list\\s+(.+)"); |
| 50 | private final static String LTTNG_LIST_PROVIDER_PATTERN = "lttng\\s+list\\s+(-u|-k)"; |
| 51 | |
| 52 | // ------------------------------------------------------------------------ |
| 53 | // Attributes |
| 54 | // ------------------------------------------------------------------------ |
| 55 | private String fScenariofile; |
| 56 | private String fScenario; |
| 57 | |
| 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>(); |
| 60 | |
| 61 | /** |
| 62 | * Parse a scenario file with the format: |
| 63 | * <SCENARIO> |
| 64 | * ScenarioName |
| 65 | * |
| 66 | * <COMMAND_INPUT> |
| 67 | * Command |
| 68 | * </COMAND_INPUT> |
| 69 | * |
| 70 | * <COMMAND_RESULT> |
| 71 | * CommandResult |
| 72 | * </COMMAND_RESULT> |
| 73 | * |
| 74 | * <COMMAND_OUTPUT> |
| 75 | * CommandOutput |
| 76 | * </COMMAND_OUTPUT> |
| 77 | * |
| 78 | * </SCENARIO> |
| 79 | * |
| 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) |
| 84 | * |
| 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) |
| 88 | * |
| 89 | * @param scenariofile - path to scenario file |
| 90 | * @throws Exception |
| 91 | */ |
| 92 | public synchronized void loadScenarioFile(String scenariofile) throws Exception { |
| 93 | fScenariofile = scenariofile; |
| 94 | |
| 95 | // clean up map |
| 96 | Collection<Map<String, ICommandResult>> values = fScenarioMap.values(); |
| 97 | for (Iterator<Map<String, ICommandResult>> iterator = values.iterator(); iterator.hasNext();) { |
| 98 | Map<String, ICommandResult> map = iterator.next(); |
| 99 | map.clear(); |
| 100 | } |
| 101 | fScenarioMap.clear(); |
| 102 | |
| 103 | // load from file |
| 104 | |
| 105 | // Open the file |
| 106 | FileInputStream fstream = new FileInputStream(fScenariofile); |
| 107 | |
| 108 | // Get the object of DataInputStream |
| 109 | DataInputStream in = new DataInputStream(fstream); |
| 110 | BufferedReader br = new BufferedReader(new InputStreamReader(in)); |
| 111 | String strLine; |
| 112 | |
| 113 | // Read File Line by Line |
| 114 | |
| 115 | // Temporary map for generating instance numbers for lttng list <session> commands. |
| 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) { |
| 143 | // Ignore comments |
| 144 | if(isComment(strLine)) { |
| 145 | continue; |
| 146 | } |
| 147 | |
| 148 | if (SCENARIO_END_KEY.equals(strLine)) { |
| 149 | // Scenario is finished |
| 150 | break; |
| 151 | } |
| 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; |
| 160 | |
| 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 | if (output != null) { |
| 188 | commandMap.put(input, new CommandResult(result, output.toArray(new String[output.size()]))); |
| 189 | } |
| 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 | } |
| 200 | if (output != null) { |
| 201 | output.add(strLine); |
| 202 | } |
| 203 | } else if (inOutput) { |
| 204 | // subsequent lines of output |
| 205 | if (output != null) { |
| 206 | output.add(strLine); |
| 207 | } |
| 208 | } |
| 209 | // else { |
| 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 | @Override |
| 230 | public synchronized ICommandResult executeCommand(String command, IProgressMonitor monitor, boolean checkReturnValue) throws ExecutionException { |
| 231 | Map<String, ICommandResult> commands = fScenarioMap.get(fScenario); |
| 232 | String fullCommand = command; |
| 233 | |
| 234 | Matcher matcher = LTTNG_LIST_SESSION_PATTERN.matcher(command); |
| 235 | if (matcher.matches() && !command.matches(LTTNG_LIST_PROVIDER_PATTERN)) { |
| 236 | String sessionName = matcher.group(1).trim(); |
| 237 | Integer i = fSessionNameMap.get(sessionName); |
| 238 | if (i != null) { |
| 239 | i++; |
| 240 | } else { |
| 241 | i = 0; |
| 242 | } |
| 243 | fSessionNameMap.put(sessionName, i); |
| 244 | fullCommand += String.valueOf(i); |
| 245 | } |
| 246 | |
| 247 | if (commands.containsKey(fullCommand)) { |
| 248 | return commands.get(fullCommand); |
| 249 | } |
| 250 | |
| 251 | String[] output = new String[1]; |
| 252 | output[0] = String.valueOf("Command not found"); |
| 253 | CommandResult result = new CommandResult(0, null); |
| 254 | // For verification of setters of class CommandResult |
| 255 | result.setOutput(output); |
| 256 | result.setResult(1); |
| 257 | return result; |
| 258 | } |
| 259 | |
| 260 | // ------------------------------------------------------------------------ |
| 261 | // Helper methods |
| 262 | // ------------------------------------------------------------------------ |
| 263 | |
| 264 | private static boolean isComment(String line) { |
| 265 | if (line == null) { |
| 266 | throw new RuntimeException("line is null"); |
| 267 | } |
| 268 | return line.matches(COMMENT_KEY); |
| 269 | } |
| 270 | } |