001    package org.crsh.term.processor;
002    
003    import org.crsh.plugin.CRaSHPlugin;
004    import org.crsh.shell.concurrent.AsyncShell;
005    import org.crsh.shell.impl.CRaSH;
006    import org.crsh.shell.impl.CRaSHSession;
007    import org.crsh.term.BaseTerm;
008    import org.crsh.term.spi.TermIO;
009    import org.crsh.term.spi.TermIOHandler;
010    
011    import java.util.concurrent.ExecutorService;
012    import java.util.concurrent.Executors;
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 ExecutorService executor;
021    
022      /** . */
023      private CRaSH crash;
024    
025      @Override
026      public TermIOHandler getImplementation() {
027        return this;
028      }
029    
030      @Override
031      public void init() {
032        this.executor = Executors.newFixedThreadPool(3);
033        this.crash = new CRaSH(getContext());
034      }
035    
036      @Override
037      public void destroy() {
038        if (executor != null) {
039          executor.shutdown();
040        }
041      }
042    
043      public void handle(final TermIO io) {
044        CRaSHSession shell = crash.createSession();
045        AsyncShell asyncShell = new AsyncShell(executor, shell);
046        BaseTerm term = new BaseTerm(io);
047        Processor processor = new Processor(term, asyncShell);
048        processor.addListener(io);
049        processor.addListener(asyncShell);
050        processor.addListener(shell);
051    
052        //
053        processor.run();
054      }
055    }