1 16 package org.jmanage.cmdui; 17 18 import org.jmanage.cmdui.util.In; 19 import org.jmanage.cmdui.util.Out; 20 21 import java.io.IOException ; 22 23 28 public class PromptMode implements CommandConstants { 29 30 private final Command authenticatedCommand; 31 32 PromptMode(Command command){ 33 this.authenticatedCommand = command; 34 } 35 36 public void start() 37 throws IOException { 38 39 while(true){ 40 try { 41 42 Command command = getCommand(); 43 44 command.execute(); 45 } catch (Exception e) { 46 handleException(e); 47 } 48 } 49 } 50 51 private Command getCommand() 52 throws IOException , InvalidCommandException { 53 String line = readLine(); 54 return Command.get(line, authenticatedCommand); 55 } 56 57 private String readLine() throws IOException { 58 59 String line = null; 60 while(line == null || line.length() == 0){ 61 Out.print("jmanage>"); 62 line = In.readLine(); 63 if(line != null) line = line.trim(); 64 } 65 return line; 66 } 67 68 private void handleException(Exception e){ 69 if(e instanceof InvalidCommandException){ 70 Out.println(e.getMessage()); 71 }else{ 72 e.printStackTrace(); 73 } 74 Out.println(); 75 } 76 } 77 | Popular Tags |