001    /*
002     * Copyright (C) 2012 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     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.shell.impl.async;
021    
022    import org.crsh.cli.impl.completion.CompletionMatch;
023    import org.crsh.shell.Shell;
024    
025    import java.io.Closeable;
026    import java.util.Collections;
027    import java.util.HashSet;
028    import java.util.Set;
029    import java.util.concurrent.CompletionService;
030    import java.util.concurrent.Executor;
031    import java.util.concurrent.ExecutorCompletionService;
032    
033    public class AsyncShell implements Shell, Closeable {
034    
035      /** . */
036      final Shell shell;
037    
038      /** . */
039      private AsyncProcess current;
040    
041      /** . */
042      final CompletionService<AsyncProcess> executor;
043    
044      /** . */
045      boolean closed;
046    
047      /** . */
048      final Object lock = new Object();
049    
050      /** . */
051      final Set<AsyncProcess> processes;
052    
053      public AsyncShell(Executor executor, Shell shell) {
054        this.shell = shell;
055        this.current = null;
056        this.executor = new ExecutorCompletionService<AsyncProcess>(executor);
057        this.closed = false;
058        this.processes = Collections.synchronizedSet(new HashSet<AsyncProcess>());
059      }
060    
061      public void close() {
062    
063        AsyncProcess[] toCancel = null;
064        synchronized (lock) {
065          if (closed) {
066            toCancel = null;
067          } else {
068            closed = true;
069            toCancel = processes.toArray(new AsyncProcess[processes.size()]);
070          }
071        }
072    
073        // Cancel all process
074        if (toCancel != null) {
075          for (AsyncProcess process : toCancel) {
076            process.cancel();
077          }
078        }
079      }
080    
081      // Shell implementation **********************************************************************************************
082    
083      public String getWelcome() {
084        return shell.getWelcome();
085      }
086    
087      public String getPrompt() {
088        return shell.getPrompt();
089      }
090    
091      public CompletionMatch complete(String prefix) {
092        return shell.complete(prefix);
093      }
094    
095      public AsyncProcess createProcess(String request) {
096        synchronized (lock) {
097          if (closed) {
098            throw new IllegalStateException();
099          }
100        }
101        return new AsyncProcess(this, request);
102      }
103    }