A process execution state machine. @author Julien Viet
| 34 | * @author Julien Viet |
| 35 | */ |
| 36 | class ProcessHandler extends Plugin implements ShellProcessContext { |
| 37 | |
| 38 | /** |
| 39 | * A thread reading a line. |
| 40 | */ |
| 41 | class Reader { |
| 42 | final Thread thread; |
| 43 | final Editor editor; |
| 44 | final ArrayBlockingQueue<String> line; |
| 45 | Reader(Thread thread, boolean echo) { |
| 46 | this.thread = thread; |
| 47 | this.editor = new Editor(console, echo); |
| 48 | this.line = new ArrayBlockingQueue<String>(1); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** . */ |
| 53 | final Console console; |
| 54 | |
| 55 | /** . */ |
| 56 | final ShellProcess process; |
| 57 | |
| 58 | /** Weather or not a thread is reading a line callback. */ |
| 59 | final AtomicReference<Reader> editor; |
| 60 | |
| 61 | ProcessHandler(Console console, ShellProcess process) { |
| 62 | this.console = console; |
| 63 | this.process = process; |
| 64 | this.editor = new AtomicReference<Reader>(); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public boolean takeAlternateBuffer() throws IOException { |
| 69 | return console.driver.takeAlternateBuffer(); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public boolean releaseAlternateBuffer() throws IOException { |
| 74 | return console.driver.releaseAlternateBuffer(); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public String getProperty(String propertyName) { |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public String readLine(String msg, boolean echo) throws IOException, InterruptedException { |
| 84 | Reader waiter = new Reader(Thread.currentThread(), echo); |
| 85 | if (editor.compareAndSet(null, waiter)) { |
| 86 | if (msg != null && msg.length() > 0) { |
| 87 | console.driver.write(msg); |
| 88 | console.driver.flush(); |
| 89 | } |
| 90 | console.iterate(); |
| 91 | try { |
| 92 | return waiter.line.take(); |
| 93 | } finally { |
nothing calls this directly
no outgoing calls
no test coverage detected