|
| 1 | +/* |
| 2 | + * Copyright 2002-2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.web.server.adapter; |
| 17 | + |
| 18 | +import javax.servlet.ServletContext; |
| 19 | +import javax.servlet.ServletException; |
| 20 | +import javax.servlet.ServletRegistration; |
| 21 | + |
| 22 | +import org.springframework.context.ApplicationContext; |
| 23 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 24 | +import org.springframework.http.server.reactive.HttpHandler; |
| 25 | +import org.springframework.http.server.reactive.ServletHttpHandlerAdapter; |
| 26 | +import org.springframework.util.Assert; |
| 27 | +import org.springframework.web.WebApplicationInitializer; |
| 28 | + |
| 29 | +/** |
| 30 | + * Base class for a {@link org.springframework.web.WebApplicationInitializer} |
| 31 | + * that installs a Spring Reactive Web Application on a Servlet container. |
| 32 | + * |
| 33 | + * <p>Spring configuration is loaded and given to |
| 34 | + * {@link WebHttpHandlerBuilder#applicationContext WebHttpHandlerBuilder} |
| 35 | + * which scans the context looking for specific beans and creates a reactive |
| 36 | + * {@link HttpHandler}. The resulting handler is installed as a Servlet through |
| 37 | + * the {@link ServletHttpHandlerAdapter}. |
| 38 | + * |
| 39 | + * @author Rossen Stoyanchev |
| 40 | + * @since 5.0.2 |
| 41 | + */ |
| 42 | +public abstract class AbstractReactiveWebInitializer implements WebApplicationInitializer { |
| 43 | + |
| 44 | + /** |
| 45 | + * The default servlet name to use. See {@link #getServletName}. |
| 46 | + */ |
| 47 | + public static final String DEFAULT_SERVLET_NAME = "http-handler-adapter"; |
| 48 | + |
| 49 | + |
| 50 | + @Override |
| 51 | + public void onStartup(ServletContext servletContext) throws ServletException { |
| 52 | + String servletName = getServletName(); |
| 53 | + Assert.hasLength(servletName, "getServletName() must not return empty or null"); |
| 54 | + |
| 55 | + ApplicationContext applicationContext = createApplicationContext(); |
| 56 | + Assert.notNull(applicationContext, "createApplicationContext() must not return null."); |
| 57 | + |
| 58 | + HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(applicationContext).build(); |
| 59 | + ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler); |
| 60 | + |
| 61 | + ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, servlet); |
| 62 | + Assert.notNull(registration, "Failed to register servlet '" + servletName + "'."); |
| 63 | + |
| 64 | + registration.setLoadOnStartup(1); |
| 65 | + registration.addMapping("/"); |
| 66 | + registration.setAsyncSupported(true); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Return the name to use to register the {@link ServletHttpHandlerAdapter}. |
| 71 | + * <p>By default this is {@link #DEFAULT_SERVLET_NAME}. |
| 72 | + */ |
| 73 | + protected String getServletName() { |
| 74 | + return DEFAULT_SERVLET_NAME; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Return the Spring configuration that contains application beans including |
| 79 | + * the ones detected by {@link WebHttpHandlerBuilder#applicationContext}. |
| 80 | + */ |
| 81 | + protected ApplicationContext createApplicationContext() { |
| 82 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
| 83 | + Class<?>[] configClasses = getConfigClasses(); |
| 84 | + Assert.notEmpty(configClasses, "No Spring configuration provided."); |
| 85 | + return context; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Specify {@link org.springframework.context.annotation.Configuration @Configuration} and/or |
| 90 | + * {@link org.springframework.stereotype.Component @Component} classes that |
| 91 | + * make up the application configuration. The config classes are given to |
| 92 | + * {@linkplain #createApplicationContext()}. |
| 93 | + */ |
| 94 | + protected abstract Class<?>[] getConfigClasses(); |
| 95 | + |
| 96 | +} |
0 commit comments