Move class present check to static initializer

Helps with native images
This commit is contained in:
Dave Syer
2020-11-19 10:54:26 +00:00
parent 63498a46c0
commit 01be2060e6

View File

@@ -80,13 +80,14 @@ import static org.springframework.web.reactive.function.server.ServerResponse.st
*/ */
class FunctionEndpointInitializer implements ApplicationContextInitializer<GenericApplicationContext> { class FunctionEndpointInitializer implements ApplicationContextInitializer<GenericApplicationContext> {
private static boolean webflux = ClassUtils.isPresent("org.springframework.http.server.reactive.HttpHandler", null);
@Override @Override
public void initialize(GenericApplicationContext context) { public void initialize(GenericApplicationContext context) {
if (ContextFunctionCatalogInitializer.enabled if (webflux && ContextFunctionCatalogInitializer.enabled
&& context.getEnvironment().getProperty(FunctionalSpringApplication.SPRING_WEB_APPLICATION_TYPE, && context.getEnvironment().getProperty(FunctionalSpringApplication.SPRING_WEB_APPLICATION_TYPE,
WebApplicationType.class, WebApplicationType.REACTIVE) == WebApplicationType.REACTIVE WebApplicationType.class, WebApplicationType.REACTIVE) == WebApplicationType.REACTIVE
&& context.getEnvironment().getProperty("spring.functional.enabled", Boolean.class, false) && context.getEnvironment().getProperty("spring.functional.enabled", Boolean.class, false)) {
&& ClassUtils.isPresent("org.springframework.http.server.reactive.HttpHandler", null)) {
registerEndpoint(context); registerEndpoint(context);
registerWebFluxAutoConfiguration(context); registerWebFluxAutoConfiguration(context);
} }
@@ -101,12 +102,11 @@ class FunctionEndpointInitializer implements ApplicationContextInitializer<Gener
private void registerEndpoint(GenericApplicationContext context) { private void registerEndpoint(GenericApplicationContext context) {
context.registerBean(RequestProcessor.class, context.registerBean(RequestProcessor.class,
() -> new RequestProcessor( () -> new RequestProcessor(context.getBeanProvider(JsonMapper.class),
context.getBeanProvider(JsonMapper.class),
context.getBeanProvider(ServerCodecConfigurer.class))); context.getBeanProvider(ServerCodecConfigurer.class)));
context.registerBean(FunctionEndpointFactory.class, context.registerBean(FunctionEndpointFactory.class,
() -> new FunctionEndpointFactory(context.getBean(FunctionCatalog.class), context.getBean(RequestProcessor.class), () -> new FunctionEndpointFactory(context.getBean(FunctionCatalog.class),
context.getEnvironment())); context.getBean(RequestProcessor.class), context.getEnvironment()));
context.registerBean(RouterFunction.class, context.registerBean(RouterFunction.class,
() -> context.getBean(FunctionEndpointFactory.class).functionEndpoints()); () -> context.getBean(FunctionEndpointFactory.class).functionEndpoints());
} }
@@ -194,8 +194,7 @@ class FunctionEndpointFactory {
private final RequestProcessor processor; private final RequestProcessor processor;
FunctionEndpointFactory(FunctionCatalog functionCatalog, RequestProcessor processor, FunctionEndpointFactory(FunctionCatalog functionCatalog, RequestProcessor processor, Environment environment) {
Environment environment) {
String handler = environment.resolvePlaceholders("${function.handler}"); String handler = environment.resolvePlaceholders("${function.handler}");
if (handler.startsWith("$")) { if (handler.startsWith("$")) {
handler = null; handler = null;
@@ -215,8 +214,8 @@ class FunctionEndpointFactory {
} }
else { else {
String[] accept = FunctionWebUtils.acceptContentTypes(request.headers().accept()); String[] accept = FunctionWebUtils.acceptContentTypes(request.headers().accept());
function = FunctionWebUtils.findFunction(request.method(), functionCatalog, function = FunctionWebUtils.findFunction(request.method(), functionCatalog, request.attributes(),
request.attributes(), request.path(), accept); request.path(), accept);
} }
return function; return function;
} }
@@ -225,8 +224,7 @@ class FunctionEndpointFactory {
public <T> RouterFunction<?> functionEndpoints() { public <T> RouterFunction<?> functionEndpoints() {
return route(POST("/**"), request -> { return route(POST("/**"), request -> {
FunctionInvocationWrapper funcWrapper = extract(request); FunctionInvocationWrapper funcWrapper = extract(request);
Class<?> outputType = funcWrapper == null Class<?> outputType = funcWrapper == null ? Object.class
? Object.class
: FunctionTypeUtils.getRawType(FunctionTypeUtils.getGenericType(funcWrapper.getOutputType())); : FunctionTypeUtils.getRawType(FunctionTypeUtils.getGenericType(funcWrapper.getOutputType()));
FunctionWrapper wrapper = RequestProcessor.wrapper(funcWrapper); FunctionWrapper wrapper = RequestProcessor.wrapper(funcWrapper);
Mono<ResponseEntity<?>> stream = request.bodyToMono(String.class) Mono<ResponseEntity<?>> stream = request.bodyToMono(String.class)
@@ -235,10 +233,10 @@ class FunctionEndpointFactory {
return status(entity.getStatusCode()).headers(headers -> headers.addAll(entity.getHeaders())) return status(entity.getStatusCode()).headers(headers -> headers.addAll(entity.getHeaders()))
.body(entity.hasBody() ? Mono.just((T) entity.getBody()) : Mono.empty(), outputType); .body(entity.hasBody() ? Mono.just((T) entity.getBody()) : Mono.empty(), outputType);
}); });
}) }).andRoute(GET("/**"), request -> {
.andRoute(GET("/**"), request -> {
FunctionInvocationWrapper funcWrapper = extract(request); FunctionInvocationWrapper funcWrapper = extract(request);
Class<?> outputType = FunctionTypeUtils.getRawType(FunctionTypeUtils.getGenericType(funcWrapper.getOutputType())); Class<?> outputType = FunctionTypeUtils
.getRawType(FunctionTypeUtils.getGenericType(funcWrapper.getOutputType()));
if (funcWrapper.isSupplier()) { if (funcWrapper.isSupplier()) {
Object result = FunctionWebUtils.invokeFunction(funcWrapper, null, funcWrapper.isInputTypeMessage()); Object result = FunctionWebUtils.invokeFunction(funcWrapper, null, funcWrapper.isInputTypeMessage());
if (!(result instanceof Publisher)) { if (!(result instanceof Publisher)) {
@@ -251,9 +249,11 @@ class FunctionEndpointFactory {
wrapper.headers(request.headers().asHttpHeaders()); wrapper.headers(request.headers().asHttpHeaders());
String argument = (String) request.attribute(WebRequestConstants.ARGUMENT).get(); String argument = (String) request.attribute(WebRequestConstants.ARGUMENT).get();
wrapper.argument(Flux.just(argument)); wrapper.argument(Flux.just(argument));
Object result = FunctionWebUtils.invokeFunction(funcWrapper, wrapper.argument(), funcWrapper.isInputTypeMessage()); Object result = FunctionWebUtils.invokeFunction(funcWrapper, wrapper.argument(),
funcWrapper.isInputTypeMessage());
return ServerResponse.ok().body(result, outputType); return ServerResponse.ok().body(result, outputType);
} }
}); });
} }
} }