001 package org.crsh;
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.term.BaseTerm;
007 import org.crsh.term.spi.TermIO;
008 import org.crsh.term.spi.TermIOHandler;
009
010 import java.util.concurrent.ExecutorService;
011 import java.util.concurrent.Executors;
012
013 /**
014 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
015 */
016 public class ProcessorIOHandler extends CRaSHPlugin<TermIOHandler> implements TermIOHandler {
017
018 /** . */
019 private ExecutorService executor;
020
021 @Override
022 public TermIOHandler getImplementation() {
023 return this;
024 }
025
026 @Override
027 public void init() {
028 this.executor = Executors.newFixedThreadPool(3);
029 }
030
031 @Override
032 public void destroy() {
033 if (executor != null) {
034 executor.shutdown();
035 }
036 }
037
038 public void handle(final TermIO io) {
039 final CRaSH shell = new CRaSH(getContext());
040 final AsyncShell asyncShell = new AsyncShell(executor, shell);
041 BaseTerm term = new BaseTerm(io);
042 Processor processor = new Processor(term, asyncShell);
043
044 //
045 processor.addListener(new ProcessorListener() {
046 public void closed() {
047 io.close();
048 }
049 });
050
051 //
052 processor.addListener(new ProcessorListener() {
053 public void closed() {
054 asyncShell.close();
055 }
056 });
057
058 //
059 processor.addListener(new ProcessorListener() {
060 public void closed() {
061 shell.close();
062 }
063 });
064
065 //
066 processor.run();
067 }
068 }