001    package org.crsh.processor.term;
002    
003    import org.crsh.plugin.CRaSHPlugin;
004    import org.crsh.shell.Shell;
005    import org.crsh.shell.ShellFactory;
006    import org.crsh.shell.impl.async.AsyncShell;
007    import org.crsh.term.BaseTerm;
008    import org.crsh.term.spi.TermIO;
009    import org.crsh.term.spi.TermIOHandler;
010    
011    import java.io.Closeable;
012    import java.security.Principal;
013    
014    /**
015     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
016     */
017    public class ProcessorIOHandler extends CRaSHPlugin<TermIOHandler> implements TermIOHandler {
018    
019      /** . */
020      private ShellFactory factory;
021    
022      @Override
023      public TermIOHandler getImplementation() {
024        return this;
025      }
026    
027      @Override
028      public void init() {
029        this.factory = getContext().getPlugin(ShellFactory.class);
030      }
031    
032      @Override
033      public void destroy() {
034      }
035    
036      public void handle(final TermIO io, Principal user) {
037        Shell shell = factory.create(user);
038        AsyncShell asyncShell = new AsyncShell(getContext().getExecutor(), shell);
039        BaseTerm term = new BaseTerm(io);
040        Processor processor = new Processor(term, asyncShell);
041        processor.addListener(io);
042        processor.addListener(asyncShell);
043        if (shell instanceof Closeable) {
044          processor.addListener((Closeable)shell);
045        }
046        processor.run();
047      }
048    }