001    /*
002     * Copyright (C) 2010 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.concurrent;
021    
022    import org.crsh.shell.Shell;
023    import org.crsh.shell.ShellProcess;
024    
025    import java.io.Closeable;
026    import java.util.Collections;
027    import java.util.HashSet;
028    import java.util.Map;
029    import java.util.Set;
030    import java.util.concurrent.CompletionService;
031    import java.util.concurrent.Executor;
032    import java.util.concurrent.ExecutorCompletionService;
033    
034    /**
035     * <p></p>The async shell wraps a shell and provides command cancellation. The shell executes the command in a separate thread
036     * allowing using the wrapped shell to which it delegates the business command execution.</p>
037     *
038     *
039     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
040     * @version $Revision$
041     */
042    public class AsyncShell implements Shell, Closeable {
043    
044      /** . */
045      final Shell shell;
046    
047      /** . */
048      private AsyncProcess current;
049    
050      /** . */
051      final CompletionService<AsyncProcess> executor;
052    
053      /** . */
054      boolean closed;
055    
056      /** . */
057      final Object lock = new Object();
058    
059      /** . */
060      final Set<AsyncProcess> processes;
061    
062      public AsyncShell(Executor executor, Shell shell) {
063        this.shell = shell;
064        this.current = null;
065        this.executor = new ExecutorCompletionService<AsyncProcess>(executor);
066        this.closed = false;
067        this.processes = Collections.synchronizedSet(new HashSet<AsyncProcess>());
068      }
069    
070      public void close() {
071    
072        AsyncProcess[] toCancel = null;
073        synchronized (lock) {
074          if (closed) {
075            toCancel = null;
076          } else {
077            closed = true;
078            toCancel = processes.toArray(new AsyncProcess[processes.size()]);
079          }
080        }
081    
082        // Cancel all process
083        if (toCancel != null) {
084          for (AsyncProcess process : toCancel) {
085            process.cancel();
086          }
087        }
088      }
089    
090      // Shell implementation **********************************************************************************************
091    
092      public String getWelcome() {
093        return shell.getWelcome();
094      }
095    
096      public String getPrompt() {
097        return shell.getPrompt();
098      }
099    
100      public Map<String, String> complete(String prefix) {
101        return shell.complete(prefix);
102      }
103    
104      public AsyncProcess createProcess(String request) {
105        synchronized (lock) {
106          if (closed) {
107            throw new IllegalStateException();
108          }
109        }
110        return new AsyncProcess(this, request);
111      }
112    }