1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.coyote;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.apache.tomcat.util.net.AbstractEndpoint.Handler.SocketState;
import org.apache.tomcat.util.res.StringManager;
import org.apache.tomcat.util.security.PrivilegedGetTccl;
import org.apache.tomcat.util.security.PrivilegedSetTccl;
/**
* Manages the state transitions for async requests.
*
* <pre>
* The internal states that are used are:
* DISPATCHED - Standard request. Not in Async mode.
* STARTING - ServletRequest.startAsync() has been called but the
* request in which that call was made has not finished
* processing.
* STARTED - ServletRequest.startAsync() has been called and the
* request in which that call was made has finished
* processing.
* MUST_COMPLETE - complete() has been called before the request in which
* ServletRequest.startAsync() has finished. As soon as that
* request finishes, the complete() will be processed.
* COMPLETING - The call to complete() was made once the request was in
* the STARTED state. May or may not be triggered by a
* container thread - depends if start(Runnable) was used
* TIMING_OUT - The async request has timed out and is waiting for a call
* to complete(). If that isn't made, the error state will
* entered.
* MUST_DISPATCH - dispatch() has been called before the request in which
* ServletRequest.startAsync() has finished. As soon as that
* request finishes, the dispatch() will be processed.
* DISPATCHING - The dispatch is being processed.
* ERROR - Something went wrong.
*
* |----------------->--------------|
* | \|/
* | |----------<---------------ERROR
* | | complete() /|\ | \
* | | | | \---------------|
* | | | | |dispatch()
* | | | |postProcess() \|/
* | | error()| | |
* | | | | |--|timeout() |
* | | postProcess() | \|/ | \|/ | auto
* | | |--------------->DISPATCHED<---------- | --------------COMPLETING<-----|
* | | | /|\ | | | /|\ |
* | | | |--->-------| | | |--| |
* | | ^ | |startAsync() | timeout() |
* | | | | | | |
* | \|/ | | complete() \|/ postProcess() | |
* | MUST_COMPLETE-<- | ----<------STARTING-->--------- | ------------| ^
* | /|\ | | | | complete() |
* | | | | | | /-----------|
* | | ^ |dispatch() | | /
* | | | | | | /
* | | | \|/ / \|/ /
* | | | MUST_DISPATCH / STARTED
* | | | | / /|
* | | | |postProcess() / / |
* ^ ^ | | / dispatch()/ |
* | | | | / / |
* | | | | |---------- / -----------/ |auto
* | | | | | / |
* | | | | | |-----/ |
* | | | auto \|/ \|/ \|/ \|/
* | | |---<------DISPATCHING<-----------------TIMING_OUT
* | | dispatch() | |
* | | | |
* | |-------<----------------------------------<------| |
* | complete() |
* | |
* |<--------<-------------------<-------------------------------<--|
* error()
* </pre>
*/
public class AsyncStateMachine<S> {
/**
* The string manager for this package.
*/
private static final StringManager sm =
StringManager.getManager(Constants.Package);
private static enum AsyncState {
DISPATCHED(false, false, false),
STARTING(true, true, false),
STARTED(true, true, false),
MUST_COMPLETE(true, false, false),
COMPLETING(true, false, false),
TIMING_OUT(true, false, false),
MUST_DISPATCH(true, true, true),
DISPATCHING(true, false, true),
ERROR(true,false,false);
private boolean isAsync;
private boolean isStarted;
private boolean isDispatching;
private AsyncState(boolean isAsync, boolean isStarted,
boolean isDispatching) {
this.isAsync = isAsync;
this.isStarted = isStarted;
this.isDispatching = isDispatching;
}
public boolean isAsync() {
return this.isAsync;
}
public boolean isStarted() {
return this.isStarted;
}
public boolean isDispatching() {
return this.isDispatching;
}
}
private volatile AsyncState state = AsyncState.DISPATCHED;
// Need this to fire listener on complete
private AsyncContextCallback asyncCtxt = null;
private Processor<S> processor;
public AsyncStateMachine(Processor<S> processor) {
this.processor = processor;
}
public boolean isAsync() {
return state.isAsync();
}
public boolean isAsyncDispatching() {
return state.isDispatching();
}
public boolean isAsyncStarted() {
return state.isStarted();
}
public boolean isAsyncTimingOut() {
return state == AsyncState.TIMING_OUT;
}
public boolean isAsyncError() {
return state == AsyncState.ERROR;
}
public synchronized void asyncStart(AsyncContextCallback asyncCtxt) {
if (state == AsyncState.DISPATCHED) {
state = AsyncState.STARTING;
this.asyncCtxt = asyncCtxt;
} else {
throw new IllegalStateException(
sm.getString("asyncStateMachine.invalidAsyncState",
"asyncStart()", state));
}
}
/*
* Async has been processed. Whether or not to enter a long poll depends on
* current state. For example, as per SRV.2.3.3.3 can now process calls to
* complete() or dispatch().
*/
public synchronized SocketState asyncPostProcess() {
if (state == AsyncState.STARTING) {
state = AsyncState.STARTED;
return SocketState.LONG;
} else if (state == AsyncState.MUST_COMPLETE) {
asyncCtxt.fireOnComplete();
state = AsyncState.DISPATCHED;
return SocketState.ASYNC_END;
} else if (state == AsyncState.COMPLETING) {
asyncCtxt.fireOnComplete();
state = AsyncState.DISPATCHED;
return SocketState.ASYNC_END;
} else if (state == AsyncState.MUST_DISPATCH) {
state = AsyncState.DISPATCHING;
return SocketState.ASYNC_END;
} else if (state == AsyncState.DISPATCHING) {
state = AsyncState.DISPATCHED;
return SocketState.ASYNC_END;
} else if (state == AsyncState.STARTED) {
// This can occur if an async listener does a dispatch to an async
// servlet during onTimeout
return SocketState.LONG;
} else {
throw new IllegalStateException(
sm.getString("asyncStateMachine.invalidAsyncState",
"asyncPostProcess()", state));
}
}
public synchronized boolean asyncComplete() {
boolean doComplete = false;
if (state == AsyncState.STARTING) {
state = AsyncState.MUST_COMPLETE;
} else if (state == AsyncState.STARTED) {
state = AsyncState.COMPLETING;
doComplete = true;
} else if (state == AsyncState.TIMING_OUT ||
state == AsyncState.ERROR) {
state = AsyncState.MUST_COMPLETE;
} else {
throw new IllegalStateException(
sm.getString("asyncStateMachine.invalidAsyncState",
"asyncComplete()", state));
}
return doComplete;
}
public synchronized boolean asyncTimeout() {
if (state == AsyncState.STARTED) {
state = AsyncState.TIMING_OUT;
return true;
} else if (state == AsyncState.COMPLETING ||
state == AsyncState.DISPATCHING ||
state == AsyncState.DISPATCHED) {
// NOOP - App called complete() or dispatch() between the the
// timeout firing and execution reaching this point
return false;
} else {
throw new IllegalStateException(
sm.getString("asyncStateMachine.invalidAsyncState",
"asyncTimeout()", state));
}
}
public synchronized boolean asyncDispatch() {
boolean doDispatch = false;
if (state == AsyncState.STARTING) {
state = AsyncState.MUST_DISPATCH;
} else if (state == AsyncState.STARTED ||
state == AsyncState.TIMING_OUT ||
state == AsyncState.ERROR) {
state = AsyncState.DISPATCHING;
doDispatch = true;
} else {
throw new IllegalStateException(
sm.getString("asyncStateMachine.invalidAsyncState",
"asyncDispatch()", state));
}
return doDispatch;
}
public synchronized void asyncDispatched() {
if (state == AsyncState.DISPATCHING) {
state = AsyncState.DISPATCHED;
} else {
throw new IllegalStateException(
sm.getString("asyncStateMachine.invalidAsyncState",
"asyncDispatched()", state));
}
}
public synchronized boolean asyncError() {
boolean doDispatch = false;
if (state == AsyncState.DISPATCHED ||
state == AsyncState.TIMING_OUT) {
state = AsyncState.ERROR;
} else {
throw new IllegalStateException(
sm.getString("asyncStateMachine.invalidAsyncState",
"asyncError()", state));
}
return doDispatch;
}
public synchronized void asyncRun(Runnable runnable) {
if (state == AsyncState.STARTING || state == AsyncState.STARTED) {
// Execute the runnable using a container thread from the
// Connector's thread pool. Use a wrapper to prevent a memory leak
ClassLoader oldCL;
if (Constants.IS_SECURITY_ENABLED) {
PrivilegedAction<ClassLoader> pa = new PrivilegedGetTccl();
oldCL = AccessController.doPrivileged(pa);
} else {
oldCL = Thread.currentThread().getContextClassLoader();
}
try {
if (Constants.IS_SECURITY_ENABLED) {
PrivilegedAction<Void> pa = new PrivilegedSetTccl(
this.getClass().getClassLoader());
AccessController.doPrivileged(pa);
} else {
Thread.currentThread().setContextClassLoader(
this.getClass().getClassLoader());
}
processor.getExecutor().execute(runnable);
} finally {
if (Constants.IS_SECURITY_ENABLED) {
PrivilegedAction<Void> pa = new PrivilegedSetTccl(
oldCL);
AccessController.doPrivileged(pa);
} else {
Thread.currentThread().setContextClassLoader(oldCL);
}
}
} else {
throw new IllegalStateException(
sm.getString("asyncStateMachine.invalidAsyncState",
"asyncRun()", state));
}
}
public synchronized void recycle() {
asyncCtxt = null;
state = AsyncState.DISPATCHED;
}
}
|