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