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.shell.ShellProcess;
023 import org.crsh.shell.ShellProcessContext;
024 import org.crsh.shell.ShellResponse;
025 import org.crsh.text.Chunk;
026
027 import java.io.IOException;
028 import java.util.concurrent.Callable;
029
030 public class AsyncProcess implements ShellProcess {
031
032
033 /** . */
034 private final String request;
035
036 /** . */
037 private ShellProcessContext caller;
038
039 /** . */
040 private ShellProcess callee;
041
042 /** . */
043 private AsyncShell shell;
044
045 /** . */
046 private Status status;
047
048 /** . */
049 private final Object lock;
050
051 AsyncProcess(AsyncShell shell, String request) {
052 this.shell = shell;
053 this.request = request;
054 this.callee = null;
055 this.status = Status.CONSTRUCTED;
056 this.lock = new Object();
057 }
058
059 public Status getStatus() {
060 return status;
061 }
062
063 /** . */
064 private final ShellProcessContext context = new ShellProcessContext() {
065 public int getWidth() {
066 return caller.getWidth();
067 }
068
069 public int getHeight() {
070 return caller.getHeight();
071 }
072
073 public String getProperty(String name) {
074 return caller.getProperty(name);
075 }
076
077 public boolean takeAlternateBuffer() {
078 return caller.takeAlternateBuffer();
079 }
080
081 public boolean releaseAlternateBuffer() {
082 return caller.releaseAlternateBuffer();
083 }
084
085 public String readLine(String msg, boolean echo) {
086 return caller.readLine(msg, echo);
087 }
088
089 public void provide(Chunk element) throws IOException {
090 caller.provide(element);
091 }
092
093 public void flush() throws IOException {
094 caller.flush();
095 }
096
097 public void end(ShellResponse response) {
098 // Always leave the status in terminated status if the method succeeds
099 // Cancelled -> Terminated
100 // Evaluating -> Terminated
101 // Terminated -> Terminated
102 synchronized (lock) {
103 switch (status) {
104 case CONSTRUCTED:
105 case QUEUED:
106 throw new AssertionError("Should not happen");
107 case CANCELED:
108 // We substitute the response
109 response = ShellResponse.cancelled();
110 status = Status.TERMINATED;
111 break;
112 case EVALUATING:
113 status = Status.TERMINATED;
114 break;
115 case TERMINATED:
116 throw new IllegalStateException("Cannot end a process already terminated");
117 }
118 }
119
120 //
121 caller.end(response);
122 }
123 };
124
125 public void execute(ShellProcessContext processContext) {
126
127 // Constructed -> Queued
128 synchronized (lock) {
129 if (status != Status.CONSTRUCTED) {
130 throw new IllegalStateException("State was " + status);
131 }
132
133 // Update state
134 status = Status.QUEUED;
135 callee = shell.shell.createProcess(request);
136 caller = processContext;
137 }
138
139 // Create the task
140 Callable<AsyncProcess> task = new Callable<AsyncProcess>() {
141 public AsyncProcess call() throws Exception {
142 try {
143 // Cancelled -> Cancelled
144 // Queued -> Evaluating
145 ShellResponse response;
146 synchronized (lock) {
147 switch (status) {
148 case CANCELED:
149 // Do nothing it was canceled in the mean time
150 response = ShellResponse.cancelled();
151 break;
152 case QUEUED:
153 // Ok we are going to run it
154 status = Status.EVALUATING;
155 response = null;
156 break;
157 default:
158 // Just in case but this can only be called by the queue
159 throw new AssertionError();
160 }
161 }
162
163 // Execute the process if we are in evalating state
164 if (response == null) {
165 // Here the status could already be in status cancelled
166 // it is a race condition, execution still happens
167 // but the callback of the callee to the end method will make the process
168 // terminate and use a cancel response
169 try {
170 callee.execute(context);
171 response = ShellResponse.ok();
172 }
173 catch (Throwable t) {
174 response = ShellResponse.internalError("Unexpected throwable when executing process", t);
175 }
176 }
177
178 // Make the callback
179 // Calling terminated twice will have no effect
180 try {
181 context.end(response);
182 }
183 catch (Throwable t) {
184 // Log it
185 }
186
187 // We return this but we don't really care for now
188 return AsyncProcess.this;
189 }
190 finally {
191 synchronized (shell.lock) {
192 shell.processes.remove(AsyncProcess.this);
193 }
194 }
195 }
196 };
197
198 //
199 synchronized (shell.lock) {
200 if (!shell.closed) {
201 shell.executor.submit(task);
202 shell.processes.add(this);
203 } else {
204 boolean invokeEnd;
205 synchronized (lock) {
206 invokeEnd = status != Status.TERMINATED;
207 status = Status.TERMINATED;
208 }
209 if (invokeEnd) {
210 caller.end(ShellResponse.cancelled());
211 }
212 }
213 }
214 }
215
216 public void cancel() {
217 // Construcuted -> ISE
218 // Evaluating -> Canceled
219 // Queued -> Canceled
220 // Cancelled -> Cancelled
221 // Terminated -> Terminated
222 boolean cancel;
223 synchronized (lock) {
224 switch (status) {
225 case CONSTRUCTED:
226 throw new IllegalStateException("Cannot call cancel on process that was not scheduled for execution yet");
227 case QUEUED:
228 status = Status.CANCELED;
229 cancel = false;
230 break;
231 case EVALUATING:
232 status = Status.CANCELED;
233 cancel = true;
234 break;
235 default:
236 cancel = false;
237 break;
238 }
239 }
240
241 //
242 if (cancel) {
243 callee.cancel();
244 }
245 }
246 }