Mercurial Hosting > luan
annotate src/org/eclipse/jetty/server/AsyncContinuation.java @ 918:7b62446899c6
remove unused states in AsyncContinuation
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 09 Oct 2016 03:37:53 -0600 |
parents | 1fc8ee20cb18 |
children | dd6b1f079634 |
rev | line source |
---|---|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.server; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 import javax.servlet.AsyncContext; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 import javax.servlet.AsyncEvent; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 import javax.servlet.AsyncListener; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 import javax.servlet.RequestDispatcher; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 import javax.servlet.ServletException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 import java.util.ArrayList; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 import java.util.List; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 import javax.servlet.ServletContext; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 import javax.servlet.ServletRequest; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 import javax.servlet.ServletResponse; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 import javax.servlet.http.HttpServletRequest; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 import org.eclipse.jetty.continuation.Continuation; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 import org.eclipse.jetty.continuation.ContinuationThrowable; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 import org.eclipse.jetty.continuation.ContinuationListener; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 import org.eclipse.jetty.io.AsyncEndPoint; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
39 import org.eclipse.jetty.io.EndPoint; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 import org.eclipse.jetty.server.handler.ContextHandler; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 import org.eclipse.jetty.server.handler.ContextHandler.Context; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
42 import org.eclipse.jetty.util.URIUtil; |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
43 import org.slf4j.Logger; |
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
44 import org.slf4j.LoggerFactory; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 import org.eclipse.jetty.util.thread.Timeout; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
46 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
47 /* ------------------------------------------------------------ */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
48 /** Implementation of Continuation and AsyncContext interfaces |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
49 * |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
50 */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
51 public class AsyncContinuation implements AsyncContext, Continuation |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
52 { |
865 | 53 private static final Logger LOG = LoggerFactory.getLogger(AsyncContinuation.class); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
54 |
865 | 55 private final static long DEFAULT_TIMEOUT=30000L; |
56 | |
57 private final static ContinuationThrowable __exception = new ContinuationThrowable(); | |
58 | |
59 // STATES: | |
60 // handling() suspend() unhandle() resume() complete() doComplete() | |
61 // startAsync() dispatch() | |
62 // IDLE DISPATCHED | |
63 // DISPATCHED ASYNCSTARTED UNCOMPLETED | |
64 // ASYNCSTARTED ASYNCWAIT REDISPATCHING COMPLETING | |
65 // REDISPATCHING REDISPATCHED | |
66 // ASYNCWAIT REDISPATCH COMPLETING | |
67 // REDISPATCH REDISPATCHED | |
68 // REDISPATCHED ASYNCSTARTED UNCOMPLETED | |
69 // COMPLETING UNCOMPLETED UNCOMPLETED | |
70 // UNCOMPLETED COMPLETED | |
71 // COMPLETED | |
72 private static final int __IDLE=0; // Idle request | |
73 private static final int __DISPATCHED=1; // Request dispatched to filter/servlet | |
74 private static final int __UNCOMPLETED=8; // Request is completable | |
75 private static final int __COMPLETED=9; // Request is complete | |
76 | |
77 /* ------------------------------------------------------------ */ | |
78 protected AbstractHttpConnection _connection; | |
79 private List<AsyncListener> _lastAsyncListeners; | |
80 private List<AsyncListener> _asyncListeners; | |
81 private List<ContinuationListener> _continuationListeners; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
82 |
865 | 83 /* ------------------------------------------------------------ */ |
84 private int _state; | |
85 private boolean _initial; | |
86 private boolean _resumed; | |
87 private boolean _expired; | |
88 private volatile boolean _responseWrapped; | |
89 private long _timeoutMs=DEFAULT_TIMEOUT; | |
90 private AsyncEventState _event; | |
91 private volatile long _expireAt; | |
92 private volatile boolean _continuation; | |
93 | |
94 /* ------------------------------------------------------------ */ | |
95 protected AsyncContinuation() | |
96 { | |
97 _state=__IDLE; | |
98 _initial=true; | |
99 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
100 |
865 | 101 /* ------------------------------------------------------------ */ |
102 protected void setConnection(final AbstractHttpConnection connection) | |
103 { | |
104 synchronized(this) | |
105 { | |
106 _connection=connection; | |
107 } | |
108 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
109 |
865 | 110 /* ------------------------------------------------------------ */ |
111 public void addListener(AsyncListener listener) | |
112 { | |
113 synchronized(this) | |
114 { | |
115 if (_asyncListeners==null) | |
116 _asyncListeners=new ArrayList<AsyncListener>(); | |
117 _asyncListeners.add(listener); | |
118 } | |
119 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
120 |
865 | 121 /* ------------------------------------------------------------ */ |
122 public void addListener(AsyncListener listener,ServletRequest request, ServletResponse response) | |
123 { | |
124 synchronized(this) | |
125 { | |
126 // TODO handle the request/response ??? | |
127 if (_asyncListeners==null) | |
128 _asyncListeners=new ArrayList<AsyncListener>(); | |
129 _asyncListeners.add(listener); | |
130 } | |
131 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
132 |
865 | 133 /* ------------------------------------------------------------ */ |
134 public void addContinuationListener(ContinuationListener listener) | |
135 { | |
136 synchronized(this) | |
137 { | |
138 if (_continuationListeners==null) | |
139 _continuationListeners=new ArrayList<ContinuationListener>(); | |
140 _continuationListeners.add(listener); | |
141 } | |
142 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
143 |
865 | 144 /* ------------------------------------------------------------ */ |
145 public void setTimeout(long ms) | |
146 { | |
147 synchronized(this) | |
148 { | |
149 _timeoutMs=ms; | |
150 } | |
151 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
152 |
865 | 153 /* ------------------------------------------------------------ */ |
154 public long getTimeout() | |
155 { | |
156 synchronized(this) | |
157 { | |
158 return _timeoutMs; | |
159 } | |
160 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
161 |
865 | 162 /* ------------------------------------------------------------ */ |
163 public AsyncEventState getAsyncEventState() | |
164 { | |
165 synchronized(this) | |
166 { | |
167 return _event; | |
168 } | |
169 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
170 |
865 | 171 /* ------------------------------------------------------------ */ |
172 /** | |
173 * @see org.eclipse.jetty.continuation.Continuation#keepWrappers() | |
174 */ | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
175 |
865 | 176 /* ------------------------------------------------------------ */ |
177 /** | |
178 * @see org.eclipse.jetty.continuation.Continuation#isResponseWrapped() | |
179 */ | |
180 public boolean isResponseWrapped() | |
181 { | |
182 return _responseWrapped; | |
183 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
184 |
865 | 185 /* ------------------------------------------------------------ */ |
186 /* (non-Javadoc) | |
187 * @see javax.servlet.ServletRequest#isInitial() | |
188 */ | |
189 public boolean isInitial() | |
190 { | |
191 synchronized(this) | |
192 { | |
193 return _initial; | |
194 } | |
195 } | |
196 | |
197 public boolean isContinuation() | |
198 { | |
199 return _continuation; | |
200 } | |
201 | |
202 /* ------------------------------------------------------------ */ | |
203 /* (non-Javadoc) | |
204 * @see javax.servlet.ServletRequest#isSuspended() | |
205 */ | |
206 public boolean isSuspended() | |
207 { | |
208 synchronized(this) | |
209 { | |
210 switch(_state) | |
211 { | |
212 default: | |
213 return false; | |
214 } | |
215 } | |
216 } | |
217 | |
218 /* ------------------------------------------------------------ */ | |
219 public boolean isSuspending() | |
220 { | |
221 synchronized(this) | |
222 { | |
223 switch(_state) | |
224 { | |
225 default: | |
226 return false; | |
227 } | |
228 } | |
229 } | |
230 | |
231 /* ------------------------------------------------------------ */ | |
232 public boolean isDispatchable() | |
233 { | |
234 synchronized(this) | |
235 { | |
236 switch(_state) | |
237 { | |
238 default: | |
239 return false; | |
240 } | |
241 } | |
242 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
243 |
865 | 244 /* ------------------------------------------------------------ */ |
245 @Override | |
246 public String toString() | |
247 { | |
248 synchronized (this) | |
249 { | |
250 return super.toString()+"@"+getStatusString(); | |
251 } | |
252 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
253 |
865 | 254 /* ------------------------------------------------------------ */ |
255 public String getStatusString() | |
256 { | |
257 synchronized (this) | |
258 { | |
259 return | |
260 ((_state==__IDLE)?"IDLE": | |
261 (_state==__DISPATCHED)?"DISPATCHED": | |
262 (_state==__UNCOMPLETED)?"UNCOMPLETED": | |
263 (_state==__COMPLETED)?"COMPLETE": | |
264 ("UNKNOWN?"+_state))+ | |
265 (_initial?",initial":"")+ | |
266 (_resumed?",resumed":"")+ | |
267 (_expired?",expired":""); | |
268 } | |
269 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
270 |
865 | 271 /* ------------------------------------------------------------ */ |
272 /** | |
273 * @return false if the handling of the request should not proceed | |
274 */ | |
275 protected boolean handling() | |
276 { | |
277 synchronized (this) | |
278 { | |
279 _continuation=false; | |
280 | |
281 switch(_state) | |
282 { | |
283 case __IDLE: | |
284 _initial=true; | |
285 _state=__DISPATCHED; | |
286 if (_lastAsyncListeners!=null) | |
287 _lastAsyncListeners.clear(); | |
288 if (_asyncListeners!=null) | |
289 _asyncListeners.clear(); | |
290 else | |
291 { | |
292 _asyncListeners=_lastAsyncListeners; | |
293 _lastAsyncListeners=null; | |
294 } | |
295 return true; | |
296 | |
297 default: | |
298 throw new IllegalStateException(this.getStatusString()); | |
299 } | |
300 } | |
301 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
302 |
865 | 303 /* ------------------------------------------------------------ */ |
304 /** | |
305 * Signal that the HttpConnection has finished handling the request. | |
306 * For blocking connectors, this call may block if the request has | |
307 * been suspended (startAsync called). | |
308 * @return true if handling is complete, false if the request should | |
309 * be handled again (eg because of a resume that happened before unhandle was called) | |
310 */ | |
311 protected boolean unhandle() | |
312 { | |
313 synchronized (this) | |
314 { | |
315 switch(_state) | |
316 { | |
317 case __DISPATCHED: | |
918
7b62446899c6
remove unused states in AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
917
diff
changeset
|
318 _state = __UNCOMPLETED; |
865 | 319 return true; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
320 |
865 | 321 case __IDLE: |
322 throw new IllegalStateException(this.getStatusString()); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
323 |
865 | 324 default: |
325 throw new IllegalStateException(this.getStatusString()); | |
326 } | |
327 } | |
328 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
329 |
865 | 330 /* ------------------------------------------------------------ */ |
331 public void dispatch() | |
332 { | |
333 boolean dispatch=false; | |
334 synchronized (this) | |
335 { | |
336 switch(_state) | |
337 { | |
338 default: | |
339 throw new IllegalStateException(this.getStatusString()); | |
340 } | |
341 } | |
342 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
343 |
865 | 344 /* ------------------------------------------------------------ */ |
345 protected void expired() | |
346 { | |
347 } | |
348 | |
349 /* ------------------------------------------------------------ */ | |
350 /* (non-Javadoc) | |
351 * @see javax.servlet.ServletRequest#complete() | |
352 */ | |
353 public void complete() | |
354 { | |
355 // just like resume, except don't set _resumed=true; | |
356 boolean dispatch=false; | |
357 synchronized (this) | |
358 { | |
359 switch(_state) | |
360 { | |
361 case __DISPATCHED: | |
362 throw new IllegalStateException(this.getStatusString()); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
363 |
865 | 364 default: |
365 throw new IllegalStateException(this.getStatusString()); | |
366 } | |
367 } | |
368 } | |
369 | |
370 /* ------------------------------------------------------------ */ | |
371 /* (non-Javadoc) | |
372 * @see javax.servlet.ServletRequest#complete() | |
373 */ | |
374 public void errorComplete() | |
375 { | |
376 // just like complete except can overrule a prior dispatch call; | |
377 synchronized (this) | |
378 { | |
379 switch(_state) | |
380 { | |
381 default: | |
382 throw new IllegalStateException(this.getStatusString()); | |
383 } | |
384 } | |
385 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
386 |
865 | 387 /* ------------------------------------------------------------ */ |
388 @Override | |
389 public <T extends AsyncListener> T createListener(Class<T> clazz) throws ServletException | |
390 { | |
391 try | |
392 { | |
393 // TODO inject | |
394 return clazz.newInstance(); | |
395 } | |
396 catch(Exception e) | |
397 { | |
398 throw new ServletException(e); | |
399 } | |
400 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
401 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
402 |
865 | 403 /* ------------------------------------------------------------ */ |
404 /* (non-Javadoc) | |
405 * @see javax.servlet.ServletRequest#complete() | |
406 */ | |
407 protected void doComplete(Throwable ex) | |
408 { | |
409 final List<ContinuationListener> cListeners; | |
410 final List<AsyncListener> aListeners; | |
411 synchronized (this) | |
412 { | |
413 switch(_state) | |
414 { | |
415 case __UNCOMPLETED: | |
918
7b62446899c6
remove unused states in AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
917
diff
changeset
|
416 _state = __COMPLETED; |
865 | 417 cListeners=_continuationListeners; |
418 aListeners=_asyncListeners; | |
419 break; | |
420 | |
421 default: | |
422 cListeners=null; | |
423 aListeners=null; | |
424 throw new IllegalStateException(this.getStatusString()); | |
425 } | |
426 } | |
427 | |
428 if (aListeners!=null) | |
429 { | |
430 for (AsyncListener listener : aListeners) | |
431 { | |
432 try | |
433 { | |
434 if (ex!=null) | |
435 { | |
436 _event.getSuppliedRequest().setAttribute(RequestDispatcher.ERROR_EXCEPTION,ex); | |
437 _event.getSuppliedRequest().setAttribute(RequestDispatcher.ERROR_MESSAGE,ex.getMessage()); | |
438 listener.onError(_event); | |
439 } | |
440 else | |
441 listener.onComplete(_event); | |
442 } | |
443 catch(Exception e) | |
444 { | |
445 LOG.warn("",e); | |
446 } | |
447 } | |
448 } | |
449 if (cListeners!=null) | |
450 { | |
451 for (ContinuationListener listener : cListeners) | |
452 { | |
453 try | |
454 { | |
455 listener.onComplete(this); | |
456 } | |
457 catch(Exception e) | |
458 { | |
459 LOG.warn("",e); | |
460 } | |
461 } | |
462 } | |
463 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
464 |
865 | 465 /* ------------------------------------------------------------ */ |
466 protected void recycle() | |
467 { | |
468 synchronized (this) | |
469 { | |
470 switch(_state) | |
471 { | |
472 case __DISPATCHED: | |
473 throw new IllegalStateException(getStatusString()); | |
474 default: | |
475 _state=__IDLE; | |
476 } | |
477 _initial = true; | |
478 _resumed=false; | |
479 _expired=false; | |
480 _responseWrapped=false; | |
481 cancelTimeout(); | |
482 _timeoutMs=DEFAULT_TIMEOUT; | |
483 _continuationListeners=null; | |
484 } | |
485 } | |
486 | |
487 /* ------------------------------------------------------------ */ | |
488 public void cancel() | |
489 { | |
490 synchronized (this) | |
491 { | |
492 cancelTimeout(); | |
493 _continuationListeners=null; | |
494 } | |
495 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
496 |
865 | 497 /* ------------------------------------------------------------ */ |
498 protected void scheduleDispatch() | |
499 { | |
500 EndPoint endp=_connection.getEndPoint(); | |
501 if (!endp.isBlocking()) | |
502 { | |
503 ((AsyncEndPoint)endp).asyncDispatch(); | |
504 } | |
505 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
506 |
865 | 507 /* ------------------------------------------------------------ */ |
508 protected void scheduleTimeout() | |
509 { | |
510 EndPoint endp=_connection.getEndPoint(); | |
511 if (_timeoutMs>0) | |
512 { | |
513 if (endp.isBlocking()) | |
514 { | |
515 synchronized(this) | |
516 { | |
517 _expireAt = System.currentTimeMillis()+_timeoutMs; | |
518 long wait=_timeoutMs; | |
519 while (_expireAt>0 && wait>0 && _connection.getServer().isRunning()) | |
520 { | |
521 try | |
522 { | |
523 this.wait(wait); | |
524 } | |
525 catch (InterruptedException e) | |
526 { | |
527 LOG.trace("",e); | |
528 } | |
529 wait=_expireAt-System.currentTimeMillis(); | |
530 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
531 |
865 | 532 if (_expireAt>0 && wait<=0 && _connection.getServer().isRunning()) |
533 { | |
534 expired(); | |
535 } | |
536 } | |
537 } | |
538 else | |
539 { | |
540 ((AsyncEndPoint)endp).scheduleTimeout(_event._timeout,_timeoutMs); | |
541 } | |
542 } | |
543 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
544 |
865 | 545 /* ------------------------------------------------------------ */ |
546 protected void cancelTimeout() | |
547 { | |
548 EndPoint endp=_connection.getEndPoint(); | |
549 if (endp.isBlocking()) | |
550 { | |
551 synchronized(this) | |
552 { | |
553 _expireAt=0; | |
554 this.notifyAll(); | |
555 } | |
556 } | |
557 else | |
558 { | |
559 final AsyncEventState event=_event; | |
560 if (event!=null) | |
561 { | |
562 ((AsyncEndPoint)endp).cancelTimeout(event._timeout); | |
563 } | |
564 } | |
565 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
566 |
865 | 567 /* ------------------------------------------------------------ */ |
568 public boolean isCompleting() | |
569 { | |
570 synchronized (this) | |
571 { | |
918
7b62446899c6
remove unused states in AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
917
diff
changeset
|
572 return false; |
865 | 573 } |
574 } | |
575 | |
576 /* ------------------------------------------------------------ */ | |
577 boolean isUncompleted() | |
578 { | |
579 synchronized (this) | |
580 { | |
581 return _state==__UNCOMPLETED; | |
582 } | |
583 } | |
584 | |
585 /* ------------------------------------------------------------ */ | |
586 public boolean isComplete() | |
587 { | |
588 synchronized (this) | |
589 { | |
590 return _state==__COMPLETED; | |
591 } | |
592 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
593 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
594 |
865 | 595 /* ------------------------------------------------------------ */ |
596 public boolean isAsyncStarted() | |
597 { | |
598 synchronized (this) | |
599 { | |
600 switch(_state) | |
601 { | |
602 default: | |
603 return false; | |
604 } | |
605 } | |
606 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
607 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
608 |
865 | 609 /* ------------------------------------------------------------ */ |
610 public boolean isAsync() | |
611 { | |
612 synchronized (this) | |
613 { | |
614 switch(_state) | |
615 { | |
616 case __IDLE: | |
617 case __DISPATCHED: | |
618 case __UNCOMPLETED: | |
619 case __COMPLETED: | |
620 return false; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
621 |
865 | 622 default: |
623 return true; | |
624 } | |
625 } | |
626 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
627 |
865 | 628 /* ------------------------------------------------------------ */ |
629 public void dispatch(ServletContext context, String path) | |
630 { | |
631 _event._dispatchContext=context; | |
632 _event.setPath(path); | |
633 dispatch(); | |
634 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
635 |
865 | 636 /* ------------------------------------------------------------ */ |
637 public void dispatch(String path) | |
638 { | |
639 _event.setPath(path); | |
640 dispatch(); | |
641 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
642 |
865 | 643 /* ------------------------------------------------------------ */ |
644 public Request getBaseRequest() | |
645 { | |
646 return _connection.getRequest(); | |
647 } | |
648 | |
649 /* ------------------------------------------------------------ */ | |
650 public ServletRequest getRequest() | |
651 { | |
652 if (_event!=null) | |
653 return _event.getSuppliedRequest(); | |
654 return _connection.getRequest(); | |
655 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
656 |
865 | 657 /* ------------------------------------------------------------ */ |
658 public ServletResponse getResponse() | |
659 { | |
660 if (_responseWrapped && _event!=null && _event.getSuppliedResponse()!=null) | |
661 return _event.getSuppliedResponse(); | |
662 return _connection.getResponse(); | |
663 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
664 |
865 | 665 /* ------------------------------------------------------------ */ |
666 public void start(final Runnable run) | |
667 { | |
668 final AsyncEventState event=_event; | |
669 if (event!=null) | |
670 { | |
875
b9aa175d9a29
remove Server.getThreadPool()
Franklin Schmidt <fschmidt@gmail.com>
parents:
865
diff
changeset
|
671 _connection.getServer().threadPool.execute(new Runnable() |
865 | 672 { |
673 public void run() | |
674 { | |
675 ((Context)event.getServletContext()).getContextHandler().handle(run); | |
676 } | |
677 }); | |
678 } | |
679 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
680 |
865 | 681 /* ------------------------------------------------------------ */ |
682 public boolean hasOriginalRequestAndResponse() | |
683 { | |
684 synchronized (this) | |
685 { | |
686 return (_event!=null && _event.getSuppliedRequest()==_connection._request && _event.getSuppliedResponse()==_connection._response); | |
687 } | |
688 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
689 |
865 | 690 /* ------------------------------------------------------------ */ |
691 public ContextHandler getContextHandler() | |
692 { | |
693 final AsyncEventState event=_event; | |
694 if (event!=null) | |
695 return ((Context)event.getServletContext()).getContextHandler(); | |
696 return null; | |
697 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
698 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
699 |
865 | 700 /* ------------------------------------------------------------ */ |
701 /** | |
702 * @see Continuation#isResumed() | |
703 */ | |
704 public boolean isResumed() | |
705 { | |
706 synchronized (this) | |
707 { | |
708 return _resumed; | |
709 } | |
710 } | |
711 /* ------------------------------------------------------------ */ | |
712 /** | |
713 * @see Continuation#isExpired() | |
714 */ | |
715 public boolean isExpired() | |
716 { | |
717 synchronized (this) | |
718 { | |
719 return _expired; | |
720 } | |
721 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
722 |
865 | 723 /* ------------------------------------------------------------ */ |
724 /** | |
725 * @see Continuation#resume() | |
726 */ | |
727 public void resume() | |
728 { | |
729 dispatch(); | |
730 } | |
731 | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
732 |
865 | 733 /* ------------------------------------------------------------ */ |
734 /** | |
735 * @see org.eclipse.jetty.continuation.Continuation#getServletResponse() | |
736 */ | |
737 public ServletResponse getServletResponse() | |
738 { | |
739 if (_responseWrapped && _event!=null && _event.getSuppliedResponse()!=null) | |
740 return _event.getSuppliedResponse(); | |
741 return _connection.getResponse(); | |
742 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
743 |
865 | 744 /* ------------------------------------------------------------ */ |
745 /** | |
746 * @see org.eclipse.jetty.continuation.Continuation#getAttribute(java.lang.String) | |
747 */ | |
748 public Object getAttribute(String name) | |
749 { | |
750 return _connection.getRequest().getAttribute(name); | |
751 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
752 |
865 | 753 /* ------------------------------------------------------------ */ |
754 /** | |
755 * @see org.eclipse.jetty.continuation.Continuation#removeAttribute(java.lang.String) | |
756 */ | |
757 public void removeAttribute(String name) | |
758 { | |
759 _connection.getRequest().removeAttribute(name); | |
760 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
761 |
865 | 762 /* ------------------------------------------------------------ */ |
763 /** | |
764 * @see org.eclipse.jetty.continuation.Continuation#setAttribute(java.lang.String, java.lang.Object) | |
765 */ | |
766 public void setAttribute(String name, Object attribute) | |
767 { | |
768 _connection.getRequest().setAttribute(name,attribute); | |
769 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
770 |
865 | 771 /* ------------------------------------------------------------ */ |
772 /** | |
773 * @see org.eclipse.jetty.continuation.Continuation#undispatch() | |
774 */ | |
775 public void undispatch() | |
776 { | |
777 if (isSuspended()) | |
778 { | |
779 if (LOG.isDebugEnabled()) | |
780 throw new ContinuationThrowable(); | |
781 else | |
782 throw __exception; | |
783 } | |
784 throw new IllegalStateException("!suspended"); | |
785 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
786 |
865 | 787 /* ------------------------------------------------------------ */ |
788 /* ------------------------------------------------------------ */ | |
789 public class AsyncTimeout extends Timeout.Task implements Runnable | |
790 { | |
791 @Override | |
792 public void expired() | |
793 { | |
794 AsyncContinuation.this.expired(); | |
795 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
796 |
865 | 797 @Override |
798 public void run() | |
799 { | |
800 AsyncContinuation.this.expired(); | |
801 } | |
802 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
803 |
865 | 804 /* ------------------------------------------------------------ */ |
805 /* ------------------------------------------------------------ */ | |
806 public class AsyncEventState extends AsyncEvent | |
807 { | |
808 private final ServletContext _suspendedContext; | |
809 private ServletContext _dispatchContext; | |
810 private String _pathInContext; | |
811 private Timeout.Task _timeout= new AsyncTimeout(); | |
812 | |
813 public AsyncEventState(ServletContext context, ServletRequest request, ServletResponse response) | |
814 { | |
815 super(AsyncContinuation.this, request,response); | |
816 _suspendedContext=context; | |
817 // Get the base request So we can remember the initial paths | |
818 Request r=_connection.getRequest(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
819 |
865 | 820 // If we haven't been async dispatched before |
821 if (r.getAttribute(AsyncContext.ASYNC_REQUEST_URI)==null) | |
822 { | |
823 // We are setting these attributes during startAsync, when the spec implies that | |
824 // they are only available after a call to AsyncContext.dispatch(...); | |
825 | |
826 // have we been forwarded before? | |
827 String uri=(String)r.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI); | |
828 if (uri!=null) | |
829 { | |
830 r.setAttribute(AsyncContext.ASYNC_REQUEST_URI,uri); | |
831 r.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,r.getAttribute(RequestDispatcher.FORWARD_CONTEXT_PATH)); | |
832 r.setAttribute(AsyncContext.ASYNC_SERVLET_PATH,r.getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH)); | |
833 r.setAttribute(AsyncContext.ASYNC_PATH_INFO,r.getAttribute(RequestDispatcher.FORWARD_PATH_INFO)); | |
834 r.setAttribute(AsyncContext.ASYNC_QUERY_STRING,r.getAttribute(RequestDispatcher.FORWARD_QUERY_STRING)); | |
835 } | |
836 else | |
837 { | |
838 r.setAttribute(AsyncContext.ASYNC_REQUEST_URI,r.getRequestURI()); | |
839 r.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,r.getContextPath()); | |
840 r.setAttribute(AsyncContext.ASYNC_SERVLET_PATH,r.getServletPath()); | |
841 r.setAttribute(AsyncContext.ASYNC_PATH_INFO,r.getPathInfo()); | |
842 r.setAttribute(AsyncContext.ASYNC_QUERY_STRING,r.getQueryString()); | |
843 } | |
844 } | |
845 } | |
846 | |
847 public ServletContext getSuspendedContext() | |
848 { | |
849 return _suspendedContext; | |
850 } | |
851 | |
852 public ServletContext getDispatchContext() | |
853 { | |
854 return _dispatchContext; | |
855 } | |
856 | |
857 public ServletContext getServletContext() | |
858 { | |
859 return _dispatchContext==null?_suspendedContext:_dispatchContext; | |
860 } | |
861 | |
862 public void setPath(String path) | |
863 { | |
864 _pathInContext=path; | |
865 } | |
866 | |
867 /* ------------------------------------------------------------ */ | |
868 /** | |
869 * @return The path in the context | |
870 */ | |
871 public String getPath() | |
872 { | |
873 return _pathInContext; | |
874 } | |
875 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
876 } |