Simplify ReactiveWebServerApplicationContext.ServerManager

Closes gh-21315
pull/21318/head
Andy Wilkinson 5 years ago
parent a688ac3f64
commit f6d8c7d23c

@ -0,0 +1,91 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.boot.actuate.autoconfigure.web.reactive;
import java.util.Map;
import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFactory;
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.server.WebServerException;
import org.springframework.http.server.reactive.HttpHandler;
import static org.mockito.Mockito.spy;
/**
* Mock {@link ReactiveWebServerFactory}.
*
* @author Brian Clozel
*/
class MockReactiveWebServerFactory extends AbstractReactiveWebServerFactory {
private MockReactiveWebServer webServer;
@Override
public WebServer getWebServer(HttpHandler httpHandler) {
this.webServer = spy(new MockReactiveWebServer(httpHandler, getPort()));
return this.webServer;
}
MockReactiveWebServer getWebServer() {
return this.webServer;
}
static class MockReactiveWebServer implements WebServer {
private final int port;
private HttpHandler httpHandler;
private Map<String, HttpHandler> httpHandlerMap;
MockReactiveWebServer(HttpHandler httpHandler, int port) {
this.httpHandler = httpHandler;
this.port = port;
}
MockReactiveWebServer(Map<String, HttpHandler> httpHandlerMap, int port) {
this.httpHandlerMap = httpHandlerMap;
this.port = port;
}
HttpHandler getHttpHandler() {
return this.httpHandler;
}
Map<String, HttpHandler> getHttpHandlerMap() {
return this.httpHandlerMap;
}
@Override
public void start() throws WebServerException {
}
@Override
public void stop() throws WebServerException {
}
@Override
public int getPort() {
return this.port;
}
}
}

@ -60,7 +60,7 @@ class ReactiveManagementContextFactoryTests {
@Bean @Bean
ReactiveWebServerFactory reactiveWebServerFactory() { ReactiveWebServerFactory reactiveWebServerFactory() {
return mock(ReactiveWebServerFactory.class); return new MockReactiveWebServerFactory();
} }
@Bean @Bean

@ -31,6 +31,7 @@ import org.springframework.context.ApplicationContextException;
import org.springframework.http.server.reactive.HttpHandler; import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
@ -90,7 +91,7 @@ public class ReactiveWebServerApplicationContext extends GenericReactiveWebAppli
String webServerFactoryBeanName = getWebServerFactoryBeanName(); String webServerFactoryBeanName = getWebServerFactoryBeanName();
ReactiveWebServerFactory webServerFactory = getWebServerFactory(webServerFactoryBeanName); ReactiveWebServerFactory webServerFactory = getWebServerFactory(webServerFactoryBeanName);
boolean lazyInit = getBeanFactory().getBeanDefinition(webServerFactoryBeanName).isLazyInit(); boolean lazyInit = getBeanFactory().getBeanDefinition(webServerFactoryBeanName).isLazyInit();
this.serverManager = ServerManager.get(webServerFactory, lazyInit); this.serverManager = new ServerManager(webServerFactory, lazyInit);
} }
initPropertySources(); initPropertySources();
} }
@ -124,8 +125,11 @@ public class ReactiveWebServerApplicationContext extends GenericReactiveWebAppli
private WebServer startReactiveWebServer() { private WebServer startReactiveWebServer() {
ServerManager serverManager = this.serverManager; ServerManager serverManager = this.serverManager;
ServerManager.start(serverManager, this::getHttpHandler); if (serverManager != null) {
return ServerManager.getWebServer(serverManager); serverManager.start(this::getHttpHandler);
return serverManager.server;
}
return null;
} }
/** /**
@ -166,11 +170,13 @@ public class ReactiveWebServerApplicationContext extends GenericReactiveWebAppli
private void stopAndReleaseReactiveWebServer() { private void stopAndReleaseReactiveWebServer() {
ServerManager serverManager = this.serverManager; ServerManager serverManager = this.serverManager;
try { if (serverManager != null) {
ServerManager.stop(serverManager); try {
} serverManager.server.stop();
finally { }
this.serverManager = null; finally {
this.serverManager = null;
}
} }
} }
@ -181,7 +187,8 @@ public class ReactiveWebServerApplicationContext extends GenericReactiveWebAppli
*/ */
@Override @Override
public WebServer getWebServer() { public WebServer getWebServer() {
return ServerManager.getWebServer(this.serverManager); ServerManager serverManager = this.serverManager;
return (serverManager != null) ? serverManager.server : null;
} }
@Override @Override
@ -225,6 +232,7 @@ public class ReactiveWebServerApplicationContext extends GenericReactiveWebAppli
private volatile HttpHandler handler; private volatile HttpHandler handler;
private ServerManager(ReactiveWebServerFactory factory, boolean lazyInit) { private ServerManager(ReactiveWebServerFactory factory, boolean lazyInit) {
Assert.notNull(factory, "ReactiveWebServerFactory must not be null");
this.handler = this::handleUninitialized; this.handler = this::handleUninitialized;
this.server = factory.getWebServer(this); this.server = factory.getWebServer(this);
this.lazyInit = lazyInit; this.lazyInit = lazyInit;
@ -243,31 +251,10 @@ public class ReactiveWebServerApplicationContext extends GenericReactiveWebAppli
return this.handler; return this.handler;
} }
static ServerManager get(ReactiveWebServerFactory factory, boolean lazyInit) { private void start(Supplier<HttpHandler> handlerSupplier) {
return new ServerManager(factory, lazyInit); this.handler = this.lazyInit ? new LazyHttpHandler(Mono.fromSupplier(handlerSupplier))
} : handlerSupplier.get();
this.server.start();
static WebServer getWebServer(ServerManager manager) {
return (manager != null) ? manager.server : null;
}
static void start(ServerManager manager, Supplier<HttpHandler> handlerSupplier) {
if (manager != null && manager.server != null) {
manager.handler = manager.lazyInit ? new LazyHttpHandler(Mono.fromSupplier(handlerSupplier))
: handlerSupplier.get();
manager.server.start();
}
}
static void stop(ServerManager manager) {
if (manager != null && manager.server != null) {
try {
manager.server.stop();
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
} }
} }

Loading…
Cancel
Save