Mercurial Hosting > luan
annotate src/org/eclipse/jetty/server/AsyncContinuation.java @ 919:dd6b1f079634
remove AsyncContinuation._lastAsyncListeners
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 09 Oct 2016 03:45:31 -0600 |
parents | 7b62446899c6 |
children | 3268ddf919d4 |
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 public boolean isCompleting() | |
561 { | |
562 synchronized (this) | |
563 { | |
918
7b62446899c6
remove unused states in AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
917
diff
changeset
|
564 return false; |
865 | 565 } |
566 } | |
567 | |
568 /* ------------------------------------------------------------ */ | |
569 boolean isUncompleted() | |
570 { | |
571 synchronized (this) | |
572 { | |
573 return _state==__UNCOMPLETED; | |
574 } | |
575 } | |
576 | |
577 /* ------------------------------------------------------------ */ | |
578 public boolean isComplete() | |
579 { | |
580 synchronized (this) | |
581 { | |
582 return _state==__COMPLETED; | |
583 } | |
584 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
585 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
586 |
865 | 587 /* ------------------------------------------------------------ */ |
588 public boolean isAsyncStarted() | |
589 { | |
590 synchronized (this) | |
591 { | |
592 switch(_state) | |
593 { | |
594 default: | |
595 return false; | |
596 } | |
597 } | |
598 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
599 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
600 |
865 | 601 /* ------------------------------------------------------------ */ |
602 public boolean isAsync() | |
603 { | |
604 synchronized (this) | |
605 { | |
606 switch(_state) | |
607 { | |
608 case __IDLE: | |
609 case __DISPATCHED: | |
610 case __UNCOMPLETED: | |
611 case __COMPLETED: | |
612 return false; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
613 |
865 | 614 default: |
615 return true; | |
616 } | |
617 } | |
618 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
619 |
865 | 620 /* ------------------------------------------------------------ */ |
621 public void dispatch(ServletContext context, String path) | |
622 { | |
623 _event._dispatchContext=context; | |
624 _event.setPath(path); | |
625 dispatch(); | |
626 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
627 |
865 | 628 /* ------------------------------------------------------------ */ |
629 public void dispatch(String path) | |
630 { | |
631 _event.setPath(path); | |
632 dispatch(); | |
633 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
634 |
865 | 635 /* ------------------------------------------------------------ */ |
636 public Request getBaseRequest() | |
637 { | |
638 return _connection.getRequest(); | |
639 } | |
640 | |
641 /* ------------------------------------------------------------ */ | |
642 public ServletRequest getRequest() | |
643 { | |
644 if (_event!=null) | |
645 return _event.getSuppliedRequest(); | |
646 return _connection.getRequest(); | |
647 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
648 |
865 | 649 /* ------------------------------------------------------------ */ |
650 public ServletResponse getResponse() | |
651 { | |
652 if (_responseWrapped && _event!=null && _event.getSuppliedResponse()!=null) | |
653 return _event.getSuppliedResponse(); | |
654 return _connection.getResponse(); | |
655 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
656 |
865 | 657 /* ------------------------------------------------------------ */ |
658 public void start(final Runnable run) | |
659 { | |
660 final AsyncEventState event=_event; | |
661 if (event!=null) | |
662 { | |
875
b9aa175d9a29
remove Server.getThreadPool()
Franklin Schmidt <fschmidt@gmail.com>
parents:
865
diff
changeset
|
663 _connection.getServer().threadPool.execute(new Runnable() |
865 | 664 { |
665 public void run() | |
666 { | |
667 ((Context)event.getServletContext()).getContextHandler().handle(run); | |
668 } | |
669 }); | |
670 } | |
671 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
672 |
865 | 673 /* ------------------------------------------------------------ */ |
674 public boolean hasOriginalRequestAndResponse() | |
675 { | |
676 synchronized (this) | |
677 { | |
678 return (_event!=null && _event.getSuppliedRequest()==_connection._request && _event.getSuppliedResponse()==_connection._response); | |
679 } | |
680 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
681 |
865 | 682 /* ------------------------------------------------------------ */ |
683 public ContextHandler getContextHandler() | |
684 { | |
685 final AsyncEventState event=_event; | |
686 if (event!=null) | |
687 return ((Context)event.getServletContext()).getContextHandler(); | |
688 return null; | |
689 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
690 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
691 |
865 | 692 /* ------------------------------------------------------------ */ |
693 /** | |
694 * @see Continuation#isResumed() | |
695 */ | |
696 public boolean isResumed() | |
697 { | |
698 synchronized (this) | |
699 { | |
700 return _resumed; | |
701 } | |
702 } | |
703 /* ------------------------------------------------------------ */ | |
704 /** | |
705 * @see Continuation#isExpired() | |
706 */ | |
707 public boolean isExpired() | |
708 { | |
709 synchronized (this) | |
710 { | |
711 return _expired; | |
712 } | |
713 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
714 |
865 | 715 /* ------------------------------------------------------------ */ |
716 /** | |
717 * @see Continuation#resume() | |
718 */ | |
719 public void resume() | |
720 { | |
721 dispatch(); | |
722 } | |
723 | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
724 |
865 | 725 /* ------------------------------------------------------------ */ |
726 /** | |
727 * @see org.eclipse.jetty.continuation.Continuation#getServletResponse() | |
728 */ | |
729 public ServletResponse getServletResponse() | |
730 { | |
731 if (_responseWrapped && _event!=null && _event.getSuppliedResponse()!=null) | |
732 return _event.getSuppliedResponse(); | |
733 return _connection.getResponse(); | |
734 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
735 |
865 | 736 /* ------------------------------------------------------------ */ |
737 /** | |
738 * @see org.eclipse.jetty.continuation.Continuation#getAttribute(java.lang.String) | |
739 */ | |
740 public Object getAttribute(String name) | |
741 { | |
742 return _connection.getRequest().getAttribute(name); | |
743 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
744 |
865 | 745 /* ------------------------------------------------------------ */ |
746 /** | |
747 * @see org.eclipse.jetty.continuation.Continuation#removeAttribute(java.lang.String) | |
748 */ | |
749 public void removeAttribute(String name) | |
750 { | |
751 _connection.getRequest().removeAttribute(name); | |
752 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
753 |
865 | 754 /* ------------------------------------------------------------ */ |
755 /** | |
756 * @see org.eclipse.jetty.continuation.Continuation#setAttribute(java.lang.String, java.lang.Object) | |
757 */ | |
758 public void setAttribute(String name, Object attribute) | |
759 { | |
760 _connection.getRequest().setAttribute(name,attribute); | |
761 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
762 |
865 | 763 /* ------------------------------------------------------------ */ |
764 /** | |
765 * @see org.eclipse.jetty.continuation.Continuation#undispatch() | |
766 */ | |
767 public void undispatch() | |
768 { | |
769 if (isSuspended()) | |
770 { | |
771 if (LOG.isDebugEnabled()) | |
772 throw new ContinuationThrowable(); | |
773 else | |
774 throw __exception; | |
775 } | |
776 throw new IllegalStateException("!suspended"); | |
777 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
778 |
865 | 779 /* ------------------------------------------------------------ */ |
780 /* ------------------------------------------------------------ */ | |
781 public class AsyncTimeout extends Timeout.Task implements Runnable | |
782 { | |
783 @Override | |
784 public void expired() | |
785 { | |
786 AsyncContinuation.this.expired(); | |
787 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
788 |
865 | 789 @Override |
790 public void run() | |
791 { | |
792 AsyncContinuation.this.expired(); | |
793 } | |
794 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
795 |
865 | 796 /* ------------------------------------------------------------ */ |
797 /* ------------------------------------------------------------ */ | |
798 public class AsyncEventState extends AsyncEvent | |
799 { | |
800 private final ServletContext _suspendedContext; | |
801 private ServletContext _dispatchContext; | |
802 private String _pathInContext; | |
803 private Timeout.Task _timeout= new AsyncTimeout(); | |
804 | |
805 public AsyncEventState(ServletContext context, ServletRequest request, ServletResponse response) | |
806 { | |
807 super(AsyncContinuation.this, request,response); | |
808 _suspendedContext=context; | |
809 // Get the base request So we can remember the initial paths | |
810 Request r=_connection.getRequest(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
811 |
865 | 812 // If we haven't been async dispatched before |
813 if (r.getAttribute(AsyncContext.ASYNC_REQUEST_URI)==null) | |
814 { | |
815 // We are setting these attributes during startAsync, when the spec implies that | |
816 // they are only available after a call to AsyncContext.dispatch(...); | |
817 | |
818 // have we been forwarded before? | |
819 String uri=(String)r.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI); | |
820 if (uri!=null) | |
821 { | |
822 r.setAttribute(AsyncContext.ASYNC_REQUEST_URI,uri); | |
823 r.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,r.getAttribute(RequestDispatcher.FORWARD_CONTEXT_PATH)); | |
824 r.setAttribute(AsyncContext.ASYNC_SERVLET_PATH,r.getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH)); | |
825 r.setAttribute(AsyncContext.ASYNC_PATH_INFO,r.getAttribute(RequestDispatcher.FORWARD_PATH_INFO)); | |
826 r.setAttribute(AsyncContext.ASYNC_QUERY_STRING,r.getAttribute(RequestDispatcher.FORWARD_QUERY_STRING)); | |
827 } | |
828 else | |
829 { | |
830 r.setAttribute(AsyncContext.ASYNC_REQUEST_URI,r.getRequestURI()); | |
831 r.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,r.getContextPath()); | |
832 r.setAttribute(AsyncContext.ASYNC_SERVLET_PATH,r.getServletPath()); | |
833 r.setAttribute(AsyncContext.ASYNC_PATH_INFO,r.getPathInfo()); | |
834 r.setAttribute(AsyncContext.ASYNC_QUERY_STRING,r.getQueryString()); | |
835 } | |
836 } | |
837 } | |
838 | |
839 public ServletContext getSuspendedContext() | |
840 { | |
841 return _suspendedContext; | |
842 } | |
843 | |
844 public ServletContext getDispatchContext() | |
845 { | |
846 return _dispatchContext; | |
847 } | |
848 | |
849 public ServletContext getServletContext() | |
850 { | |
851 return _dispatchContext==null?_suspendedContext:_dispatchContext; | |
852 } | |
853 | |
854 public void setPath(String path) | |
855 { | |
856 _pathInContext=path; | |
857 } | |
858 | |
859 /* ------------------------------------------------------------ */ | |
860 /** | |
861 * @return The path in the context | |
862 */ | |
863 public String getPath() | |
864 { | |
865 return _pathInContext; | |
866 } | |
867 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
868 } |