001    /*
002     * Copyright (C) 2011 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     *
007     * published by the Free Software Foundation; either version 2.1 of
008     * the License, or (at your option) any later version.
009     *
010     * This software is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013     * Lesser General Public License for more details.
014     *
015     * You should have received a copy of the GNU Lesser General Public
016     * License along with this software; if not, write to the Free
017     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
019     */
020    
021    package org.crsh.term.processor;
022    
023    import org.crsh.shell.ShellProcessContext;
024    import org.crsh.shell.ShellResponse;
025    import org.crsh.term.TermEvent;
026    import org.crsh.util.LatchedFuture;
027    
028    import java.io.IOException;
029    
030    /**
031    * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
032    */
033    class ShellInvoker implements ShellProcessContext {
034    
035      /** . */
036      final LatchedFuture<State> result = new LatchedFuture<State>();
037    
038      /** . */
039      private final Processor processor;
040    
041      public ShellInvoker(Processor processor) {
042        this.processor = processor;
043      }
044    
045      public int getWidth() {
046        return processor.term.getWidth();
047      }
048    
049      public String getProperty(String name) {
050        return processor.term.getProperty(name);
051      }
052    
053      public String readLine(String msg, boolean echo) {
054        try {
055          processor.term.setEcho(echo);
056          processor.term.write(msg);
057          TermEvent action = processor.term.read();
058          CharSequence line = null;
059          if (action instanceof TermEvent.ReadLine) {
060            line = ((TermEvent.ReadLine)action).getLine();
061            processor.log.debug("Read from console");
062          }
063          else {
064            processor.log.debug("Ignoring action " + action + " returning null");
065          }
066          processor.term.write("\r\n");
067          return line.toString();
068        }
069        catch (Exception e) {
070          processor.log.error("Reading from console failed", e);
071          return null;
072        }
073        finally {
074          processor.term.setEcho(true);
075        }
076      }
077    
078      public void end(ShellResponse response) {
079        try {
080    
081          //
082          if (response instanceof ShellResponse.Close) {
083            processor.log.debug("received close response");
084            result.set(State.WANT_CLOSE);
085          } else {
086            if (response instanceof ShellResponse.Cancelled) {
087              result.set(State.OPEN);
088            } else {
089              String ret = response.getText();
090              processor.log.debug("Command completed with result " + ret);
091              try {
092                processor.term.write(ret);
093              }
094              catch (IOException e) {
095                processor.log.error("Write to term failure", e);
096              }
097              processor.process = null;
098            }
099    
100            //
101            processor.writePrompt();
102    
103            //
104            result.set(State.OPEN);
105          }
106        }
107        finally {
108          processor.process = null;
109        }
110      }
111    }