|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2012 the original author or authors. |
| 2 | + * Copyright 2002-2015 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
13 | 13 | * See the License for the specific language governing permissions and
|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
| 16 | + |
16 | 17 | package org.springframework.web.context.request.async;
|
17 | 18 |
|
18 |
| -import java.lang.reflect.Constructor; |
19 | 19 | import javax.servlet.ServletRequest;
|
20 | 20 | import javax.servlet.http.HttpServletRequest;
|
21 | 21 | import javax.servlet.http.HttpServletResponse;
|
22 | 22 |
|
23 |
| -import org.springframework.beans.BeanUtils; |
24 | 23 | import org.springframework.util.ClassUtils;
|
25 | 24 | import org.springframework.web.context.request.RequestAttributes;
|
26 | 25 | import org.springframework.web.context.request.WebRequest;
|
|
29 | 28 | * Utility methods related to processing asynchronous web requests.
|
30 | 29 | *
|
31 | 30 | * @author Rossen Stoyanchev
|
| 31 | + * @author Juergen Hoeller |
32 | 32 | * @since 3.2
|
33 | 33 | */
|
34 | 34 | public abstract class WebAsyncUtils {
|
35 | 35 |
|
36 | 36 | public static final String WEB_ASYNC_MANAGER_ATTRIBUTE = WebAsyncManager.class.getName() + ".WEB_ASYNC_MANAGER";
|
37 | 37 |
|
38 |
| - private static Constructor<?> standardAsyncRequestConstructor; |
| 38 | + // Determine whether Servlet 3.0's ServletRequest.startAsync method is available |
| 39 | + private static final boolean startAsyncAvailable = ClassUtils.hasMethod(ServletRequest.class, "startAsync"); |
39 | 40 |
|
40 | 41 |
|
41 | 42 | /**
|
@@ -66,31 +67,27 @@ public static WebAsyncManager getAsyncManager(WebRequest webRequest) {
|
66 | 67 | }
|
67 | 68 |
|
68 | 69 | /**
|
69 |
| - * Create an AsyncWebRequest instance. By default an instance of |
70 |
| - * {@link StandardServletAsyncWebRequest} is created if running in Servlet |
71 |
| - * 3.0 (or higher) environment or as a fallback, an instance of |
72 |
| - * {@link NoSupportAsyncWebRequest} is returned. |
73 |
| - * |
| 70 | + * Create an AsyncWebRequest instance. By default, an instance of |
| 71 | + * {@link StandardServletAsyncWebRequest} gets created when running in |
| 72 | + * Servlet 3.0 (or higher) environment - as a fallback, an instance |
| 73 | + * of {@link NoSupportAsyncWebRequest} will be returned. |
74 | 74 | * @param request the current request
|
75 | 75 | * @param response the current response
|
76 |
| - * @return an AsyncWebRequest instance, never {@code null} |
| 76 | + * @return an AsyncWebRequest instance (never {@code null}) |
77 | 77 | */
|
78 | 78 | public static AsyncWebRequest createAsyncWebRequest(HttpServletRequest request, HttpServletResponse response) {
|
79 |
| - return ClassUtils.hasMethod(ServletRequest.class, "startAsync") ? |
80 |
| - createStandardServletAsyncWebRequest(request, response) : new NoSupportAsyncWebRequest(request, response); |
| 79 | + return (startAsyncAvailable ? AsyncWebRequestFactory.createStandardAsyncWebRequest(request, response) : |
| 80 | + new NoSupportAsyncWebRequest(request, response)); |
81 | 81 | }
|
82 | 82 |
|
83 |
| - private static AsyncWebRequest createStandardServletAsyncWebRequest(HttpServletRequest request, HttpServletResponse response) { |
84 |
| - try { |
85 |
| - if (standardAsyncRequestConstructor == null) { |
86 |
| - String className = "org.springframework.web.context.request.async.StandardServletAsyncWebRequest"; |
87 |
| - Class<?> clazz = ClassUtils.forName(className, WebAsyncUtils.class.getClassLoader()); |
88 |
| - standardAsyncRequestConstructor = clazz.getConstructor(HttpServletRequest.class, HttpServletResponse.class); |
89 |
| - } |
90 |
| - return (AsyncWebRequest) BeanUtils.instantiateClass(standardAsyncRequestConstructor, request, response); |
91 |
| - } |
92 |
| - catch (Throwable t) { |
93 |
| - throw new IllegalStateException("Failed to instantiate StandardServletAsyncWebRequest", t); |
| 83 | + |
| 84 | + /** |
| 85 | + * Inner class to avoid a hard dependency on the Servlet 3.0 API. |
| 86 | + */ |
| 87 | + private static class AsyncWebRequestFactory { |
| 88 | + |
| 89 | + public static AsyncWebRequest createStandardAsyncWebRequest(HttpServletRequest request, HttpServletResponse response) { |
| 90 | + return new StandardServletAsyncWebRequest(request, response); |
94 | 91 | }
|
95 | 92 | }
|
96 | 93 |
|
|
0 commit comments