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.security.Principal;
012    import java.util.concurrent.ExecutorService;
013    import java.util.concurrent.Executors;
014    
015    /**
016     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
017     */
018    public class ProcessorIOHandler extends CRaSHPlugin<TermIOHandler> implements TermIOHandler {
019    
020      /** . */
021      private ExecutorService executor;
022    
023      /** . */
024      private CRaSH crash;
025    
026      @Override
027      public TermIOHandler getImplementation() {
028        return this;
029      }
030    
031      @Override
032      public void init() {
033        this.executor = Executors.newFixedThreadPool(3);
034        this.crash = new CRaSH(getContext());
035      }
036    
037      @Override
038      public void destroy() {
039        if (executor != null) {
040          executor.shutdown();
041        }
042      }
043    
044      public void handle(final TermIO io, Principal user) {
045        CRaSHSession shell = crash.createSession(user);
046        AsyncShell asyncShell = new AsyncShell(executor, shell);
047        BaseTerm term = new BaseTerm(io);
048        Processor processor = new Processor(term, asyncShell);
049        processor.addListener(io);
050        processor.addListener(asyncShell);
051        processor.addListener(shell);
052    
053        //
054        processor.run();
055      }
056    }