1 11 12 package org.eclipse.osgi.framework.internal.core; 13 14 import java.io.*; 15 import java.lang.reflect.*; 16 import java.net.URL ; 17 import java.util.*; 18 import org.eclipse.osgi.framework.console.CommandInterpreter; 19 import org.eclipse.osgi.framework.console.CommandProvider; 20 import org.eclipse.osgi.util.NLS; 21 import org.osgi.framework.Bundle; 22 23 33 public class FrameworkCommandInterpreter implements CommandInterpreter { 34 private static final String WS_DELIM = " \t\n\r\f"; 36 37 private StringTokenizer tok; 38 39 private Object [] commandProviders; 40 41 private FrameworkConsole con; 42 43 private PrintWriter out; 44 45 46 private String tab = "\t"; private String newline = "\r\n"; private boolean firstCommand = true; 49 50 54 protected static int maxLineCount; 55 56 57 protected int currentLineCount; 58 59 63 public FrameworkCommandInterpreter(String cmdline, Object [] commandProviders, FrameworkConsole con) { 64 tok = new StringTokenizer(cmdline); 65 this.commandProviders = commandProviders; 66 this.con = con; 67 this.out = con.getWriter(); 68 } 69 70 78 public String nextArgument() { 79 if (tok == null || !tok.hasMoreElements()) 80 return null; 81 82 String arg = tok.nextToken(); 83 if (arg.startsWith("\"")) { if (arg.endsWith("\"")) { if (arg.length() >= 2) 86 return arg.substring(1, arg.length() - 1); 88 } 89 String remainingArg = tok.nextToken("\""); arg = arg.substring(1) + remainingArg; 91 tok.nextToken(WS_DELIM); 93 } else if (arg.startsWith("'")) { if (arg.endsWith("'")) { if (arg.length() >= 2) 96 return arg.substring(1, arg.length() - 1); 98 } 99 String remainingArg = tok.nextToken("'"); arg = arg.substring(1) + remainingArg; 101 tok.nextToken(WS_DELIM); 103 } 104 return arg; 105 } 106 107 118 public Object execute(String cmd) { 119 if (!firstCommand) 120 return innerExecute(cmd); 121 firstCommand = false; 122 resetLineCount(); 123 Object retval = null; 124 if (cmd.equalsIgnoreCase("more")) { try { 127 _more(); 128 } catch (Exception e) { 129 printStackTrace(e); 130 } 131 return retval; 132 } 133 if (cmd.equalsIgnoreCase("disconnect") && con.getUseSocketStream()) { try { 136 _disconnect(); 137 } catch (Exception e) { 138 printStackTrace(e); 139 } 140 return retval; 141 } 142 Class [] parameterTypes = new Class [] {CommandInterpreter.class}; 143 Object [] parameters = new Object [] {this}; 144 boolean executed = false; 145 int size = commandProviders.length; 146 for (int i = 0; !executed && (i < size); i++) { 147 try { 148 Object target = commandProviders[i]; 149 Method method = target.getClass().getMethod("_" + cmd, parameterTypes); retval = method.invoke(target, parameters); 151 executed = true; } catch (NoSuchMethodException ite) { 153 } catch (InvocationTargetException ite) { 155 executed = true; printStackTrace(ite.getTargetException()); 157 } catch (Exception ee) { 158 executed = true; printStackTrace(ee); 160 } 161 } 162 if (!executed) { 164 for (int i = 0; i < size; i++) { 165 try { 166 CommandProvider commandProvider = (CommandProvider) commandProviders[i]; 167 out.print(commandProvider.getHelp()); 168 out.flush(); 169 } catch (Exception ee) { 170 printStackTrace(ee); 171 } 172 } 173 out.print(getHelp()); 175 out.flush(); 176 } 177 return retval; 178 } 179 180 private Object innerExecute(String cmd) { 181 if (cmd != null && cmd.length() > 0) { 182 CommandInterpreter intcp = new FrameworkCommandInterpreter(cmd, commandProviders, con); 183 String command = intcp.nextArgument(); 184 if (command != null) 185 return intcp.execute(command); 186 } 187 return null; 188 } 189 190 196 private int getMaximumLinesToScroll() { 197 return maxLineCount; 198 } 199 200 212 private void setMaximumLinesToScroll(int lines) { 213 if (lines < 0) { 214 throw new IllegalArgumentException (ConsoleMsg.CONSOLE_LINES_TO_SCROLL_NEGATIVE_ERROR); 215 } 216 217 maxLineCount = lines; 218 } 219 220 223 private void resetLineCount() { 224 currentLineCount = 0; 225 } 226 227 234 private void printline(Object o) { 235 print(o + newline); 236 } 237 238 243 public void print(Object o) { 244 synchronized (out) { 245 check4More(); 246 out.print(o); 247 out.flush(); 248 } 249 } 250 251 254 public void println() { 255 println(""); } 257 258 262 public void printStackTrace(Throwable t) { 263 t.printStackTrace(out); 264 265 Method[] methods = t.getClass().getMethods(); 266 267 int size = methods.length; 268 Class throwable = Throwable .class; 269 270 for (int i = 0; i < size; i++) { 271 Method method = methods[i]; 272 273 if (Modifier.isPublic(method.getModifiers()) && method.getName().startsWith("get") && throwable.isAssignableFrom(method.getReturnType()) && (method.getParameterTypes().length == 0)) { try { 275 Throwable nested = (Throwable ) method.invoke(t, null); 276 277 if ((nested != null) && (nested != t)) { 278 out.println(ConsoleMsg.CONSOLE_NESTED_EXCEPTION); 279 printStackTrace(nested); 280 } 281 } catch (IllegalAccessException e) { 282 } catch (InvocationTargetException e) { 283 } 284 } 285 } 286 } 287 288 299 public void println(Object o) { 300 if (o == null) { 301 return; 302 } 303 synchronized (out) { 304 check4More(); 305 printline(o); 306 currentLineCount++; 307 currentLineCount += o.toString().length() / 80; 308 } 309 } 310 311 317 public void printDictionary(Dictionary dic, String title) { 318 if (dic == null) 319 return; 320 321 int count = dic.size(); 322 String [] keys = new String [count]; 323 Enumeration keysEnum = dic.keys(); 324 int i = 0; 325 while (keysEnum.hasMoreElements()) { 326 keys[i++] = (String ) keysEnum.nextElement(); 327 } 328 Util.sort(keys); 329 330 if (title != null) { 331 println(title); 332 } 333 for (i = 0; i < count; i++) { 334 println(" " + keys[i] + " = " + dic.get(keys[i])); } 336 println(); 337 } 338 339 345 public void printBundleResource(Bundle bundle, String resource) { 346 URL entry = null; 347 entry = bundle.getEntry(resource); 348 if (entry != null) { 349 try { 350 println(resource); 351 InputStream in = entry.openStream(); 352 byte[] buffer = new byte[1024]; 353 int read = 0; 354 try { 355 while ((read = in.read(buffer)) != -1) 356 print(new String (buffer, 0, read)); 357 } finally { 358 if (in != null) { 359 try { 360 in.close(); 361 } catch (IOException e) { 362 } 363 } 364 } 365 } catch (Exception e) { 366 System.err.println(NLS.bind(ConsoleMsg.CONSOLE_ERROR_READING_RESOURCE, resource)); 367 } 368 } else { 369 println(NLS.bind(ConsoleMsg.CONSOLE_RESOURCE_NOT_IN_BUNDLE, resource, bundle.toString())); 370 } 371 } 372 373 378 private void check4More() { 379 int max = getMaximumLinesToScroll(); 380 if (max > 0) { 381 if (currentLineCount >= max) { 382 out.print(ConsoleMsg.CONSOLE_MORE); 383 out.flush(); 384 con.getInput(); resetLineCount(); } 387 } 388 } 389 390 394 public String getHelp() { 395 StringBuffer help = new StringBuffer (256); 396 help.append(newline); 397 help.append(ConsoleMsg.CONSOLE_HELP_CONTROLLING_CONSOLE_HEADING); 398 help.append(newline); 399 help.append(tab); 400 help.append("more - "); help.append(ConsoleMsg.CONSOLE_HELP_MORE); 402 if (con.getUseSocketStream()) { 403 help.append(newline); 404 help.append(tab); 405 help.append("disconnect - "); help.append(ConsoleMsg.CONSOLE_HELP_DISCONNECT); 407 } 408 return help.toString(); 409 } 410 411 415 public void _more() throws Exception { 416 if (confirm(ConsoleMsg.CONSOLE_CONFIRM_MORE, true)) { 417 int lines = prompt(newline + ConsoleMsg.CONSOLE_MORE_ENTER_LINES, 24); 418 setMaximumLinesToScroll(lines); 419 } else { 420 setMaximumLinesToScroll(0); 421 } 422 } 423 424 private void _disconnect() throws Exception { 425 if (confirm(ConsoleMsg.CONSOLE_CONFIRM_DISCONNECT, true)) { 426 con.disconnect(); 427 } 428 } 429 430 438 protected boolean confirm(String string, boolean defaultAnswer) { 439 synchronized (out) { 440 if (string.length() > 0) { 441 print(string); 442 } else { 443 print(ConsoleMsg.CONSOLE_CONFIRM); 444 } 445 print(" (" + ConsoleMsg.CONSOLE_CONFIRM_VALUES); if (defaultAnswer) { 447 print(ConsoleMsg.CONSOLE_Y + ") "); } else { 449 print(ConsoleMsg.CONSOLE_N + ") "); } 451 } 452 String input = con.getInput(); 453 resetLineCount(); 454 if (input.length() == 0) { 455 return defaultAnswer; 456 } 457 return input.toLowerCase().charAt(0) == ConsoleMsg.CONSOLE_Y.charAt(0); 458 } 459 460 469 protected String prompt(String string, String defaultAnswer) { 470 if (string.length() > 0) { 471 if (defaultAnswer.length() > 0) { 472 StringBuffer buf = new StringBuffer (256); 473 buf.append(string); 474 buf.append(" "); buf.append(ConsoleMsg.CONSOLE_PROMPT_DEFAULT); 476 buf.append("="); buf.append(defaultAnswer); 478 buf.append(") "); print(buf.toString()); 480 } else { 481 print(string); 482 } 483 } 484 String input = con.getInput(); 485 resetLineCount(); 486 if (input.length() > 0) { 487 return input; 488 } 489 return defaultAnswer; 490 } 491 492 501 protected int prompt(String string, int defaultAnswer) { 502 Integer i = new Integer (defaultAnswer); 503 int answer; 504 for (int j = 0; j < 3; j++) { 505 String s = prompt(string, i.toString()); 506 try { 507 answer = Integer.parseInt(s); 508 if (answer >= 0) { 509 return answer; 510 } 511 } catch (NumberFormatException e) { 512 } 513 println(ConsoleMsg.CONSOLE_INVALID_INPUT); 514 } 515 println(ConsoleMsg.CONSOLE_TOO_MUCH_INVALID_INPUT); 516 return defaultAnswer; 517 } 518 } 519 | Popular Tags |