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